From 750022bd896989f20cd84192f7f671ac15f1bb4c Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Tue, 7 May 2019 07:10:32 +0200 Subject: [PATCH] fetch meta and errors from event data api. #262 --- app/models/event.rb | 28 ++++++++++----- docker-compose.yml | 17 --------- .../Event/find_by_id/not_found.yml | 21 +++++------ spec/models/event_spec.rb | 36 ++++++++++--------- 4 files changed, 49 insertions(+), 53 deletions(-) diff --git a/app/models/event.rb b/app/models/event.rb index 525c2b8bb..67ee72781 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -1,14 +1,20 @@ class Event def self.find_by_id(id) - return [] unless id.present? + return { errors: [{ "title" => "No id provided"} ] } unless id.present? url = "https://api.datacite.org/events/#{id}" response = Maremma.get(url, accept: "application/vnd.api+json; version=2") - return [] if response.status != 200 + if response.status == 200 + message = response.body.dig("data", "attributes") + data = [parse_message(id: id, message: message)] + else + data = [] + end - message = response.body.dig("data", "attributes") - [parse_message(id: id, message: message)] + { data: data, + meta: response.body.fetch("meta", {}), + errors: response.body.fetch("errors", []) } end def self.query(query, options={}) @@ -20,12 +26,16 @@ def self.query(query, options={}) response = Maremma.get(url, accept: "application/vnd.api+json; version=2") - return [] if response.status != 200 - - items = response.body.fetch("data", []) - items.map do |message| - parse_message(id: message['id'], message: message['attributes']) + if response.status == 200 + items = response.body.fetch("data", []) + data = items.map { |message| parse_message(id: message['id'], message: message['attributes']) } + else + data = [] end + + { data: data, + meta: response.body.fetch("meta", {}), + errors: response.body.fetch("errors", []) } end def self.parse_message(id: nil, message: nil) diff --git a/docker-compose.yml b/docker-compose.yml index 491aa4d34..a7a991c32 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -22,23 +22,6 @@ services: depends_on: elasticsearch: condition: service_healthy - lagottino: - env_file: .env - environment: - - ELASTIC_PASSWORD=changeme - image: datacite/lagottino - ports: - - "8085:80" - - "2285:22" - networks: - - public - depends_on: - elasticsearch: - condition: service_healthy - memcached: - image: memcached:1.4.31 - networks: - - public mysql: command: --max_allowed_packet=50000000 environment: diff --git a/spec/fixtures/vcr_cassettes/Event/find_by_id/not_found.yml b/spec/fixtures/vcr_cassettes/Event/find_by_id/not_found.yml index a6cf9fe5f..931da579d 100644 --- a/spec/fixtures/vcr_cassettes/Event/find_by_id/not_found.yml +++ b/spec/fixtures/vcr_cassettes/Event/find_by_id/not_found.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: get - uri: https://api.datacite.org/events/https://doi.org/10.13039/xxx + uri: https://api.datacite.org/events/xxx body: encoding: US-ASCII string: '' @@ -17,28 +17,29 @@ http_interactions: message: Not Found headers: Date: - - Mon, 06 May 2019 20:55:35 GMT + - Tue, 07 May 2019 05:09:23 GMT Content-Type: - - application/json; charset=UTF-8 - Content-Length: - - '34' + - application/json; charset=utf-8 Connection: - keep-alive Status: - 404 Not Found + Cache-Control: + - no-cache Vary: - - Origin + - Accept-Encoding, Origin X-Runtime: - - '0.001265' + - '0.059691' X-Request-Id: - - 73761d8a-4945-45ee-b8d5-021e5596c1a4 + - 6ac28d34-dc1e-451f-9c7a-26a682b83a47 X-Powered-By: - Phusion Passenger 6.0.2 Server: - nginx/1.15.8 + Phusion Passenger 6.0.2 body: encoding: ASCII-8BIT - string: '{"status":404,"error":"Not Found"}' + string: '{"errors":[{"status":"404","title":"The resource you are looking for + doesn''t exist."}]}' http_version: - recorded_at: Mon, 06 May 2019 20:55:35 GMT + recorded_at: Tue, 07 May 2019 05:09:23 GMT recorded_with: VCR 3.0.3 diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb index 62c50e932..714862662 100644 --- a/spec/models/event_spec.rb +++ b/spec/models/event_spec.rb @@ -4,44 +4,46 @@ describe "find_by_id" do it "found" do id = "03f5743e-089e-4163-a406-f6529e81e422" - events = Event.find_by_id(id) - expect(events.size).to eq(1) - expect(events.first).to eq(:id=>"03f5743e-089e-4163-a406-f6529e81e422", :subj_id=>"https://api.datacite.org/reports/21fd2e8e-5481-4bbd-b2ef-742d8b270a66", :obj_id=>"https://doi.org/10.7272/q6z60kzd", :source_id=>"datacite-usage", :relation_type_id=>"total-dataset-investigations-regular", :total=>23164) + response = Event.find_by_id(id) + expect(response[:data].size).to eq(1) + expect(response[:data].first).to eq(:id=>"03f5743e-089e-4163-a406-f6529e81e422", :subj_id=>"https://api.datacite.org/reports/21fd2e8e-5481-4bbd-b2ef-742d8b270a66", :obj_id=>"https://doi.org/10.7272/q6z60kzd", :source_id=>"datacite-usage", :relation_type_id=>"total-dataset-investigations-regular", :total=>23164) end it "not found" do - id = "https://doi.org/10.13039/xxx" - event = Event.find_by_id(id) - expect(event).to be_empty + id = "xxx" + response = Event.find_by_id(id) + expect(response[:data]).to be_empty + expect(response[:errors]).to eq([{"status"=>404, "title"=>"Not found"}]) end end describe "query" do it "all" do query = nil - events = Event.query(query) - expect(events.size).to eq(100) - expect(events.first).to eq(:id=>"c8bcd46c-3433-47ac-b8db-d039ce346d65", :subj_id=>"https://doi.org/10.5281/zenodo.595698", :obj_id=>"https://doi.org/10.13039/501100000780", :source_id=>"datacite-funder", :relation_type_id=>"is-funded-by", :total=>1) + response = Event.query(query) + expect(response[:data].size).to eq(100) + expect(response[:data].first).to eq(:id=>"c8bcd46c-3433-47ac-b8db-d039ce346d65", :subj_id=>"https://doi.org/10.5281/zenodo.595698", :obj_id=>"https://doi.org/10.13039/501100000780", :source_id=>"datacite-funder", :relation_type_id=>"is-funded-by", :total=>1) end it "limit" do query = nil - events = Event.query(query, limit: 10) - expect(events.size).to eq(10) - expect(events.first).to eq(:id=>"c8bcd46c-3433-47ac-b8db-d039ce346d65", :subj_id=>"https://doi.org/10.5281/zenodo.595698", :obj_id=>"https://doi.org/10.13039/501100000780", :source_id=>"datacite-funder", :relation_type_id=>"is-funded-by", :total=>1) + response = Event.query(query, limit: 10) + expect(response[:data].size).to eq(10) + expect(response[:data].first).to eq(:id=>"c8bcd46c-3433-47ac-b8db-d039ce346d65", :subj_id=>"https://doi.org/10.5281/zenodo.595698", :obj_id=>"https://doi.org/10.13039/501100000780", :source_id=>"datacite-funder", :relation_type_id=>"is-funded-by", :total=>1) end it "source_id" do source_id = "datacite-usage" - events = Event.query(nil, source_id: source_id) - expect(events.size).to eq(100) - expect(events.first).to eq(:id=>"308185f3-1607-478b-a25e-ed5671994db5", :subj_id=>"https://api.datacite.org/reports/fa2ad308-1b25-4394-9bc6-e0c7511e763d", :obj_id=>"https://doi.org/10.7272/q6g15xs4", :source_id=>"datacite-usage", :relation_type_id=>"total-dataset-investigations-regular", :total=>4) + response = Event.query(nil, source_id: source_id) + expect(response[:data].size).to eq(100) + expect(response[:data].first).to eq(:id=>"308185f3-1607-478b-a25e-ed5671994db5", :subj_id=>"https://api.datacite.org/reports/fa2ad308-1b25-4394-9bc6-e0c7511e763d", :obj_id=>"https://doi.org/10.7272/q6g15xs4", :source_id=>"datacite-usage", :relation_type_id=>"total-dataset-investigations-regular", :total=>4) end it "not found" do source_id = "xxx" - events = Event.query(nil, source_id: source_id) - expect(events).to be_empty + response = Event.query(nil, source_id: source_id) + expect(response[:data]).to be_empty + expect(response[:meta]).to eq("page"=>1, "total"=>0, "totalPages"=>0) end end end \ No newline at end of file