From 8b09d591078cdb5b33bbc29ec234dc876a5fdea8 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sat, 12 Jan 2019 11:02:55 +0100 Subject: [PATCH] csv in content negotiation. datacite/bolognese#53 --- Gemfile.lock | 8 ++++---- app/controllers/dois_controller.rb | 4 ++-- app/models/doi.rb | 2 +- config/initializers/_version.rb | 2 +- config/initializers/mime_types.rb | 6 ++++++ config/routes.rb | 2 ++ spec/requests/dois_spec.rb | 26 +++++++++++++++++++++++++- 7 files changed, 41 insertions(+), 9 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index c7a2ee9d1..d6f3cca8c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -554,4 +554,4 @@ DEPENDENCIES webmock (~> 3.1) BUNDLED WITH - 1.17.2 + 2.0.1 diff --git a/app/controllers/dois_controller.rb b/app/controllers/dois_controller.rb index af134a7d7..66dfb0745 100644 --- a/app/controllers/dois_controller.rb +++ b/app/controllers/dois_controller.rb @@ -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) @@ -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 diff --git a/app/models/doi.rb b/app/models/doi.rb index 75fa7c4a9..03b1761ea 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -716,7 +716,7 @@ def resource_type end def date_registered - minted + minted.iso8601 if minted.present? end def date_updated diff --git a/config/initializers/_version.rb b/config/initializers/_version.rb index b7b9ff914..3bf060700 100644 --- a/config/initializers/_version.rb +++ b/config/initializers/_version.rb @@ -1,5 +1,5 @@ module Lupo class Application - VERSION = "2.3.7" + VERSION = "2.3.8" end end \ No newline at end of file diff --git a/config/initializers/mime_types.rb b/config/initializers/mime_types.rb index f89abdec4..a6d3d0ab0 100644 --- a/config/initializers/mime_types.rb +++ b/config/initializers/mime_types.rb @@ -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 @@ -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 diff --git a/config/routes.rb b/config/routes.rb index ac8e2c787..8f8887b5f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 @@ -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 diff --git a/spec/requests/dois_spec.rb b/spec/requests/dois_spec.rb index ac72c056b..633454f95 100644 --- a/spec/requests/dois_spec.rb +++ b/spec/requests/dois_spec.rb @@ -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 @@ -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."}])