diff --git a/app/models/doi.rb b/app/models/doi.rb index 1fd62c31f..a3226a8dc 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -1055,13 +1055,17 @@ def uid end def resource_type_id - types["resourceTypeGeneral"].underscore.dasherize if types.to_h["resourceTypeGeneral"].present? + r = types.to_h["resourceTypeGeneral"] + r.underscore.dasherize if RESOURCE_TYPES_GENERAL[r].present? rescue TypeError nil end def resource_type_id_and_name - "#{resource_type_id}:#{types["resourceTypeGeneral"].titleize}" if types.to_h["resourceTypeGeneral"].present? + r = types.to_h["resourceTypeGeneral"] + "#{r.underscore.dasherize}:#{RESOURCE_TYPES_GENERAL[r]}" if RESOURCE_TYPES_GENERAL[r].present? + rescue TypeError + nil end def media_ids diff --git a/config/initializers/constants.rb b/config/initializers/constants.rb index e6439a213..c8c8bcf62 100644 --- a/config/initializers/constants.rb +++ b/config/initializers/constants.rb @@ -49,6 +49,24 @@ class IdentifierError < RuntimeError; end "AMER" => "Americas" } +RESOURCE_TYPES_GENERAL = { + "Audiovisual" => "Audiovisual", + "Collection" => "Collection", + "Dataset" => "Dataset", + "DataPaper" => "Data Paper", + "Event" => "Event", + "Image" => "Image", + "InteractiveResource" => "Interactive Resource", + "Model" => "Model", + "PhysicalObject" => "Physical Object", + "Service" => "Service", + "Sound" => "Sound", + "Software" => "Software", + "Text" => "Text", + "Workflow" => "Workflow", + "Other" => "Other" +} + LAST_SCHEMA_VERSION = "http://datacite.org/schema/kernel-4" METADATA_FORMATS = [ diff --git a/spec/requests/dois_spec.rb b/spec/requests/dois_spec.rb index fa573f259..85b1fdf93 100644 --- a/spec/requests/dois_spec.rb +++ b/spec/requests/dois_spec.rb @@ -285,7 +285,7 @@ sleep 3 end - it 'filter for instruments' do + it 'filter for interactive resources' do get "/dois?resource-type-id=interactive-resource", nil, headers expect(last_response.status).to eq(200) @@ -296,6 +296,26 @@ expect(json.dig('meta', 'resourceTypes')).to eq([{"count"=>3, "id"=>"interactive-resource", "title"=>"Interactive Resource"}]) end end + + describe 'GET /dois for fake resources', elasticsearch: true, vcr: true do + let!(:dois) { create_list(:doi, 3, types: { "resourceTypeGeneral" => "Fake", "resourceType" => "Presentation" }, client: client) } + + before do + Doi.import + sleep 3 + end + + it 'filter for fake resources' do + get "/dois?resource-type-id=fake", nil, headers + + expect(last_response.status).to eq(200) + expect(json['data'].size).to eq(3) + expect(json.dig('meta', 'total')).to eq(3) + expect(json.dig('data', 0, 'attributes', 'publicationYear')).to eq(2011) + expect(json.dig('data', 0, 'attributes', 'types')).to eq("resourceType"=>"Presentation", "resourceTypeGeneral"=>"Fake") + expect(json.dig('meta', 'resourceTypes')).to eq([]) + end + end describe 'GET /dois with views and downloads', elasticsearch: true, vcr: true do let(:doi) { create(:doi, client: client, aasm_state: "findable") }