Skip to content

Commit

Permalink
Merge pull request #1180 from datacite/fix-prefix-500-bug
Browse files Browse the repository at this point in the history
fix prefix 500 status error
  • Loading branch information
wendelfabianchinsamy authored Apr 19, 2024
2 parents c861f12 + e00fa9e commit 89d22aa
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions app/models/handle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# frozen_string_literal: true

class Handle
include Searchable

attr_reader :id,
:prefix,
:registration_agency,
:clients,
:providers,
:created,
:cache_key,
:updated

RA_HANDLES = {
"10.SERV/CROSSREF" => "Crossref",
"10.SERV/DEFAULT" => "Crossref",
"10.SERV/DATACITE" => "DataCite",
"10.SERV/ETH" => "DataCite",
"10.SERV/EIDR" => "EIDR",
"10.SERV/KISTI" => "KISTI",
"10.SERV/MEDRA" => "mEDRA",
"10.SERV/ISTIC" => "ISTIC",
"10.SERV/JALC" => "JaLC",
"10.SERV/AIRITI" => "Airiti",
"10.SERV/CNKI" => "CNKI",
"10.SERV/OP" => "OP",
}.freeze

def initialize(attributes, _options = {})
@id = attributes.fetch("id").underscore.dasherize
@prefix = @id
@registration_agency = attributes.fetch("registration_agency", nil)
@updated = attributes.fetch("updated", nil)
end

def cache_key
"handles/#{id}-#{updated}"
end

def client_ids
[]
end

def provider_ids
[]
end

def self.get_query_url(options = {})
options[:id].present? ? "#{url}/#{options[:id]}" : url
end

def self.parse_data(result, options = {})
return nil if result.blank? || result["errors"]

if options[:id]
response_code = result.body.dig("data", "responseCode")
return nil unless response_code == 1

record =
result.body.fetch("data", {}).fetch("values", []).detect do |hs|
hs["type"] == "HS_SERV"
end

fail ActiveRecord::RecordNotFound if record.blank?

ra = record.dig("data", "value")

item = {
"id" => result.body.dig("data", "handle"),
"registration_agency" => RA_HANDLES[ra] || ra || "unknown",
"updated" => record.fetch("timestamp", nil),
}

parse_item(item)
end
end

def self.url
"https://doi.org/api/handles"
end
end

0 comments on commit 89d22aa

Please sign in to comment.