Module: Bolognese::Readers::CrossrefReader

Included in:
MetadataUtils
Defined in:
lib/bolognese/readers/crossref_reader.rb

Instance Method Summary collapse

Instance Method Details

#crossref_alternate_identifiers(bibliographic_metadata) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/bolognese/readers/crossref_reader.rb', line 173

def crossref_alternate_identifiers()
  if .dig("publisher_item", "item_number").present?
    { "identifier" => parse_attributes(.dig("publisher_item", "item_number")),
      "identifierType" => "Publisher ID" }
  elsif parse_attributes(.fetch("item_number", nil)).present?
    { "identifier" => parse_attributes(.fetch("item_number", nil)),
      "identifierType" => "Publisher ID" }
  elsif parse_attributes(.fetch("isbn", nil)).present?
    { "identifier" => parse_attributes(.fetch("isbn", nil), first: true),
      "identifierType" => "ISBN" }
  end
end

#crossref_date_published(bibliographic_metadata) ⇒ Object



265
266
267
268
269
270
271
272
273
# File 'lib/bolognese/readers/crossref_reader.rb', line 265

def crossref_date_published()
  pub_date = Array.wrap(.fetch("publication_date", nil)).presence ||
    Array.wrap(.fetch("acceptance_date", nil))
  if pub_date.present?
    get_date_from_parts(pub_date.first["year"], pub_date.first["month"], pub_date.first["day"])
  else
    nil
  end
end

#crossref_description(bibliographic_metadata) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/bolognese/readers/crossref_reader.rb', line 186

def crossref_description()
  abstract = Array.wrap(.dig("abstract")).map do |r|
    { "descriptionType" => "Abstract", "description" => sanitize(parse_attributes(r, content: 'p')) }.compact
  end

  description = Array.wrap(.dig("description")).map do |r|
    { "descriptionType" => "Other", "description" => sanitize(parse_attributes(r)) }.compact
  end

  (abstract + description)
end

#crossref_funding_reference(program_metadata) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/bolognese/readers/crossref_reader.rb', line 230

def crossref_funding_reference()
  fundref = Array.wrap().find { |a| a["name"] == "fundref" } || {}
  Array.wrap(fundref.fetch("assertion", [])).select { |a| a["name"] == "fundgroup" && a["assertion"].present? }.map do |f|
    funder_identifier = nil
    funder_identifier_type = nil
    funder_name = nil
    award_title = nil
    award_number = nil
    award_uri = nil
    
    Array.wrap(f.fetch("assertion")).each do |a|
      if a.fetch("name") == "award_number"
        award_number = a.fetch("__content__", nil)
        award_uri = a.fetch("awardURI", nil)
      elsif a.fetch("name") == "funder_name"
        funder_name = a.fetch("__content__", nil).to_s.squish.presence
        funder_identifier = validate_funder_doi(a.dig("assertion", "__content__"))
        funder_identifier_type = "Crossref Funder ID" if funder_identifier.present?
      end
    end

    # funder_name is required in DataCite
    if funder_name.present?
      { "funderIdentifier" => funder_identifier,
        "funderIdentifierType" => funder_identifier_type,
        "funderName" => funder_name,
        "awardTitle" => award_title,
        "awardNumber" => award_number,
        "awardUri" => award_uri }.compact
    else
      nil
    end
  end.compact
end

#crossref_is_part_of(model_metadata) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/bolognese/readers/crossref_reader.rb', line 275

def crossref_is_part_of()
  if .present? && .fetch("issn", nil).present?
    { "relatedIdentifier" => normalize_issn(.fetch("issn", nil)),
      "relationType" => "IsPartOf",
      "relatedIdentifierType" => "ISSN",
      "resourceTypeGeneral" => "Collection" }.compact
  elsif .present? && .fetch("relatedIdentifier", nil).present?
    { "relatedIdentifier" => .fetch("relatedIdentifier", nil),
      "relationType" => "IsPartOf",
      "relatedIdentifierType" => "DOI" }.compact
  else
    nil
  end
end

#crossref_license(program_metadata) ⇒ Object



198
199
200
201
202
203
204
205
206
207
# File 'lib/bolognese/readers/crossref_reader.rb', line 198

def crossref_license()
  access_indicator = Array.wrap().find { |m| m["name"] == "AccessIndicators" }
  if access_indicator.present?
    Array.wrap(access_indicator["license_ref"]).map do |license|
      { "rightsUri" => normalize_url(parse_attributes(license)) }
    end.uniq
  else
    []
  end
end

#crossref_people(bibliographic_metadata, contributor_role) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/bolognese/readers/crossref_reader.rb', line 209

