aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Houston <robin.houston@gmail.com>2012-07-04 16:38:54 +0100
committerRobin Houston <robin.houston@gmail.com>2012-07-04 16:38:54 +0100
commita2791ac03e5715f98cb84f1fc3cbd9f103ef2d7b (patch)
treea59d4d0dd78ed47fa616680326273fa9ddd6ee87
parent525575961e656c812e7d1c2ab04b1fa0a351f6a2 (diff)
parentd314c21449823f62afdb708b27ad327443162d8c (diff)
Merge branch 'feature/public-body-api-2a' into develop
-rw-r--r--app/controllers/api_controller.rb51
-rw-r--r--app/models/info_request.rb6
-rw-r--r--app/models/public_body.rb4
-rw-r--r--app/models/user.rb3
-rw-r--r--app/views/api/request_events.atom.builder25
-rw-r--r--config/routes.rb6
-rw-r--r--spec/controllers/api_controller_spec.rb54
7 files changed, 137 insertions, 12 deletions
diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb
index 524aa44b7..2ba88eb1a 100644
--- a/app/controllers/api_controller.rb
+++ b/app/controllers/api_controller.rb
@@ -155,6 +155,57 @@ class ApiController < ApplicationController
head :no_content
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"
+ # 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
def check_api_key
raise "Missing required parameter 'k'" if params[:k].nil?
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index d09acbcf6..a41d6d2db 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -1,11 +1,10 @@
# == Schema Information
-# Schema version: 114
#
# Table name: info_requests
#
# id :integer not null, primary key
# title :text not null
-# user_id :integer not null
+# user_id :integer
# public_body_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
@@ -17,10 +16,11 @@
# allow_new_responses_from :string(255) default("anybody"), not null
# handle_rejected_responses :string(255) default("bounce"), not null
# idhash :string(255) not null
+# external_user_name :string(255)
+# external_url :string(255)
# attention_requested :boolean default(FALSE)
#
-
require 'digest/sha1'
class InfoRequest < ActiveRecord::Base
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index bc8f084bb..9efeadf55 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -1,5 +1,4 @@
# == Schema Information
-# Schema version: 114
#
# Table name: public_bodies
#
@@ -19,7 +18,6 @@
# publication_scheme :text default(""), not null
# api_key :string(255) not null
#
-
# models/public_body.rb:
# A public body, from which information can be requested.
#
@@ -583,5 +581,3 @@ class PublicBody < ActiveRecord::Base
end
end
-
-
diff --git a/app/models/user.rb b/app/models/user.rb
index a21676f68..657ea2a4a 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1,5 +1,4 @@
# == Schema Information
-# Schema version: 114
#
# Table name: users
#
@@ -21,9 +20,7 @@
# email_bounce_message :text default(""), not null
# no_limit :boolean default(FALSE), not null
# receive_email_alerts :boolean default(TRUE), not null
-# user_similarity_id :integer
#
-
# models/user.rb:
# Model of people who use the site to file requests, make comments etc.
#
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 13ab6669e..a9c2c889a 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -244,8 +244,10 @@ ActionController::Routing::Routes.draw do |map|
map.with_options :controller => 'api' do |api|
api.api_create_request '/api/v2/request.json', :action => 'create_request', :conditions => { :method => :post }
- 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_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_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..98751a93a 100644
--- a/spec/controllers/api_controller_spec.rb
+++ b/spec/controllers/api_controller_spec.rb
@@ -260,4 +260,58 @@ 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 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,
+ :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
+
+ 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