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
|
# File 'lib/commonmeta/readers/datacite_reader.rb', line 27
def read_datacite(string: nil, **_options)
errors = jsonlint(string)
return { 'errors' => errors } if errors.present?
read_options = ActiveSupport::HashWithIndifferentAccess.new(_options.except(:doi, :id, :url,
:sandbox, :validate, :ra))
meta = string.present? ? JSON.parse(string) : {}
meta = meta.dig('data', 'attributes') if meta.dig('data').present?
meta.transform_keys!(&:underscore)
id = normalize_doi(meta.fetch('doi', nil))
resource_type_general = meta.dig('types', 'resourceTypeGeneral')
resource_type = meta.dig('types', 'resourceType')
type = Commonmeta::Utils::DC_TO_CM_TRANSLATIONS.fetch(resource_type, nil) ||
Commonmeta::Utils::DC_TO_CM_TRANSLATIONS.fetch(resource_type_general, 'Other')
alternate_identifiers = Array.wrap(meta.fetch('alternate_identifiers', nil)).map do |i|
i.transform_keys! { |k| k.camelize(:lower) }
end
url = meta.fetch('url', nil)
titles = Array.wrap(meta.fetch('titles', nil)).map do |title|
title.compact
end
creators = get_authors(from_datacite(meta.fetch('creators', nil)))
publisher = { 'name' => meta.fetch('publisher', nil) }
contributors = get_authors(from_datacite(meta.fetch('contributors', nil)))
container = meta.fetch('container', nil)
funding_references = meta.fetch('funding_references', nil)
date = {}
date['created'] =
get_iso8601_date(meta.dig('created')) || get_date(meta.dig('dates'), 'Created')
date['published'] =
get_iso8601_date(meta.dig('published')) || get_date(meta.dig('dates'),
'Issued') || get_iso8601_date(meta.dig('publication_year'))
date['registered'] = get_iso8601_date(meta.dig('registered'))
date['updated'] =
get_iso8601_date(meta.dig('updated')) || get_date(meta.dig('dates'), 'Updated')
descriptions = Array.wrap(meta.fetch('descriptions', nil)).map do |description|
description.compact
end
license = Array.wrap(meta.fetch('rights_list', nil)).find do |r|
r['rightsUri'].present?
end
license = hsh_to_spdx('rightsURI' => license['rightsUri']) if license.present?
version = meta.fetch('version', nil)
subjects = meta.fetch('subjects', nil)
language = meta.fetch('language', nil)
geo_locations = meta.fetch('geo_locations', nil)
references = (Array.wrap(meta.fetch('related_identifiers',
nil)) + Array.wrap(meta.fetch('related_items',
nil))).select do |r|
%w[References Cites IsSupplementedBy].include?(r['relationType'])
end.map do |reference|
get_datacite_reference(reference)
end
formats = meta.fetch('formats', nil)
sizes = meta.fetch('sizes', nil)
schema_version = meta.fetch('schema_version', nil) || 'http://datacite.org/schema/kernel-4'
state = id.present? || read_options.present? ? 'findable' : 'not_found'
{ 'id' => id,
'type' => type,
'additional_type' => resource_type == type ? nil : resource_type,
'url' => url,
'titles' => titles,
'creators' => creators,
'contributors' => contributors,
'container' => container,
'publisher' => publisher,
'provider' => 'DataCite',
'alternate_identifiers' => alternate_identifiers.presence,
'references' => references,
'funding_references' => funding_references,
'date' => date.compact,
'descriptions' => descriptions,
'license' => license,
'version' => version,
'subjects' => subjects,
'language' => language,
'geo_locations' => geo_locations,
'formats' => formats,
'sizes' => sizes,
'state' => state }.compact end
|