Module: Commonmeta::Readers::CrossrefXmlReader

Included in:
MetadataUtils
Defined in:
lib/commonmeta/readers/crossref_xml_reader.rb

Instance Method Summary collapse

Instance Method Details

#crossref_alternate_identifiers(bibmeta) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/commonmeta/readers/crossref_xml_reader.rb', line 214

def crossref_alternate_identifiers(bibmeta)
  if bibmeta.dig('publisher_item', 'item_number').present?
    Array.wrap(bibmeta.dig('publisher_item', 'item_number')).map do |item|
      if item.is_a?(String)
        { 'alternateIdentifier' => item,
          'alternateIdentifierType' => 'Publisher ID' }
      else
        { 'alternateIdentifier' => item.fetch('__content__', nil),
          'alternateIdentifierType' => item.fetch('item_number_type', nil) || 'Publisher ID' }
      end
    end
  elsif parse_attributes(bibmeta.fetch('item_number', nil)).present?
    [{ 'alternateIdentifier' => parse_attributes(bibmeta.fetch('item_number', nil)),
       'alternateIdentifierType' => parse_attributes(bibmeta.dig('item_number',
                                                                 'item_number_type')) || 'Publisher ID' }]
  elsif parse_attributes(bibmeta.fetch('isbn', nil)).present?
    [{ 'alternateIdentifier' => parse_attributes(bibmeta.fetch('isbn', nil), first: true),
       'alternateIdentifierType' => 'ISBN' }]
  else
    []
  end
end

#crossref_date_published(bibmeta) ⇒ Object



346
347
348
349
350
351
352
# File 'lib/commonmeta/readers/crossref_xml_reader.rb', line 346

def crossref_date_published(bibmeta)
  pub_date = Array.wrap(bibmeta.fetch('publication_date', nil)).presence ||
             Array.wrap(bibmeta.fetch('acceptance_date', nil))
  return unless pub_date.present?

  get_date_from_parts(pub_date.first['year'], pub_date.first['month'], pub_date.first['day'])
end

#crossref_description(bibmeta) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/commonmeta/readers/crossref_xml_reader.rb', line 237

def crossref_description(bibmeta)
  abstract = Array.wrap(bibmeta['abstract']).map do |r|
    { 'descriptionType' => r.fetch('abstract_type', nil).present? ? 'Other' : 'Abstract',
      'description' => sanitize(parse_attributes(r, content: 'p'), first: true) }.compact
  end

  description = Array.wrap(bibmeta['description']).map do |r|
    { 'descriptionType' => 'Other', 'description' => sanitize(parse_attributes(r)) }.compact
  end

  (abstract + description)
end

#crossref_funding_reference(program_metadata) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/commonmeta/readers/crossref_xml_reader.rb', line 310

def crossref_funding_reference()
  fundref = Array.wrap().find { |a| a['name'] == 'fundref' } || {}
  Array.wrap(fundref.fetch('assertion', [])).select do |a|
    a['name'] == 'fundgroup' && a['assertion'].present?
  end.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|
      case a.fetch('name')
      when 'award_number'
        award_number = a.fetch('__content__', nil)
        award_uri = a.fetch('awardURI', nil)
      when '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
    next unless funder_name.present?

    { 'funderIdentifier' => funder_identifier,
      'funderIdentifierType' => funder_identifier_type,
      'funderName' => funder_name,
      'awardTitle' => award_title,
      'awardNumber' => award_number,
      'awardUri' => award_uri }.compact
  end.compact
end

#crossref_license(program_metadata) ⇒ Object



250
251
252
253
254
255
256
257
# File 'lib/commonmeta/readers/crossref_xml_reader.rb', line 250

def crossref_license()
  access_indicator = Array.wrap().find { |m| m['name'] == 'AccessIndicators' }
  return unless access_indicator.present?

  Array.wrap(access_indicator['license_ref']).reduce({}) do |sum, license|
    sum.merge!(hsh_to_spdx('rightsURI' => parse_attributes(license)))
  end
end