def crossref_people(, contributor_role)
  person = .dig("contributors", "person_name")
  person = [{ "name" => ":(unav)", "contributor_role"=>"author" }] if contributor_role == "author" && Array.wrap(person).select { |a| a["contributor_role"] == "author" }.blank?

  Array.wrap(person).select { |a| a["contributor_role"] == contributor_role }.map do |a|
    name_identifiers = normalize_orcid(parse_attributes(a["ORCID"])).present? ? [{ "nameIdentifier" => normalize_orcid(parse_attributes(a["ORCID"])), "nameIdentifierScheme" => "ORCID", "schemeUri"=>"https://orcid.org" }] : nil
    if a["surname"].present? || a["given_name"].present? || name_identifiers.present?
      { "nameType" => "Personal",
        "nameIdentifiers" => name_identifiers,
        "name" => [a["surname"], a["given_name"]].join(", "),
        "givenName" => a["given_name"],
        "familyName" => a["surname"],
        "affiliation" => Array.wrap(a["affiliation"]).map { |a| { "name" => a }}.presence,
        "contributorType" => contributor_role == "editor" ? "Editor" : nil }.compact
    else
      { "nameType" => "Organizational",
        "name" => a["name"] }
    end
  end.unwrap
end

#crossref_references(bibliographic_metadata) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/bolognese/readers/crossref_reader.rb', line 290

def crossref_references()
  refs = .dig("citation_list", "citation")
  Array.wrap(refs).select { |a| a["doi"].present? }.map do |c|
    if c["doi"].present?
      { "relatedIdentifier" => parse_attributes(c["doi"]).downcase,
        "relationType" => "References",
        "relatedIdentifierType" => "DOI" }.compact
    else
      nil
    end
  end.compact.unwrap
end

#get_crossref(id: nil, **options) ⇒ Object

CrossRef types from api.crossref.org/types



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/bolognese/readers/crossref_reader.rb', line 7

def get_crossref(id: nil, **options)
  return { "string" => nil, "state" => "not_found" } unless id.present?

  doi = doi_from_url(id)
  url = "https://api.crossref.org/works/#{doi}/transform/application/vnd.crossref.unixsd+xml"
  response = Maremma.get(url, accept: "text/xml", raw: true)
  string = response.body.fetch("data", nil)
  string = Nokogiri::XML(string, nil, 'UTF-8', &:noblanks).to_s if string.present?

  { "string" => string }
end

#read_crossref(string: nil, **options) ⇒ Object



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
49
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
85
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/bolognese/readers/crossref_reader.rb', line 19

