Skip to content

Commit

Permalink
make v6 API more consistent with CIteproc JSON. #287
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Apr 11, 2015
1 parent 2b2f172 commit ed5e336
Show file tree
Hide file tree
Showing 22 changed files with 192 additions and 202 deletions.
4 changes: 4 additions & 0 deletions app/decorators/alert_decorator.rb
Expand Up @@ -21,4 +21,8 @@ def source
def work
work_id ? model.work.to_param : nil
end

def timestamp
model.create_date
end
end
4 changes: 4 additions & 0 deletions app/decorators/relation_decorator.rb
Expand Up @@ -22,4 +22,8 @@ def event_id
def source_id
model.source.name
end

def timestamp
model.update_date
end
end
4 changes: 4 additions & 0 deletions app/decorators/retrieval_status_decorator.rb
Expand Up @@ -17,4 +17,8 @@ def source_id
def work_id
work.pid
end

def timestamp
model.update_date
end
end
4 changes: 4 additions & 0 deletions app/decorators/source_decorator.rb
Expand Up @@ -22,6 +22,10 @@ def id
name
end

def timestamp
model.update_date
end

def responses
{ "count" => response_count,
"average" => average_count,
Expand Down
20 changes: 20 additions & 0 deletions app/decorators/work_decorator.rb
Expand Up @@ -32,10 +32,30 @@ def url
canonical_url
end

def DOI
model.doi
end

def URL
model.canonical_url
end

def PMID
model.pmid
end

def PMCID
model.pmcid
end

def mendeley
mendeley_uuid
end

def timestamp
model.update_date
end

def events
model.events
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/api/v6/alerts/index.json.jbuilder
Expand Up @@ -9,6 +9,6 @@ end

json.data @alerts do |alert|
json.cache! ['v6', alert], skip_digest: true do
json.(alert, :id, :level, :class_name, :message, :status, :hostname, :target_url, :source, :work, :unresolved, :create_date)
json.(alert, :id, :level, :class_name, :message, :status, :hostname, :target_url, :source, :work, :unresolved, :timestamp)
end
end
2 changes: 1 addition & 1 deletion app/views/api/v6/docs/index.json.jbuilder
Expand Up @@ -7,6 +7,6 @@ end

json.docs @docs do |doc|
json.cache! ['v6', @doc], skip_digest: true do
json.(doc, :id, :title, :update_date)
json.(doc, :id, :title, :timestamp)
end
end
2 changes: 1 addition & 1 deletion app/views/api/v6/docs/show.json.jbuilder
Expand Up @@ -6,6 +6,6 @@ end

json.doc do
json.cache! ['v6', @doc], skip_digest: true do
json.(@doc, :id, :title, :layout, :content, :update_date)
json.(@doc, :id, :title, :layout, :content, :timestamp)
end
end
9 changes: 6 additions & 3 deletions app/views/api/v6/events/index.json.jbuilder
Expand Up @@ -9,8 +9,11 @@ end

json.events @events do |event|
json.cache! ['v6', "event", event, params[:work_id], params[:source_id], params[:relation_type_id]], skip_digest: true do

json.(event.work, :id, :title, :issued, :container_title, :volume, :page, :issue, :publisher_id, :doi, :url, :pmid, :pmcid, :scp, :wos, :ark, :metrics)
json.(event, :source_id, :event_id, :relation_type_id, :update_date)
json.(event.work, :id, :publisher_id)
json.(event, :event_id, :source_id, :relation_type_id)
json.(event.work, :title, :issued)
json.set! :"container-title", event.work.container_title
json.(event.work, :volume, :page, :issue, :DOI, :URL, :PMID, :PMCID, :scp, :wos, :ark, :metrics)
json.(event, :timestamp)
end
end
9 changes: 6 additions & 3 deletions app/views/api/v6/references/index.json.jbuilder
Expand Up @@ -9,8 +9,11 @@ end

json.references @references do |reference|
json.cache! ['v6', "reference", reference, params[:work_id], params[:source_id], params[:relation_type_id]], skip_digest: true do

json.(reference.related_work, :id, :title, :issued, :container_title, :volume, :page, :issue, :publisher_id, :doi, :url, :pmid, :pmcid, :scp, :wos, :ark, :metrics)
json.(reference, :reference_id, :source_id, :relation_type_id, :update_date)
json.(reference.related_work, :id, :publisher_id)
json.(reference, :reference_id, :source_id, :relation_type_id)
json.(reference.related_work, :title, :issued)
json.set! :"container-title", reference.related_work.container_title
json.(reference.related_work, :volume, :page, :issue, :DOI, :URL, :PMID, :PMCID, :scp, :wos, :ark, :metrics)
json.(reference, :timestamp)
end
end
2 changes: 1 addition & 1 deletion app/views/api/v6/relation_types/index.json.jbuilder
Expand Up @@ -7,6 +7,6 @@ end

json.relation_types @relation_types do |relation_type|
json.cache! ['v6', relation_type], skip_digest: true do
json.(relation_type, :id, :title, :inverse_title, :update_date)
json.(relation_type, :id, :title, :inverse_title, :timestamp)
end
end
2 changes: 1 addition & 1 deletion app/views/api/v6/relation_types/show.json.jbuilder
Expand Up @@ -6,6 +6,6 @@ end

json.relation_type do
json.cache! ['v6', @relation_type], skip_digest: true do
json.(@relation_type, :id, :title, :inverse_title, :update_date)
json.(@relation_type, :id, :title, :inverse_title, :timestamp)
end
end
2 changes: 1 addition & 1 deletion app/views/api/v6/sources/index.json.jbuilder
Expand Up @@ -7,6 +7,6 @@ end

json.sources @sources do |source|
json.cache! ['v6', source], skip_digest: true do
json.(source, :id, :title, :group_id, :description, :state, :error_count, :work_count, :event_count, :status, :responses, :by_day, :by_month, :update_date)
json.(source, :id, :title, :group_id, :description, :state, :error_count, :work_count, :event_count, :status, :responses, :by_day, :by_month, :timestamp)
end
end
2 changes: 1 addition & 1 deletion app/views/api/v6/sources/show.json.jbuilder
Expand Up @@ -6,6 +6,6 @@ end

json.source do
json.cache! ['v6', @source], skip_digest: true do
json.(@source, :id, :title, :group_id, :description, :state, :error_count, :work_count, :event_count, :status, :responses, :by_day, :by_month, :update_date)
json.(@source, :id, :title, :group_id, :description, :state, :error_count, :work_count, :event_count, :status, :responses, :by_day, :by_month, :timestamp)
end
end
4 changes: 3 additions & 1 deletion app/views/api/v6/status/index.json.jbuilder
Expand Up @@ -9,10 +9,12 @@ end

json.status @status do |status|
json.cache! ['v6', status, @user], skip_digest: true do
json.(status, :id, :works_count, :works_new_count, :sources, :events_count, :responses_count, :requests_count, :requests_average, :version, :update_date)
json.(status, :id, :works_count, :works_new_count, :sources, :events_count, :responses_count, :requests_count, :requests_average, :version)

if current_user && current_user.is_admin_or_staff?
json.(status, :alerts_count, :db_size)
end

json.(status :timestamp)
end
end
2 changes: 1 addition & 1 deletion app/views/api/v6/work_types/index.json.jbuilder
Expand Up @@ -7,6 +7,6 @@ end

json.work_types @work_types do |work_type|
json.cache! ['v6', work_type], skip_digest: true do
json.(work_type, :id, :title, :container, :update_date)
json.(work_type, :id, :title, :container, :timestamp)
end
end
2 changes: 1 addition & 1 deletion app/views/api/v6/work_types/show.json.jbuilder
Expand Up @@ -6,6 +6,6 @@ end

json.work_type do
json.cache! ['v6', @work_type], skip_digest: true do
json.(@work_type, :id, :title, :container, :update_date)
json.(@work_type, :id, :title, :container, :timestamp)
end
end
4 changes: 3 additions & 1 deletion app/views/api/v6/works/index.json.jbuilder
Expand Up @@ -9,6 +9,8 @@ end

json.works @works do |work|
json.cache! ['v6', work], skip_digest: true do
json.(work, :id, :title, :issued, :container_title, :volume, :page, :issue, :publisher_id, :doi, :url, :pmid, :pmcid, :scp, :wos, :ark, :metrics, :update_date)
json.(work, :id, :publisher_id, :title, :issued)
json.set! :"container-title", work.container_title
json.(work, :volume, :page, :issue, :DOI, :URL, :PMID, :PMCID, :scp, :wos, :ark, :metrics, :timestamp)
end
end
4 changes: 3 additions & 1 deletion app/views/api/v6/works/show.json.jbuilder
Expand Up @@ -6,6 +6,8 @@ end

json.work do
json.cache! ['v6', @work], skip_digest: true do
json.(@work, :id, :title, :issued, :container_title, :volume, :page, :issue, :publisher_id, :doi, :url, :pmid, :pmcid, :scp, :wos, :ark, :metrics, :update_date)
json.(@work, :id, :publisher_id, :title, :issued)
json.set! :"container-title", @work.container_title
json.(@work, :volume, :page, :issue, :DOI, :URL, :PMID, :PMCID, :scp, :wos, :ark, :metrics, :timestamp)
end
end
26 changes: 15 additions & 11 deletions spec/apis/v6/alerts_spec.rb
@@ -1,16 +1,20 @@
require "rails_helper"

describe "/api/v6/alerts", :type => :api do
let(:error) { { "error" => "You are not authorized to access this page."} }
let(:user) { FactoryGirl.create(:admin_user) }
let(:error) { { "meta"=> { "status"=>"error", "error"=>"You are not authorized to access this page." } } }
let(:user) { FactoryGirl.create(:user) }
let(:headers) do
{ "HTTP_ACCEPT" => "application/json",
"Authorization" => "Token token=#{user.api_key}" }
end
let(:jsonp_headers) do
{ "HTTP_ACCEPT" => "application/javascript",
"Authorization" => "Token token=#{user.api_key}" }
end

context "index" do
context "most recent articles" do
let(:uri) { "/api/v6/alerts" }
let(:uri) { "/api/alerts" }
let!(:alert) { FactoryGirl.create_list(:alert, 55) }

it "JSON" do
Expand All @@ -27,7 +31,7 @@
end

context "only unresolved alerts" do
let(:uri) { "/api/v6/alerts?unresolved=1" }
let(:uri) { "/api/alerts?unresolved=1" }

before(:each) do
FactoryGirl.create_list(:alert, 2, unresolved: false)
Expand All @@ -47,7 +51,7 @@
end

context "with source" do
let(:uri) { "/api/v6/alerts?source_id=citeulike" }
let(:uri) { "/api/alerts?source_id=citeulike" }

before(:each) do
FactoryGirl.create_list(:alert, 2)
Expand All @@ -67,7 +71,7 @@
end

context "with class_name" do
let(:uri) { "/api/v6/alerts?class_name=nomethoderror" }
let(:uri) { "/api/alerts?class_name=nomethoderror" }

before(:each) do
FactoryGirl.create_list(:alert, 2)
Expand All @@ -87,7 +91,7 @@
end

context "with level ERROR" do
let(:uri) { "/api/v6/alerts?level=error" }
let(:uri) { "/api/alerts?level=error" }

before(:each) do
FactoryGirl.create_list(:alert, 2)
Expand All @@ -107,7 +111,7 @@
end

context "with query" do
let(:uri) { "/api/v6/alerts?q=nomethod" }
let(:uri) { "/api/alerts?q=nomethod" }

before(:each) do
FactoryGirl.create_list(:alert, 2)
Expand All @@ -127,7 +131,7 @@
end

context "with pagination" do
let(:uri) { "/api/v6/alerts?page=2" }
let(:uri) { "/api/alerts?page=2" }

before(:each) { FactoryGirl.create_list(:alert, 55) }

Expand All @@ -143,7 +147,7 @@

context "as staff user" do
let(:user) { FactoryGirl.create(:user, :role => "staff") }
let(:uri) { "/api/v6/alerts" }
let(:uri) { "/api/alerts" }

it "JSON" do
get uri, nil, headers
Expand All @@ -156,7 +160,7 @@

context "as regular user" do
let(:user) { FactoryGirl.create(:user, :role => "user") }
let(:uri) { "/api/v6/alerts" }
let(:uri) { "/api/alerts" }

it "JSON" do
get uri, nil, headers
Expand Down

0 comments on commit ed5e336

Please sign in to comment.