Module: Commonmeta::DoiUtils

Included in:
CLI, MetadataUtils
Defined in:
lib/commonmeta/doi_utils.rb

Instance Method Summary collapse

Instance Method Details

#crossref_api_url(doi, _options = {}) ⇒ Object



35
36
37
# File 'lib/commonmeta/doi_utils.rb', line 35

def crossref_api_url(doi, _options = {})
  "https://api.crossref.org/works/#{doi_from_url(doi)}"
end

#datacite_api_url(doi, options = {}) ⇒ Object



30
31
32
33
# File 'lib/commonmeta/doi_utils.rb', line 30

def datacite_api_url(doi, options = {})
  sandbox = Array(/handle\.stage\.datacite.\org/.match(doi)).last
  sandbox.present? || options[:sandbox] ? "https://api.stage.datacite.org/dois/#{doi_from_url(doi)}?include=media,client" : "https://api.datacite.org/dois/#{doi_from_url(doi)}?include=media,client"
end

#doi_as_url(doi) ⇒ Object



56
57
58
# File 'lib/commonmeta/doi_utils.rb', line 56

def doi_as_url(doi)
  "https://doi.org/#{doi}" if doi.present?
end

#doi_from_url(url) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/commonmeta/doi_utils.rb', line 47

def doi_from_url(url)
  unless %r{\A(?:(http|https)://(dx\.)?(doi\.org|handle\.stage\.datacite\.org|handle\.test\.datacite\.org)/)?(doi:)?(10\.\d{4,5}/.+)\z}.match?(url)
    return
  end

  uri = Addressable::URI.parse(url)
  uri.path.gsub(%r{^/}, '').downcase
end

#doi_resolver(doi, options = {}) ⇒ Object



25
26
27
28
# File 'lib/commonmeta/doi_utils.rb', line 25

def doi_resolver(doi, options = {})
  sandbox = Array(/handle\.stage\.datacite\.org/.match(doi)).last
  sandbox.present? || options[:sandbox] ? 'https://handle.stage.datacite.org/' : 'https://doi.org/'
end

#get_crossref_member(member_id) ⇒ Object

get Crossref member name from id



72
73
74
75
76
77
78
79
# File 'lib/commonmeta/doi_utils.rb', line 72

def get_crossref_member(member_id)
  url = "https://api.crossref.org/members/#{member_id}"
  response = HTTP.get(url)
  body = JSON.parse(response.body)
  name = body.dig('message', 'primary-name')

  { 'id' => "https://api.crossref.org/members/#{member_id}", 'name' => name }
end

#get_doi_ra(doi) ⇒ Object

get DOI registration agency



61
62
63
64
65
66
67
68
69
# File 'lib/commonmeta/doi_utils.rb', line 61

def get_doi_ra(doi)
  prefix = validate_prefix(doi)
  return nil if prefix.blank?

  url = "https://doi.org/ra/#{prefix}"
  response = HTTP.get(url)
  body = JSON.parse(response.body)
  body.dig(0, 'RA')
end

#normalize_doi(doi, options = {}) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/commonmeta/doi_utils.rb', line 39

def normalize_doi(doi, options = {})
  doi_str = validate_doi(doi)
  return nil unless doi_str.present?

  # turn DOI into URL, escape unsafe characters
  doi_resolver(doi, options) + Addressable::URI.encode(doi_str)
end

#validate_doi(doi) ⇒ Object



5
6
7
8
9
# File 'lib/commonmeta/doi_utils.rb', line 5

def validate_doi(doi)
  doi = Array(%r{\A(?:(http|https):/(/)?(dx\.)?(doi\.org|handle\.stage\.datacite\.org|handle\.test\.datacite\.org)/)?(doi:)?(10\.\d{4,5}/.+)\z}.match(doi)).last
  # remove non-printing whitespace and downcase
  doi.delete("\u200B").downcase if doi.present?
end

#validate_funder_doi(doi) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/commonmeta/doi_utils.rb', line 11

def validate_funder_doi(doi)
  doi = Array(%r{\A(?:(http|https):/(/)?(dx\.)?(doi\.org|handle\.stage\.datacite\.org|handle\.test\.datacite\.org)/)?(doi:)?(10\.13039/)?([1-9]\d+)\z}.match(doi)).last

  # remove non-printing whitespace and downcase
  return unless doi.present?

  doi.delete("\u200B").downcase
  "https://doi.org/10.13039/#{doi}"
end

#validate_prefix(doi) ⇒ Object



21
22
23
# File 'lib/commonmeta/doi_utils.rb', line 21

def validate_prefix(doi)
  Array(%r{\A(?:(http|https):/(/)?(dx\.)?(doi\.org|handle\.stage\.datacite\.org|handle\.test\.datacite\.org)/)?(doi:)?(10\.\d{4,5}).*\z}.match(doi)).last
end