Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions app/models/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,14 @@ def self.get_datacite_xml(id)
Maremma.from_xml(xml).to_h.fetch("resource", {})
end

def self.get_datacite_json(id, include_client: false)
def self.get_datacite_json(id)
doi = doi_from_url(id)
if doi.blank?
Rails.logger.error "#{id} is not a valid DOI"
return {}
end

url = ENV["API_URL"] + "/dois/#{doi}?affiliation=true#{include_client ? "&include=client" : ""}"
url = ENV["API_URL"] + "/dois/#{doi}?affiliation=true"
response = Maremma.get(url)

if response.status != 200
Expand All @@ -257,9 +257,27 @@ def self.get_datacite_json(id, include_client: false)
end

attributes = (response.body.dig("data", "attributes") || {}).except("xml")
included = response.body.dig("included")

included.present? ? attributes.merge("included" => included) : attributes
relationships = response.body.dig("data", "relationships") || {}

attributes.merge("relationships" => relationships)
end

def self.get_client(id)
url = ENV["API_URL"] + "/clients/#{id}"
response = Maremma.get(url)
return {} if response.status != 200

response.body.dig("data", "attributes") || {}
end

def self.raid_registry_record?(attributes)
client_id = attributes.dig("relationships", "client", "data", "id")
return false if client_id.blank?

client = cached_client(client_id)
return false if client.blank?

client.dig("clientType") == "raidRegistry"
end

def self.get_datacite_metadata(id)
Expand Down
6 changes: 6 additions & 0 deletions app/models/concerns/cacheable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,11 @@ def cached_crossref_member_id(id)
Base.get_crossref_member_id(id)
end
end

def cached_client(id)
Rails.cache.fetch("clients/#{id}", expires_in: 1.day) do
Base.get_client(id)
end
end
end
end
8 changes: 5 additions & 3 deletions app/models/name_identifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def self.import_one(options = {})
return message
end

attributes = get_datacite_json(doi, include_client: true)
attributes = get_datacite_json(doi)
response = push_item({ "id" => doi, "type" => "dois",
"attributes" => attributes })
end
Expand Down Expand Up @@ -72,10 +72,12 @@ def self.push_item(item)
related_identifiers = Array.wrap(attributes.fetch("relatedIdentifiers",
nil))

## Don't process DOIs with certain relationTypes or DOIs in a client with clientType raidRegistry
raid_registry_record = raid_registry_record?(attributes)

## Don't process DOIs with certain relationTypes or DOIs in a raidRegistry
skip_doi = related_identifiers.any? do |related_identifier|
["IsIdenticalTo", "IsPartOf", "IsPreviousVersionOf", "IsVersionOf"].include?(related_identifier["relationType"] || "")
end || item.dig("attributes", "included", 0, "attributes", "clientType") == "raidRegistry"
end || raid_registry_record

creators = attributes.fetch("creators", []).select do |n|
Array.wrap(n.fetch("nameIdentifiers", nil)).any? do |n|
Expand Down

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

70 changes: 70 additions & 0 deletions spec/fixtures/vcr_cassettes/Base/get_client/fetch_client.yml

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

Loading