def read_crossref(string: nil, **options)
  read_options = ActiveSupport::HashWithIndifferentAccess.new(options.except(:doi, :id, :url, :sandbox, :validate, :ra))

  if string.present?
    m = Maremma.from_xml(string).dig("crossref_result", "query_result", "body", "query", "doi_record") || {}
    meta = m.dig("doi_record", "crossref", "error").nil? ? m : {}

    # query contains information from outside metadata schema, e.g. publisher name
    query = Maremma.from_xml(string).dig("crossref_result", "query_result", "body", "query") || {}
  else
    meta = {}
    query = {}
  end

  # model should be one of book, conference, database, dissertation, journal, peer_review, posted_content,
  # report-paper, sa_component, standard
  model = meta.dig("crossref").to_h.keys.last

  resource_type = nil
   = {}
   = {}
   = nil
  journal_issue = {}
   = nil
  publisher = query.dig("crm_item", 0)

  case model
  when "book"
     = meta.dig("crossref", "book", "book_metadata")
     = meta.dig("crossref", "book", "book_series_metadata")
     = meta.dig("crossref", "book", "book_set_metadata")
     = meta.dig("crossref", "book", "content_item") ||  || 
    resource_type = .fetch("component_type", nil) ? "book-" + .fetch("component_type") : "book"
    publisher = .present? ? .dig("publisher", "publisher_name") : .dig("publisher", "publisher_name")
  when "conference"
     = meta.dig("crossref", "conference", "event_metadata") || {}
     = meta.dig("crossref", "conference", "conference_paper").to_h
  when "journal"
     = meta.dig("crossref", "journal", "journal_metadata") || {}
    journal_issue = meta.dig("crossref", "journal", "journal_issue") || {}
    journal_article = meta.dig("crossref", "journal", "journal_article") || {}
     = journal_article.presence || journal_issue.presence || 
     = .dig("crossmark", "custom_metadata", "program") || .dig("program")
    
    resource_type = if journal_article.present?
                        "journal_article"
                      elsif journal_issue.present?
                        "journal_issue"
                      else
                        "journal"
                      end
  when "posted_content"
     = meta.dig("crossref", "posted_content").to_h
    publisher = .dig("institution", "institution_name")
  when "sa_component"
     = meta.dig("crossref", "sa_component", "component_list", "component").to_h
    related_identifier = Array.wrap(query.to_h["crm_item"]).find { |cr| cr["name"] == "relation" }
     = { "relatedIdentifier" => related_identifier.to_h.fetch("__content", nil) }
  when "database"
     = meta.dig("crossref", "database", "dataset").to_h
    resource_type = "dataset"
  end

  resource_type = (resource_type || model).to_s.underscore.camelcase.presence
  schema_org = Bolognese::Utils::CR_TO_SO_TRANSLATIONS[resource_type] || "ScholarlyArticle"
  types = {
    "resourceTypeGeneral" => Bolognese::Utils::SO_TO_DC_TRANSLATIONS[schema_org],
    "resourceType" => resource_type,
    "schemaOrg" => schema_org,
    "citeproc" => Bolognese::Utils::CR_TO_CP_TRANSLATIONS[resource_type] || "article-journal",
    "bibtex" => Bolognese::Utils::CR_TO_BIB_TRANSLATIONS[resource_type] || "misc",
    "ris" => Bolognese::Utils::CR_TO_RIS_TRANSLATIONS[resource_type] || "JOUR"
  }.compact

  titles = if .dig("titles").present?
             Array.wrap(.dig("titles")).map do |r|
               if r.blank? || (r["title"].blank? && r["original_language_title"].blank?)
                 nil
               elsif r["title"].is_a?(String)
                 { "title" => sanitize(r["title"]) }
               elsif r["original_language_title"].present?
                 { "title" => sanitize(r.dig("original_language_title", "__content__")), "lang" => r.dig("original_language_title", "language") }
               else
                 { "title" => sanitize(r.dig("title", "__content__")) }.compact
               end
             end.compact
           else
             [{ "title" => ":{unav)" }]
           end

  date_created = Array.wrap(query.to_h["crm_item"]).find { |cr| cr["name"] == "created" }
  date_updated = Array.wrap(query.to_h["crm_item"]).find { |cr| cr["name"] == "last-update" }
  date_updated = { "date" => date_updated.fetch("__content__", nil), "dateType" => "Updated" } if date_updated.present?
  dates = [
    { "date" => crossref_date_published() || date_created.to_h.fetch("__content__", "")[0..9].presence, "dateType" => "Issued" }, date_updated
  ].compact
  publication_year = crossref_date_published().present? ? crossref_date_published()[0..3] : date_created.to_h.fetch("__content__", "")[0..3].presence
  state = meta.present? || read_options.present? ? "findable" : "not_found"

  related_identifiers = Array.wrap(crossref_is_part_of()) + Array.wrap(crossref_references())
  container = if .present? || .present?
    issn = normalize_issn(.to_h.fetch("issn", nil))

    { "type" => "Journal",
      "identifier" => issn,
      "identifierType" => issn.present? ? "ISSN" : nil,
      "title" => parse_attributes(.to_h["full_title"]),
      "volume" => parse_attributes(journal_issue.dig("journal_volume", "volume")),
      "issue" => parse_attributes(journal_issue.dig("issue")),
      "firstPage" => .dig("pages", "first_page"),
      "lastPage" => .dig("pages", "last_page") }.compact
  elsif .to_h.fetch("series_metadata", nil).present?
    issn = normalize_issn(.dig("series_metadata", "issn"))

    { "type" => "Book Series",
      "identifier" => issn,
      "identifierType" => issn.present? ? "ISSN" : nil,
      "title" => .dig("series_metadata", "titles", "title"),
      "volume" => .fetch("volume", nil) }.compact
  else
    nil
  end

  identifiers = [{ "identifierType" => "DOI", "identifier" => normalize_doi(options[:doi] || options[:id] || .dig("doi_data", "doi")) }, crossref_alternate_identifiers()].compact

  id = Array.wrap(identifiers).first.to_h.fetch("identifier", nil)
  doi = Array.wrap(identifiers).find { |r| r["identifierType"] == "DOI" }.to_h.fetch("identifier", nil)

  { "id" => id,
    "types" => types,
    "doi" => doi_from_url(doi),
    "url" => parse_attributes(.dig("doi_data", "resource"), first: true),
    "titles" => titles,
    "identifiers" => identifiers,
    "creators" => crossref_people(, "author"),
    "contributors" => crossref_people(, "editor"),
    "funding_references" => crossref_funding_reference(),
    "publisher" => publisher,
    "container" => container,
    "agency" => agency = options[:ra] || "Crossref",
    "related_identifiers" => related_identifiers,
    "dates" => dates,
    "publication_year" => publication_year,
    "descriptions" => crossref_description(),
    "rights_list" => crossref_license(),
    "version_info" => nil,
    "subjects" => nil,
    "language" => nil,
    "sizes" => nil,
    "schema_version" => nil,
    "state" => state
  }.merge(read_options)
end