#crossref_people(bibmeta, contributor_role) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/commonmeta/readers/crossref_xml_reader.rb', line 259

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

  (Array.wrap(person) + Array.wrap(organization)).select do |a|
    a['contributor_role'] == contributor_role
  end.map do |a|
    id = parse_attributes(a['ORCID']) ? normalize_orcid(parse_attributes(a['ORCID'])) : nil
    if a['surname'].present? || a['given_name'].present?
      given_name = parse_attributes(a['given_name']).presence
      family_name = parse_attributes(a['surname']).presence
      affiliation = Array.wrap(a['affiliation']).map do |a|
        if a.is_a?(Hash) && a.key?('__content__') && a['__content__'].strip.blank?
          nil
        elsif a.is_a?(Hash) && a.key?('__content__')
          { 'name' => a['__content__'] }
        elsif a.is_a?(Hash)
          a
        elsif a.strip.blank?
          nil
        elsif a.is_a?(String)
          { 'name' => a }
        end
      end.compact

      { 'type' => 'Person',
        'id' => id,
        'name' => if given_name || family_name
                    nil
                  else
                    [family_name, given_name].compact.join(', ')
                  end,
        'givenName' => given_name,
        'familyName' => family_name,
        'affiliation' => affiliation.presence,
        'contributorRoles' => contributor_role == 'editor' ? ['Editor'] : ['Author'] }.compact
    else
      { 'type' => 'Organization',
        'name' => a['name'] || a['__content__'],
        'contributorRoles' => contributor_role == 'editor' ? ['Editor'] : ['Author'] }.compact
    end
  end
end

#crossref_references(bibmeta) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/commonmeta/readers/crossref_xml_reader.rb', line 354

def crossref_references(bibmeta)
  refs = bibmeta.dig('citation_list', 'citation')
  Array.wrap(refs).map do |reference|
    return nil unless reference.present? || !reference.is_a?(Hash)

    doi = parse_attributes(reference.dig('doi'))

    {
      'key' => reference.dig('key'),
      'doi' => doi ? normalize_doi(doi) : nil,
      'contributor' => reference.dig('author'),
      'title' => reference.dig('article_title'),
      'publisher' => reference.dig('publisher'),
      'publicationYear' => reference.dig('cYear'),
      'volume' => reference.dig('volume'),
      'issue' => reference.dig('issue'),
      'firstPage' => reference.dig('first_page'),
      'lastPage' => reference.dig('last_page'),
      'containerTitle' => reference.dig('journal_title'),
      'edition' => nil,
      'unstructured' => doi.nil? ? reference.dig('unstructured') : nil
    }.compact
  end.unwrap
end

#get_crossref_xml(id: nil, **_options) ⇒ Object

CrossRef types from api.crossref.org/types



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

def get_crossref_xml(id: nil, **_options)
  return { 'string' => nil, 'state' => 'not_found' } unless id.present?

  doi = doi_from_url(id)
  api_url = "https://api.crossref.org/works/#{doi}/transform/application/vnd.crossref.unixsd+xml"
  response = HTTP.get(api_url)
  return { 'string' => nil, 'state' => 'not_found' } unless response.status.success?

  { 'string' => response.body.to_s }
