aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Houston <robin.houston@gmail.com>2012-07-04 13:45:29 +0100
committerRobin Houston <robin.houston@gmail.com>2012-07-04 13:45:29 +0100
commitd5a43f1aa76c1e0086f08bec9c575d2a41ae9a9b (patch)
tree9c8a851c0f6e26b40be181f705bae582df20c90b
parent886a8baef232cce37c829137a8b6a625d16b0504 (diff)
Atom feed of request events
We need not only new requests, but new outgoing correspondence of any sort. The idea is that this feed will contain any event that would have triggered an email to be sent to the public body, so can be used as an alternative, equivalent way to stay up-to-date with happenings on WDTK (or the Alaveteli installation of choice).
-rw-r--r--app/controllers/api_controller.rb20
-rwxr-xr-xapp/helpers/link_to_helper.rb6
-rw-r--r--app/views/api/new_requests.atom.builder22
-rw-r--r--app/views/api/request_events.atom.builder25
-rw-r--r--config/routes.rb2
-rw-r--r--spec/controllers/api_controller_spec.rb16
6 files changed, 57 insertions, 34 deletions
diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb
index a8c9b5fef..3001ca3cb 100644
--- a/app/controllers/api_controller.rb
+++ b/app/controllers/api_controller.rb
@@ -155,17 +155,27 @@ class ApiController < ApplicationController
head :no_content
end
- def body_new_requests
+ def body_request_events
feed_type = params[:feed_type]
raise PermissionDenied.new("#{@public_body.id} != #{params[:id]}") if @public_body.id != params[:id].to_i
- @requests = @public_body.info_requests
+ @events = InfoRequestEvent.find_by_sql([
+ %(select info_request_events.*
+ from info_requests
+ join info_request_events on info_requests.id = info_request_events.info_request_id
+ where info_requests.public_body_id = ?
+ and info_request_events.event_type in (
+ 'sent', 'followup_sent', 'resent', 'followup_resent'
+ )
+ order by info_request_events.created_at desc
+ ), @public_body.id
+ ])
if feed_type == "atom"
- render :template => "api/new_requests.atom", :layout => false
+ render :template => "api/request_events.atom", :layout => false
elsif feed_type == "json"
- render :json => @requests
+ render :json => @events
else
- raise ActiveRecord::RecordNotFound.new("Unrecognised feed type: " + feed_type)
+ raise ActiveRecord::RecordNotFound.new("Unrecognised feed type: #{feed_type}")
end
end
diff --git a/app/helpers/link_to_helper.rb b/app/helpers/link_to_helper.rb
index efa20b723..1a86333b6 100755
--- a/app/helpers/link_to_helper.rb
+++ b/app/helpers/link_to_helper.rb
@@ -16,12 +16,6 @@ module LinkToHelper
params = {:url_title => info_request.url_title, :only_path => true}
return show_request_url(params.merge(extra_params))
end
-
- def info_request_url(info_request)
- # This method is called implicitly by the builder view views/api/new_requests.atom.builder
- # to construct the URLs for the feed entries. Therefore we return fully-qualified URLs here.
- return main_url(request_url(info_request))
- end
def request_link(info_request, cls=nil )
link_to h(info_request.title), request_url(info_request), :class => cls
diff --git a/app/views/api/new_requests.atom.builder b/app/views/api/new_requests.atom.builder
deleted file mode 100644
index bd5c2f90e..000000000
--- a/app/views/api/new_requests.atom.builder
+++ /dev/null
@@ -1,22 +0,0 @@
-atom_feed do |feed|
- feed.title("New requests made to #{@public_body.name}")
- feed.updated(@requests.first.updated_at)
-
- puts @requests.inspect
- for request in @requests
- feed.entry(request) do |entry|
- entry.updated(request.updated_at)
- entry.published(request.created_at)
- entry.title(request.title)
- entry.content(request.last_event_forming_initial_request.outgoing_message.body, :type => 'text')
- entry.author do |author|
- author.name(request.user_name)
- if !request.user.nil?
- author.uri(main_url(user_url(request.user)))
- end
- author.email(request.incoming_email)
- end
- end
- end
-end
-
diff --git a/app/views/api/request_events.atom.builder b/app/views/api/request_events.atom.builder
new file mode 100644
index 000000000..4f0133051
--- /dev/null
+++ b/app/views/api/request_events.atom.builder
@@ -0,0 +1,25 @@
+atom_feed("xmlns:alaveteli" => "http://www.alaveteli.org/API/v2/RequestEvents/Atom") do |feed|
+ feed.title("Events relating to #{@public_body.name}")
+ feed.updated(@events.first.created_at)
+
+ for event in @events
+ feed.entry(event) do |entry|
+ request = event.info_request
+
+ entry.published(event.created_at)
+ entry.tag!("alaveteli:event_type", event.event_type)
+ entry.tag!("alaveteli:request_url", main_url(request_url(request)))
+ entry.title(request.title)
+
+ entry.content(event.outgoing_message.body, :type => 'text')
+
+ entry.author do |author|
+ author.name(request.user_name)
+ if !request.user.nil?
+ author.uri(main_url(user_url(request.user)))
+ end
+ author.email(request.incoming_email)
+ end
+ end
+ end
+end
diff --git a/config/routes.rb b/config/routes.rb
index e76d86816..a9c2c889a 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -247,7 +247,7 @@ ActionController::Routing::Routes.draw do |map|
api.api_show_request '/api/v2/request/:id.json', :action => 'show_request', :conditions => { :method => :get }
api.api_add_correspondence '/api/v2/request/:id.json', :action => 'add_correspondence', :conditions => { :method => :post }
- api.api_body_new_requests '/api/v2/body/:id/new_requests.:feed_type', :action => 'body_new_requests', :feed_type => '^(json|atom)$'
+ api.api_body_request_events '/api/v2/body/:id/request_events.:feed_type', :action => 'body_request_events', :feed_type => '^(json|atom)$'
end
map.filter('conditionallyprependlocale')
diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb
index 1f65576b6..39ffae7fc 100644
--- a/spec/controllers/api_controller_spec.rb
+++ b/spec/controllers/api_controller_spec.rb
@@ -260,4 +260,20 @@ describe ApiController, "when using the API" do
# assigns them and changing assignment to an equality
# check, which does not really test anything at all.
end
+
+ it "should show a feed of new request events" do
+ get :body_request_events,
+ :id => public_bodies(:geraldine_public_body).id,
+ :k => public_bodies(:geraldine_public_body).api_key,
+ :feed_type => "atom"
+
+ response.should be_success
+ response.should render_template("api/request_events.atom")
+ assigns[:events].size.should > 0
+ assigns[:events].each do |event|
+ event.info_request.public_body.should == public_bodies(:geraldine_public_body)
+ event.outgoing_message.should_not be_nil
+ event.event_type.should satisfy {|x| ['sent', 'followup_sent', 'resent', 'followup_resent'].include?(x)}
+ end
+ end
end