diff options
-rw-r--r-- | app/controllers/request_controller.rb | 2 | ||||
-rw-r--r-- | app/controllers/track_controller.rb | 31 | ||||
-rw-r--r-- | app/models/info_request.rb | 30 | ||||
-rw-r--r-- | app/models/info_request_event.rb | 31 | ||||
-rw-r--r-- | spec/controllers/track_controller_spec.rb | 18 |
5 files changed, 54 insertions, 58 deletions
diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb index a66fdf5bb..545a40cfe 100644 --- a/app/controllers/request_controller.rb +++ b/app/controllers/request_controller.rb @@ -71,7 +71,7 @@ class RequestController < ApplicationController respond_to do |format| format.html { @has_json = true } - format.json { render :json => @info_request.json_for_api } + format.json { render :json => @info_request.json_for_api(true) } end end diff --git a/app/controllers/track_controller.rb b/app/controllers/track_controller.rb index 224e8a1bf..89fc74706 100644 --- a/app/controllers/track_controller.rb +++ b/app/controllers/track_controller.rb @@ -125,33 +125,10 @@ class TrackController < ApplicationController @xapian_object = perform_search([InfoRequestEvent], @track_thing.track_query, @track_thing.params[:feed_sortby], nil, 25, 1) respond_to do |format| format.atom { render :template => 'track/atom_feed' } - format.json { - render :json => - @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, - :info_request => r[:model].info_request.url_title, - :public_body => r[:model].info_request.public_body.url_name, - :user => r[:model].info_request.user.url_name, - # params_yaml has possibly sensitive data in it, don't include it - :created_at => r[:model].created_at, - :described_state => r[:model].described_state, - :calculated_state => r[:model].calculated_state, - :last_described_at => r[:model].last_described_at, - :incoming_message_id => r[:model].incoming_message_id, - :outgoing_message_id => r[:model].outgoing_message_id, - :comment_id => r[:model].comment_id, - - :snippet => @template.highlight_and_excerpt(r[:model].search_text_main(true), @xapian_object.words_to_highlight, 150) - # 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 + format.json { render :json => @xapian_object.results.map { |r| r[:model].json_for_api(true, + lambda { |t| @template.highlight_and_excerpt(t, @xapian_object.words_to_highlight, 150) } + ) } } + end end # Change or delete a track diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 6b9e7b674..f35ef7f7d 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -958,13 +958,11 @@ public return info_request end - def json_for_api - return { + def json_for_api(deep) + ret = { :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, @@ -976,24 +974,14 @@ public # 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 - } } - } + + if deep + ret[:user] = self.user.json_for_api + ret[:public_body] = self.public_body.json_for_api + ret[:info_request_events] = self.info_request_events.map { |e| e.json_for_api(false) } + end + return ret end end diff --git a/app/models/info_request_event.rb b/app/models/info_request_event.rb index ab7844a4d..a6ef2b58c 100644 --- a/app/models/info_request_event.rb +++ b/app/models/info_request_event.rb @@ -370,6 +370,37 @@ class InfoRequestEvent < ActiveRecord::Base return league_table end + def json_for_api(deep, snippet_highlight_proc = nil) + ret = { + :id => self.id, + :event_type => self.event_type, + # params_yaml has possibly sensitive data in it, don't include it + :created_at => self.created_at, + :described_state => self.described_state, + :calculated_state => self.calculated_state, + :last_described_at => self.last_described_at, + :incoming_message_id => self.incoming_message_id, + :outgoing_message_id => self.outgoing_message_id, + :comment_id => self.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 + # perhaps can call with @template somehow + } + + if !snippet_highlight_proc.nil? + ret[:snippet] = snippet_highlight_proc.call(self.search_text_main(true)) + end + + if deep + ret[:info_request] = self.info_request.json_for_api(false) + ret[:public_body] = self.info_request.public_body.json_for_api + ret[:user] = self.info_request.user.json_for_api + end + + return ret + end + end diff --git a/spec/controllers/track_controller_spec.rb b/spec/controllers/track_controller_spec.rb index eb0f632c4..13c3d4716 100644 --- a/spec/controllers/track_controller_spec.rb +++ b/spec/controllers/track_controller_spec.rb @@ -128,17 +128,17 @@ describe TrackController, "when viewing JSON version of a track feed" do a[1]['id'].should == info_request_events(:useless_incoming_message_event).id a[2]['id'].should == info_request_events(:useless_outgoing_message_event).id - a[0]['info_request'].should == 'why_do_you_have_such_a_fancy_dog' - a[1]['info_request'].should == 'why_do_you_have_such_a_fancy_dog' - a[2]['info_request'].should == 'why_do_you_have_such_a_fancy_dog' + a[0]['info_request']['url_title'].should == 'why_do_you_have_such_a_fancy_dog' + a[1]['info_request']['url_title'].should == 'why_do_you_have_such_a_fancy_dog' + a[2]['info_request']['url_title'].should == 'why_do_you_have_such_a_fancy_dog' - a[0]['public_body'].should == 'tgq' - a[1]['public_body'].should == 'tgq' - a[2]['public_body'].should == 'tgq' + a[0]['public_body']['url_name'].should == 'tgq' + a[1]['public_body']['url_name'].should == 'tgq' + a[2]['public_body']['url_name'].should == 'tgq' - a[0]['user'].should == 'bob_smith' - a[1]['user'].should == 'bob_smith' - a[2]['user'].should == 'bob_smith' + a[0]['user']['url_name'].should == 'bob_smith' + a[1]['user']['url_name'].should == 'bob_smith' + a[2]['user']['url_name'].should == 'bob_smith' a[0]['event_type'].should == 'comment' a[1]['event_type'].should == 'response' |