aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/controllers/api_controller.rb46
-rw-r--r--spec/controllers/api_controller_spec.rb15
2 files changed, 50 insertions, 11 deletions
diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb
index 6c98ebeba..409a432eb 100644
--- a/app/controllers/api_controller.rb
+++ b/app/controllers/api_controller.rb
@@ -167,17 +167,41 @@ class ApiController < ApplicationController
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
- ])
+ since_date_str = params[:since_date]
+ if since_date_str.nil?
+ @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
+ ])
+ else
+ begin
+ since_date = Date.strptime(since_date_str, "%Y-%m-%d")
+ rescue ArgumentError
+ render :json => {"errors" => [
+ "Parameter since_date must be in format yyyy-mm-dd (not '#{since_date_str}')" ] },
+ :status => 500
+ return
+ end
+ @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'
+ )
+ and info_request_events.created_at >= ?
+ order by info_request_events.created_at desc
+ ), @public_body.id, since_date
+ ])
+ end
if feed_type == "atom"
render :template => "api/request_events.atom", :layout => false
elsif feed_type == "json"
diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb
index 925b7adb4..ded9a040a 100644
--- a/spec/controllers/api_controller_spec.rb
+++ b/spec/controllers/api_controller_spec.rb
@@ -320,6 +320,21 @@ describe ApiController, "when using the API" do
assigns[:event_data].should == [first_event]
end
+ it "should honour the since_date parameter for the Atom feed" do
+ get :body_request_events,
+ :id => public_bodies(:humpadink_public_body).id,
+ :k => public_bodies(:humpadink_public_body).api_key,
+ :since_date => "2010-01-01",
+ :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.created_at.should >= Date.new(2010, 1, 1)
+ end
+ end
+
it "should return a JSON 404 error for non-existent requests" do
request_id = 123459876 # Let's hope this doesn't exist!
sent_at = "2012-05-28T12:35:39+01:00"