Module: Commonmeta::Readers::CodemetaReader

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

Instance Method Summary collapse

Instance Method Details

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



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

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

  id = normalize_id(id)
  url = github_as_codemeta_url(id)
  response = HTTP.get(url)
  return { 'string' => nil, 'state' => 'not_found' } unless response.status.success?

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

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



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
82
83
84
85
86
87
# File 'lib/commonmeta/readers/codemeta_reader.rb', line 17

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

  meta = string.present? ? JSON.parse(string) : {}

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

  id = normalize_id(options[:doi] || meta.fetch('@id', nil) || meta.fetch('identifier', nil))

  has_agents = meta.fetch('agents', nil)
  authors = has_agents.nil? ? meta.fetch('authors', nil) : has_agents
  creators = get_authors(from_schema_org(Array.wrap(authors)))
  contributors = get_authors(from_schema_org(Array.wrap(meta.fetch('editor', nil))))
  # strip milliseconds from iso8601, as edtf library doesn't handle them
  date = {}
  if Date.edtf(strip_milliseconds(meta.fetch('datePublished', nil))).present?
    date['published'] = strip_milliseconds(meta.fetch('datePublished'))
  end
  if Date.edtf(strip_milliseconds(meta.fetch('dateCreated', nil))).present?
    date['created'] = strip_milliseconds(meta.fetch('dateCreated'))
  end
  if Date.edtf(strip_milliseconds(meta.fetch('dateModified', nil))).present?
    date['updated'] = strip_milliseconds(meta.fetch('dateModified'))
  end

  publisher = { 'name' => meta.fetch('publisher', nil) }.compact
  state = meta.present? || read_options.present? ? 'findable' : 'not_found'
  schema_org = meta.fetch('@type', nil)
  type = Commonmeta::Utils::SO_TO_CM_TRANSLATIONS.fetch(schema_org, 'Software')

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

    sum
  end

  has_title = meta.fetch('title', nil)
  titles = if has_title.nil?
             [{ 'title' => meta.fetch('name', nil) }]
           else
             [{ 'title' => has_title }]
           end
  license = hsh_to_spdx('rightsIdentifier' => meta.fetch('licenseId'))

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