aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/controllers/api_controller.rb69
-rw-r--r--spec/controllers/api_controller_spec.rb40
2 files changed, 87 insertions, 22 deletions
diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb
index 3001ca3cb..2ba88eb1a 100644
--- a/app/controllers/api_controller.rb
+++ b/app/controllers/api_controller.rb
@@ -156,27 +156,54 @@ class ApiController < ApplicationController
end
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
-
- @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/request_events.atom", :layout => false
- elsif feed_type == "json"
- render :json => @events
- else
- raise ActiveRecord::RecordNotFound.new("Unrecognised feed type: #{feed_type}")
- end
+ feed_type = params[:feed_type]
+ raise PermissionDenied.new("#{@public_body.id} != #{params[:id]}") if @public_body.id != params[:id].to_i
+
+ @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/request_events.atom", :layout => false
+ elsif feed_type == "json"
+ # For the JSON feed, we take a "since" parameter that allows the client
+ # to restrict to events more recent than a certain other event
+ if params[:since_event_id]
+ @since_event_id = params[:since_event_id].to_i
+ end
+ @event_data = []
+ @events.each do |event|
+ break if event.id == @since_event_id
+
+ request = event.info_request
+ this_event = {
+ :event_id => event.id,
+ :created_at => event.created_at.iso8601,
+ :event_type => event.event_type,
+ :request_url => main_url(request_url(request)),
+ :request_email => request.incoming_email,
+ :title => request.title,
+ :body => event.outgoing_message.body,
+
+ :user_name => request.user_name,
+ }
+ if request.user
+ this_event[:user_url] = main_url(user_url(request.user))
+ end
+
+ @event_data.push(this_event)
+ end
+ render :json => @event_data
+ else
+ raise ActiveRecord::RecordNotFound.new("Unrecognised feed type: #{feed_type}")
+ end
end
protected
diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb
index 39ffae7fc..98751a93a 100644
--- a/spec/controllers/api_controller_spec.rb
+++ b/spec/controllers/api_controller_spec.rb
@@ -261,7 +261,7 @@ describe ApiController, "when using the API" do
# check, which does not really test anything at all.
end
- it "should show a feed of new request events" do
+ it "should show an Atom 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,
@@ -276,4 +276,42 @@ describe ApiController, "when using the API" do
event.event_type.should satisfy {|x| ['sent', 'followup_sent', 'resent', 'followup_resent'].include?(x)}
end
end
+
+ it "should show a JSON 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 => "json"
+
+ response.should be_success
+ 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
+
+ assigns[:event_data].size.should == assigns[:events].size
+ assigns[:event_data].each do |event_record|
+ event_record[:event_type].should satisfy {|x| ['sent', 'followup_sent', 'resent', 'followup_resent'].include?(x)}
+ end
+ end
+
+ it "should honour the since_event_id parameter" do
+ get :body_request_events,
+ :id => public_bodies(:geraldine_public_body).id,
+ :k => public_bodies(:geraldine_public_body).api_key,
+ :feed_type => "json"
+ response.should be_success
+ first_event = assigns[:event_data][0]
+ second_event_id = assigns[:event_data][1][:event_id]
+
+ get :body_request_events,
+ :id => public_bodies(:geraldine_public_body).id,
+ :k => public_bodies(:geraldine_public_body).api_key,
+ :feed_type => "json",
+ :since_event_id => second_event_id
+ response.should be_success
+ assigns[:event_data].should == [first_event]
+ end
end