diff options
-rw-r--r-- | app/controllers/request_controller.rb | 38 | ||||
-rw-r--r-- | app/controllers/track_controller.rb | 4 | ||||
-rw-r--r-- | app/models/info_request.rb | 38 | ||||
-rw-r--r-- | spec/controllers/request_controller_spec.rb | 6 | ||||
-rw-r--r-- | todo.txt | 2 |
5 files changed, 46 insertions, 42 deletions
diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb index 8425b33d6..a66fdf5bb 100644 --- a/app/controllers/request_controller.rb +++ b/app/controllers/request_controller.rb @@ -71,43 +71,7 @@ class RequestController < ApplicationController respond_to do |format| format.html { @has_json = true } - format.json { - render :json => { - :id => @info_request.id, - :url_title => @info_request.url_title, - :title => @info_request.title, - :user => @info_request.user.url_name, - :public_body => @info_request.public_body.url_name, - :created_at => @info_request.created_at, - :updated_at => @info_request.updated_at, - :described_state => @info_request.described_state, - :awaiting_description => @info_request.awaiting_description , - :prominence => @info_request.prominence, - :law_used => @info_request.law_used, - :tags => @info_request.tag_array, - - # not sure we need to make these, mainly anti-spam, admin params public - # allow_new_responses_from - # handle_rejected_responses - - :info_request_events => @info_request_events.map { |e| { - # XXX this code is partly duplicated with the track controller - :id => e.id, - :event_type => e.event_type, - # params_yaml has possibly sensitive data in it, don't include it - :created_at => e.created_at, - :described_state => e.described_state, - :calculated_state => e.calculated_state, - :last_described_at => e.last_described_at, - :incoming_message_id => e.incoming_message_id, - :outgoing_message_id => e.outgoing_message_id, - :comment_id => e.comment_id, - # XXX would be nice to add links here, but alas the - # code to make them is in views only. See views/request/details.rhtml - } } - - } - } + format.json { render :json => @info_request.json_for_api } end end diff --git a/app/controllers/track_controller.rb b/app/controllers/track_controller.rb index 72ee13f98..224e8a1bf 100644 --- a/app/controllers/track_controller.rb +++ b/app/controllers/track_controller.rb @@ -127,7 +127,9 @@ class TrackController < ApplicationController format.atom { render :template => 'track/atom_feed' } format.json { render :json => - @xapian_object.results.map { |r| { + @xapian_object.results.map { |r| r[:model]. + + { # XXX this code is partly duplicated with the request controller :id => r[:model].id, :event_type => r[:model].event_type, diff --git a/app/models/info_request.rb b/app/models/info_request.rb index b485bd80e..6b9e7b674 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -957,6 +957,44 @@ public end return info_request end + + def json_for_api + return { + :id => self.id, + :url_title => self.url_title, + :title => self.title, + :user => self.user.json_for_api, + :public_body => self.public_body.json_for_api, + :created_at => self.created_at, + :updated_at => self.updated_at, + :described_state => self.described_state, + :awaiting_description => self.awaiting_description , + :prominence => self.prominence, + :law_used => self.law_used, + :tags => self.tag_array, + + # not sure we need to make these, mainly anti-spam, admin params public + # allow_new_responses_from + # handle_rejected_responses + + :info_request_events => self.info_request_events.map { |e| { + # XXX this code is partly duplicated with the track controller + :id => e.id, + :event_type => e.event_type, + # params_yaml has possibly sensitive data in it, don't include it + :created_at => e.created_at, + :described_state => e.described_state, + :calculated_state => e.calculated_state, + :last_described_at => e.last_described_at, + :incoming_message_id => e.incoming_message_id, + :outgoing_message_id => e.outgoing_message_id, + :comment_id => e.comment_id, + # XXX would be nice to add links here, but alas the + # code to make them is in views only. See views/request/details.rhtml + } } + + } + end end diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index eddbe7869..648a66e28 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -1225,15 +1225,15 @@ describe RequestController, "when showing JSON version for API" do fixtures :info_requests, :info_request_events, :public_bodies, :users, :incoming_messages, :raw_emails, :outgoing_messages, :comments - it "should be successful" do + it "should return data in JSON form" do get :show, :url_title => 'why_do_you_have_such_a_fancy_dog', :format => 'json' ir = JSON.parse(response.body) ir.class.to_s.should == 'Hash' ir['url_title'].should == 'why_do_you_have_such_a_fancy_dog' - ir['public_body'].should == 'tgq' - ir['user'].should == 'bob_smith' + ir['public_body']['url_name'].should == 'tgq' + ir['user']['url_name'].should == 'bob_smith' end end @@ -2,7 +2,7 @@ API: * Displayable status (i.e. text not code) as parameter on info requests * Get all the tags for a given request in RSS feed JSONs -* Add stuff to allow AJAX callbacks +* Add stuff to allow Javascript callbacks (JSONP) * Pagination on the Atom feed JSON, so you can get later pages, and/or choose > 25 items |