Skip to content

Commit

Permalink
fetch meta and errors from event data api. #262
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 7, 2019
1 parent faeecd7 commit 750022b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 53 deletions.
28 changes: 19 additions & 9 deletions app/models/event.rb
Original file line number Diff line number Diff line change
@@ -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={})
Expand All @@ -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)
Expand Down
17 changes: 0 additions & 17 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 11 additions & 10 deletions spec/fixtures/vcr_cassettes/Event/find_by_id/not_found.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 19 additions & 17 deletions spec/models/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 750022b

Please sign in to comment.