end

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



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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/commonmeta/readers/crossref_xml_reader.rb', line 18

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

  if string.present?
    # query contains information from outside metadata schema, e.g. publisher name
    query = Hash.from_xml(string).dig('crossref_result', 'query_result', 'body', 'query')
    meta = query.dig('doi_record', 'crossref', 'error').nil? ? query.dig('doi_record') : {}
  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['crossref'].to_h.keys.first

  resource_type = nil
  bibmeta = {}
   = {}
   = nil
  journal_issue = {}
   = nil
  publisher = query.dig('crm_item', 0)
  publisher = nil unless publisher.is_a?(String)

  case model
  when 'book'
     = meta.dig('crossref', 'book', 'book_metadata')
     = meta.dig('crossref', 'book', 'book_series_metadata')
     = meta.dig('crossref', 'book', 'book_set_metadata')
    bibmeta = meta.dig('crossref', 'book',
                       'content_item') ||  ||  || 
    resource_type = if bibmeta.fetch('component_type', nil)
                      "book-#{bibmeta.fetch('component_type')}"
                    else
                      'book'
                    end
    # publisher = if book_metadata.present?
    #               book_metadata.dig("publisher", "publisher_name")
    #             elsif book_series_metadata.present?
    #               book_series_metadata.dig("publisher", "publisher_name")
    #             end
  when 'conference'
     = meta.dig('crossref', 'conference', 'event_metadata') || {}
    bibmeta = 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') || {}
    bibmeta = journal_article.presence || journal_issue.presence || 
     = bibmeta.dig('crossmark', 'custom_metadata',
                                   'program') || bibmeta['program']
    resource_type = if journal_article.present?
                      'journal_article'
                    elsif journal_issue.present?
                      'journal_issue'
                    else
                      'journal'
                    end
  when 'posted_content'
    bibmeta = meta.dig('crossref', 'posted_content').to_h
    publisher ||= bibmeta.dig('institution', 'institution_name')
  when 'sa_component'
    bibmeta = meta.dig('crossref', 'sa_component', 'component_list', 'component').to_h
    related_identifier = Array.wrap(query.to_h['crm_item']).find do |cr|
      cr['name'] == 'relation'
    end
     = { 'relatedIdentifier' => related_identifier.to_h.fetch('__content',
                                                                              nil) }
  when 'database'
    bibmeta = meta.dig('crossref', 'database', 'dataset').to_h
    resource_type = 'dataset'
  when 'report_paper'
    bibmeta = meta.dig('crossref', 'report_paper', 'report_paper_metadata').to_h
    resource_type = 'report'
  when 'peer_review'
    bibmeta = meta.dig('crossref', 'peer_review')
  when 'dissertation'
    bibmeta = meta.dig('crossref', 'dissertation')
  end

  resource_type = (resource_type || model).to_s.underscore.camelcase.presence
  resource_type = resource_type.present? ? resource_type.underscore.camelcase : nil
  type = Commonmeta::Utils::CR_TO_CM_TRANSLATIONS.fetch(resource_type, 'Other')

  contributors = crossref_people(bibmeta, 'author')
  contributors += crossref_people(bibmeta, 'editor')

  titles = if bibmeta['titles'].present?
             Array.wrap(bibmeta['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
                 # TODO: handle titles with <i> and <b> tags
                 { 'title' => sanitize(r.dig('title', '__content__')) }.compact
               end
             end.compact
           else
             [{ 'title' => ':(unav)' }]
           end

  date = {}
  date['published'] = crossref_date_published(bibmeta) # || Array.wrap(query.to_h["crm_item"]).find { |cr| cr["name"] == "created" }
  date['updated'] = parse_attributes(Array.wrap(query.to_h['crm_item']).find do |cr|
                                       cr['name'] == 'last-update'
                                     end)
  date['registered'] = Array.wrap(query.to_h['crm_item']).find do |cr|
    cr['name'] == 'deposit-timestamp'
  end
  if date['registered'].present?
    date['registered'] = get_datetime_from_time(parse_attributes(date['registered']))
  end

  # check that date is valid iso8601 date
  date['published'] = nil unless Date.edtf(date['published']).present?
  date['updated'] = nil unless Date.edtf(date['updated']).present?

  # TODO: fix timestamp. Until then, remove time as this is not always stable with Crossref (different server timezones)
  date['published'] = get_iso8601_date(date['published']) if date['published'].present?
  date['registered'] = get_iso8601_date(date['registered']) if date['registered'].present?
  date['updated'] = get_iso8601_date(date['updated']) if date['updated'].present?

  state = meta.present? || read_options.present? ? 'findable' : 'not_found'

  references = Array.wrap(crossref_references(bibmeta))

  container = if .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['issue']),
                  'firstPage' => bibmeta.dig('pages',
                                             'first_page') || parse_attributes(journal_article.to_h.dig('publisher_item', 'item_number'),
                                                                               first: true),
                  'lastPage' => bibmeta.dig('pages', 'last_page') }.compact

              # By using book_metadata, we can account for where resource_type is `BookChapter` and not assume its a whole book
              elsif .present?
                identifiers = crossref_alternate_identifiers()

                {
                  'type' => 'Book',
                  'title' => .dig('titles', 'title'),
                  'firstPage' => bibmeta.dig('pages', 'first_page'),
                  'lastPage' => bibmeta.dig('pages', 'last_page'),
                  'identifiers' => identifiers
                }.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' => bibmeta.fetch('volume', nil) }.compact
              end

  id = normalize_doi(options[:doi] || options[:id] || bibmeta.dig('doi_data', 'doi'))

  # Let sections override this in case of alternative metadata structures, such as book chapters, which
  # have their meta inside `content_item`, but the main book indentifers inside of `book_metadata`
  alternate_identifiers ||= crossref_alternate_identifiers(bibmeta)
  url = parse_attributes(bibmeta.dig('doi_data', 'resource'), first: true)
  url = normalize_url(url) if url.present?

  { 'id' => id,
    'type' => type,
    'url' => url,
    'titles' => titles,
    'alternate_identifiers' => alternate_identifiers,
    'contributors' =>  contributors,
    'funding_references' => crossref_funding_reference(),
    'publisher' => { 'name' => publisher },
    'container' => container,
    'provider' => provider = options[:ra] || get_doi_ra(id) || 'Crossref',
    'references' => references,
    'date' => date.compact,
    'descriptions' => crossref_description(bibmeta),
    'license' => crossref_license(),
    'version' => nil,
    'subjects' => nil,
    'language' => nil,
    'sizes' => nil,
    'state' => state }.compact.merge(read_options)
end