Module: Commonmeta::Readers::CffReader

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

Instance Method Summary collapse

Instance Method Details

#cff_contributors(contributors) ⇒ Object



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
# File 'lib/commonmeta/readers/cff_reader.rb', line 83

def cff_contributors(contributors)
  Array.wrap(contributors).map do |a|
    id = normalize_orcid(parse_attributes(a['orcid']))
    if a['given-names'].present? || a['family-names'].present? || id.present?
      given_name = parse_attributes(a['given-names'])
      family_name = parse_attributes(a['family-names'])
      affiliation = Array.wrap(a['affiliation']).map do |a|
        if a.is_a?(Hash)
          a
        elsif 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.strip.blank?
          nil
        elsif a.is_a?(String)
          { 'name' => a }
        end
      end.compact

      { 'type' => 'Person',
        'contributorRoles' => ['Author'],
        'id' => id,
        'givenName' => given_name,
        'familyName' => family_name,
        'affiliation' => affiliation.presence }.compact
    else
      { 'type' => 'Organization',
        'contributorRoles' => ['Author'],
        'name' => a['name'] || a['__content__'] }
    end
  end
end

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



6
7
8
9
10
11
12
13
14
# File 'lib/commonmeta/readers/cff_reader.rb', line 6

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

  id = normalize_id(id)
  url = github_as_cff_url(id)
  response = HTTP.get(url)

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

#get_cff_reference(reference) ⇒ Object



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
# File 'lib/commonmeta/readers/cff_reader.rb', line 117

def get_cff_reference(reference)
  return nil unless reference.present? || !reference.is_a?(Hash)

  doi = Array.wrap(reference['identifiers']).find { |i| i['type'] == 'doi' }.to_h['value']
  doi = normalize_doi(doi) if doi.present?
  url = reference.dig('url')
  date = {}
  if reference.dig('date-released').present?
    date['published'] = reference.dig('date-released').iso8601
  end

  {
    'key' => doi || url,
    'doi' => doi,
    'url' => url,
    'contributor' => reference.dig('author'),
    'title' => reference.dig('article-title'),
    'publisher' => reference.dig('publisher'),
    'publicationYear' => date['published'] ? date['published'][0..3] : nil,
    '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

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



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
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
# File 'lib/commonmeta/readers/cff_reader.rb', line 16

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

  # Dates are parsed to date object, need to convert to iso8601 later
  meta = string.is_a?(String) ? Psych.safe_load(string, permitted_classes: [Date]) : string

  identifiers = Array.wrap(meta.fetch('identifiers', nil)).map do |r|
    r = normalize_id(r) if r.is_a?(String)
    if r.is_a?(String) && URI(r).host != 'doi.org'
      { 'identifierType' => 'URL', 'identifier' => r }
    elsif r.is_a?(Hash)
      { 'identifierType' => get_identifier_type(r['propertyID']), 'identifier' => r['value'] }
    end
  end.compact.uniq

  id = normalize_id(options[:doi] || meta.fetch('doi',
                                                nil) || Array.wrap(meta.fetch('identifiers', nil)).find do |i|
                                                          i['type'] == 'doi'
                                                        end.to_h.fetch('value', nil))
  url = normalize_id(meta.fetch('repository-code', nil))
  contributors = cff_contributors(Array.wrap(meta.fetch('authors', nil)))

  date = {}
  if meta.fetch('date-released', nil).present?
    date['published'] = meta.fetch('date-released', nil).iso8601
  end

  publisher = url.to_s.starts_with?('https://github.com') ? { 'name' => 'GitHub' } : nil
  state = meta.present? || read_options.present? ? 'findable' : 'not_found'

  type = 'Software'
  subjects = Array.wrap(meta.fetch('keywords', nil)).reduce([]) do |sum, subject|
    sum += name_to_fos(subject)

    sum
  end

  titles = if meta.fetch('title', nil).present?
             [{ 'title' => meta.fetch('title', nil) }]
           else
             []
           end

  references = Array.wrap(meta.fetch('references', nil)).map { |r| get_cff_reference(r) }

  license = hsh_to_spdx('rightsIdentifier' => meta.fetch('license', nil))

  { 'id' => id,
    'type' => type,
    'identifiers' => identifiers,
    'url' => url,
    'titles' => titles,
    'contributors' => contributors,
    'publisher' => publisher,
    'references' => references,
    'date' => date,
    'descriptions' => if meta.fetch('abstract', nil).present?
                        [{ 'description' => sanitize(meta.fetch('abstract')),
                           'descriptionType' => 'Abstract' }]
                      end,
    'license' => license,
    'version' => meta.fetch('version', nil),
    'subjects' => subjects,
    'state' => state }.compact.merge(read_options)
end