Module: OrcidClient::Metadata

Included in:
Notification, Work
Defined in:
lib/orcid_client/metadata.rb

Instance Method Summary collapse

Instance Method Details

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/orcid_client/metadata.rb', line 13

def (doi, options = {})
  return {} if doi.blank?

  url = "http://api.crossref.org/works/" + PostRank::URI.escape(doi)
  response = Maremma.get(url, options.merge(host: true))
  return response if response.body["errors"]

   = response.body.fetch("data", {}).fetch("message", {})
  return { "errors" => [{ "title" => "Not found.", "status" => 404 }] } if .blank?

  date_parts = .fetch("issued", {}).fetch("date-parts", []).first
  year, month, day = date_parts[0], date_parts[1], date_parts[2]

  # use date indexed if date issued is in the future
  if year.nil? || Date.new(*date_parts) > Time.zone.now.to_date
    date_parts = .fetch("indexed", {}).fetch("date-parts", []).first
    year, month, day = date_parts[0], date_parts[1], date_parts[2]
  end
  ["issued"] = { "date-parts" => [date_parts] }

  ["title"] = case ["title"].length
        when 0 then nil
        when 1 then ["title"][0]
        else ["title"][0].presence || ["title"][1]
        end

  if ["title"].blank? && !TYPES_WITH_TITLE.include?(["type"])
    ["title"] = ["container-title"][0].presence || "No title"
  end

  ["container-title"] = .fetch("container-title", [])[0]
  ["type"] = CROSSREF_TYPE_TRANSLATIONS[["type"]] if ["type"]
  ["author"] = ["author"].map { |author| author.except("affiliation") }

  
end

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/orcid_client/metadata.rb', line 50

def (doi, options = {})
  return {} if doi.blank?

  params = { q: "doi:" + doi,
             rows: 1,
             fl: "doi,creator,title,publisher,publicationYear,resourceTypeGeneral,description,datacentre,datacentre_symbol,prefix,relatedIdentifier,xml,minted,updated",
             wt: "json" }
  url = "http://search.datacite.org/api?" + URI.encode_www_form(params)
  response = Maremma.get(url, options)
  return response if response.body["errors"]

   = response.body.fetch("data", {}).fetch("response", {}).fetch("docs", []).first
  return { "errors" => [{ "title" => "Not found.", "status" => 404 }] } if .blank?

  doi = .fetch("doi", nil)
  doi = doi.upcase if doi.present?
  title = .fetch("title", []).first
  title = title.chomp(".") if title.present?

  xml = Base64.decode64(.fetch('xml', "PGhzaD48L2hzaD4=\n"))
  xml = Hash.from_xml(xml).fetch("resource", {})
  authors = xml.fetch("creators", {}).fetch("creator", [])
  authors = [authors] if authors.is_a?(Hash)

  { "author" => get_hashed_authors(authors),
    "title" => title,
    "container-title" => .fetch("publisher", nil),
    "description" => .fetch("description", nil),
    "published" => .fetch("publicationYear", nil),
    "issued" => .fetch("minted", nil),
    "DOI" => doi,
    "type" => .fetch("resourceTypeGeneral", nil),
    "subtype" => .fetch("resourceType", nil),
    "publisher_id" => .fetch("datacentre_symbol", nil) }
end

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/orcid_client/metadata.rb', line 113

def get_doi_ra(doi, options = {})
  return {} if doi.blank?

  url = "http://doi.crossref.org/doiRA/" + CGI.unescape(doi)
  response = Maremma.get(url, options.merge(host: true))
  return response if response.body["errors"]

  ra = response.body.fetch("data", {}).first.fetch("RA", nil)
  if ra.present?
    ra.delete(' ').downcase
  else
    { "errors" => [{ "title" => "An error occured", "status" => 400 }] }
  end
end

#get_metadata(id, service, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/orcid_client/metadata.rb', line 3

def (id, service, options = {})
  case service
  when "crossref" then (id, options = {})
  when "datacite" then (id, options = {})
  when "orcid" then (id, options = {})
  else
    { "errors" => [{ "title" => 'Not found.', "status" => 404 }] }
  end
end

#get_orcid_metadata(orcid, options = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/orcid_client/metadata.rb', line 86

def (orcid, options = {})
  return {} if orcid.blank?

  url = "http://pub.orcid.org/v1.2/#{orcid}/orcid-bio"
  response = Maremma.get(url, options)
  return response if response.body["errors"]

   = response.body.fetch("data", {}).fetch("orcid_message", {}).fetch("orcid_profile", nil)
  .extend Hashie::Extensions::DeepFetch

  personal_details = .deep_fetch("orcid_bio", "personal_details") { {} }
  personal_details.extend Hashie::Extensions::DeepFetch

  author = { "family" => personal_details.fetch("family_name", nil),
             "given" => personal_details.fetch("given_names", nil) }
  url = .deep_fetch("orcid_identifier", "uri") { nil }
  timestamp = Time.zone.now.utc.iso8601

  { "author" => [author],
    "title" => "ORCID record for #{author.fetch('given', '')} #{author.fetch('family', '')}",
    "container-title" => "ORCID Registry",
    "issued" => get_date_parts(timestamp),
    "timestamp" => timestamp,
    "URL" => url,
    "type" => 'entry' }
end