Skip to content

Commit

Permalink
csv in content negotiation. datacite/bolognese#53
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jan 12, 2019
1 parent c2d4769 commit 8b09d59
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 9 deletions.
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ GEM
api-pagination (4.8.2)
arel (9.0.0)
aws-eventstream (1.0.1)
aws-partitions (1.130.0)
aws-partitions (1.131.0)
aws-sdk-core (3.44.2)
aws-eventstream (~> 1.0)
aws-partitions (~> 1.0)
Expand Down Expand Up @@ -93,7 +93,7 @@ GEM
latex-decode (~> 0.0)
binding_of_caller (0.8.0)
debug_inspector (>= 0.0.1)
bolognese (1.0.36)
bolognese (1.1.1)
activesupport (>= 4.2.5, < 6)
benchmark_methods (~> 0.7)
bibtex-ruby (~> 4.1)
Expand Down Expand Up @@ -223,7 +223,7 @@ GEM
flipper (~> 0.16.0)
gender_detector (0.1.2)
unicode_utils (>= 1.3.0)
globalid (0.4.1)
globalid (0.4.2)
activesupport (>= 4.2.0)
haml (5.0.4)
temple (>= 0.8.0)
Expand Down Expand Up @@ -554,4 +554,4 @@ DEPENDENCIES
webmock (~> 3.1)

BUNDLED WITH
1.17.2
2.0.1
4 changes: 2 additions & 2 deletions app/controllers/dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def index
# fetch formatted citations
render citation: response.records.to_a, style: params[:style] || "apa", locale: params[:locale] || "en-US"
end
format.any(:bibtex, :citeproc, :codemeta, :crosscite, :datacite, :datacite_json, :jats, :ris, :schema_org) { render request.format.to_sym => response.records.to_a }
format.any(:bibtex, :citeproc, :codemeta, :crosscite, :datacite, :datacite_json, :jats, :ris, :csv, :schema_org) { render request.format.to_sym => response.records.to_a }
end
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => exception
Bugsnag.notify(exception)
Expand Down Expand Up @@ -252,7 +252,7 @@ def show
# fetch formatted citation
render citation: @doi, style: params[:style] || "apa", locale: params[:locale] || "en-US"
end
format.any(:bibtex, :citeproc, :codemeta, :crosscite, :datacite, :datacite_json, :jats, :ris, :schema_org) { render request.format.to_sym => @doi }
format.any(:bibtex, :citeproc, :codemeta, :crosscite, :datacite, :datacite_json, :jats, :ris, :csv, :schema_org) { render request.format.to_sym => @doi }
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ def resource_type
end

def date_registered
minted
minted.iso8601 if minted.present?
end

def date_updated
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/_version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Lupo
class Application
VERSION = "2.3.7"
VERSION = "2.3.8"
end
end
6 changes: 6 additions & 0 deletions config/initializers/mime_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Mime::Type.register "text/html", :html, %w( application/xhtml+xml ), %w( xhtml )
Mime::Type.register "text/plain", :text, [], %w(txt)
Mime::Type.register "application/json", :json, %w( text/x-json application/jsonrequest application/vnd.api+json )
Mime::Type.register "text/csv", :csv

# Mime types supported by bolognese gem https://github.com/datacite/bolognese
Mime::Type.register "application/vnd.crossref.unixref+xml", :crossref
Expand Down Expand Up @@ -66,3 +67,8 @@
ActionController::Renderers.add :ris do |obj, options|
Array.wrap(obj).map { |o| o.send("ris") }.join("\n\n")
end

ActionController::Renderers.add :csv do |obj, options|
%w(doi url year registered state resourceTypeGeneral bibtexType title author publisher journal volume issue pages).to_csv +
Array.wrap(obj).map { |o| o.send("csv") }.join("\n")
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
get '/dois/application/vnd.jats+xml/:id', :to => 'dois#show', constraints: { :id => /.+/ }, defaults: { format: :jats }
get '/dois/application/x-bibtex/:id', :to => 'dois#show', constraints: { :id => /.+/ }, defaults: { format: :bibtex }
get '/dois/application/x-research-info-systems/:id', :to => 'dois#show', constraints: { :id => /.+/ }, defaults: { format: :ris }
get '/dois/text/csv/:id', :to => 'dois#show', constraints: { :id => /.+/ }, defaults: { format: :csv }
get '/dois/text/x-bibliography/:id', :to => 'dois#show', constraints: { :id => /.+/ }, defaults: { format: :citation }

# content negotiation for collections
Expand All @@ -29,6 +30,7 @@
get '/dois/application/vnd.jats+xml', :to => 'dois#index', defaults: { format: :jats }
get '/dois/application/x-bibtex', :to => 'dois#index', defaults: { format: :bibtex }
get '/dois/application/x-research-info-systems', :to => 'dois#index', defaults: { format: :ris }
get '/dois/text/csv', :to => 'dois#index', defaults: { format: :csv }
get '/dois/text/x-bibliography', :to => 'dois#index', defaults: { format: :citation }

# manage DOIs
Expand Down
26 changes: 25 additions & 1 deletion spec/requests/dois_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2513,6 +2513,30 @@
expect(response).to have_http_status(200)
end
end

context "text/csv" do
before { get "/dois/#{doi.doi}", headers: { "HTTP_ACCEPT" => "text/csv", 'Authorization' => 'Bearer ' + bearer } }

it 'returns the Doi' do
expect(response.body).to include(doi.doi)
end

it 'returns status code 200' do
expect(response).to have_http_status(200)
end
end

context "text/csv link" do
before { get "/dois/text/csv/#{doi.doi}" }

it 'returns the Doi' do
expect(response.body).to include(doi.doi)
end

it 'returns status code 200' do
expect(response).to have_http_status(200)
end
end

context "text/x-bibliography", vcr: true do
context "default style" do
Expand Down Expand Up @@ -2577,7 +2601,7 @@
end

context "unknown content type" do
before { get "/dois/#{doi.doi}", headers: { "HTTP_ACCEPT" => "text/csv", 'Authorization' => 'Bearer ' + bearer } }
before { get "/dois/#{doi.doi}", headers: { "HTTP_ACCEPT" => "application/vnd.ms-excel", 'Authorization' => 'Bearer ' + bearer } }

it 'returns the Doi' do
expect(json["errors"]).to eq([{"status"=>"406", "title"=>"The content type is not recognized."}])
Expand Down

0 comments on commit 8b09d59

Please sign in to comment.