Skip to content

Commit 3502e77

Browse files
Merge pull request #229 from datacite/raid-orcid-skip-fix
Get client when processing nameIdentifiers and skip pushes if DOI is in a raidRegistry
2 parents f635824 + 5bd9aed commit 3502e77

9 files changed

Lines changed: 481 additions & 27 deletions

File tree

app/models/base.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,14 @@ def self.get_datacite_xml(id)
241241
Maremma.from_xml(xml).to_h.fetch("resource", {})
242242
end
243243

244-
def self.get_datacite_json(id, include_client: false)
244+
def self.get_datacite_json(id)
245245
doi = doi_from_url(id)
246246
if doi.blank?
247247
Rails.logger.error "#{id} is not a valid DOI"
248248
return {}
249249
end
250250

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

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

259259
attributes = (response.body.dig("data", "attributes") || {}).except("xml")
260-
included = response.body.dig("included")
261-
262-
included.present? ? attributes.merge("included" => included) : attributes
260+
relationships = response.body.dig("data", "relationships") || {}
261+
262+
attributes.merge("relationships" => relationships)
263+
end
264+
265+
def self.get_client(id)
266+
url = ENV["API_URL"] + "/clients/#{id}"
267+
response = Maremma.get(url)
268+
return {} if response.status != 200
269+
270+
response.body.dig("data", "attributes") || {}
271+
end
272+
273+
def self.raid_registry_record?(attributes)
274+
client_id = attributes.dig("relationships", "client", "data", "id")
275+
return false if client_id.blank?
276+
277+
client = cached_client(client_id)
278+
return false if client.blank?
279+
280+
client.dig("clientType") == "raidRegistry"
263281
end
264282

265283
def self.get_datacite_metadata(id)

app/models/concerns/cacheable.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,11 @@ def cached_crossref_member_id(id)
4444
Base.get_crossref_member_id(id)
4545
end
4646
end
47+
48+
def cached_client(id)
49+
Rails.cache.fetch("clients/#{id}", expires_in: 1.day) do
50+
Base.get_client(id)
51+
end
52+
end
4753
end
4854
end

app/models/name_identifier.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def self.import_one(options = {})
3434
return message
3535
end
3636

37-
attributes = get_datacite_json(doi, include_client: true)
37+
attributes = get_datacite_json(doi)
3838
response = push_item({ "id" => doi, "type" => "dois",
3939
"attributes" => attributes })
4040
end
@@ -72,10 +72,12 @@ def self.push_item(item)
7272
related_identifiers = Array.wrap(attributes.fetch("relatedIdentifiers",
7373
nil))
7474

75-
## Don't process DOIs with certain relationTypes or DOIs in a client with clientType raidRegistry
75+
raid_registry_record = raid_registry_record?(attributes)
76+
77+
## Don't process DOIs with certain relationTypes or DOIs in a raidRegistry
7678
skip_doi = related_identifiers.any? do |related_identifier|
7779
["IsIdenticalTo", "IsPartOf", "IsPreviousVersionOf", "IsVersionOf"].include?(related_identifier["relationType"] || "")
78-
end || item.dig("attributes", "included", 0, "attributes", "clientType") == "raidRegistry"
80+
end || raid_registry_record
7981

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

spec/fixtures/vcr_cassettes/Base/get_client/fetch_cached_client.yml

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/Base/get_client/fetch_client.yml

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)