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
|
# File 'lib/bolognese/readers/datacite_reader.rb', line 51
def read_datacite(string: nil, **options)
doc = Nokogiri::XML(string, nil, 'UTF-8', &:noblanks)
ns = doc.collect_namespaces.find { |k, v| v.start_with?("http://datacite.org/schema/kernel") }
schema_version = Array.wrap(ns).last || "http://datacite.org/schema/kernel-4"
doc.remove_namespaces!
string = doc.to_xml(:indent => 2)
meta = Maremma.from_xml(string).to_h.fetch("resource", {})
if options[:validate]
errors = datacite_errors(xml: string, schema_version: schema_version)
return { "errors" => errors } if errors.present?
end
if options[:doi]
id = normalize_doi(options[:doi], sandbox: options[:sandbox])
else
id = normalize_doi(meta.dig("identifier", "__content__") || options[:id], sandbox: options[:sandbox])
end
doi = doi_from_url(id)
resource_type_general = meta.dig("resourceType", "resourceTypeGeneral")
additional_type = meta.fetch("resourceType", {}).fetch("__content__", nil)
type = Bolognese::Utils::CR_TO_SO_TRANSLATIONS[additional_type.to_s.underscore.camelcase] || Bolognese::Utils::DC_TO_SO_TRANSLATIONS[resource_type_general.to_s.dasherize] || "CreativeWork"
title = Array.wrap(meta.dig("titles", "title")).map do |r|
if r.is_a?(String)
sanitize(r)
else
{ "title_type" => r["titleType"], "lang" => r["lang"], "text" => sanitize(r["__content__"]) }.compact
end
end.unwrap
alternate_identifiers = Array.wrap(meta.dig("alternateIdentifiers", "alternateIdentifier")).map do |r|
{ "type" => r["alternateIdentifierType"], "name" => r["__content__"] }
end.unwrap
description = Array.wrap(meta.dig("descriptions", "description")).select { |r| r["descriptionType"] != "SeriesInformation" }.map do |r|
{ "type" => r["descriptionType"], "text" => sanitize(r["__content__"]) }.compact
end.unwrap
rights = Array.wrap(meta.dig("rightsList", "rights")).map do |r|
{ "id" => normalize_url(r["rightsURI"]), "name" => r["__content__"] }.compact
end.unwrap
keywords = Array.wrap(meta.dig("subjects", "subject")).map do |k|
if k.nil?
nil
elsif k.is_a?(String)
sanitize(k)
else
{ "subject_scheme" => k["subjectScheme"], "scheme_uri" => k["schemeURI"], "text" => sanitize(k["__content__"]) }.compact
end
end.compact
dates = Array.wrap(meta.dig("dates", "date")).map do |d|
{
"date" => parse_attributes(d),
"date_type" => parse_attributes(d, content: "dateType")
}
end
sizes = Array.wrap(meta.dig("sizes", "size")).unwrap
formats = Array.wrap(meta.dig("formats", "format")).unwrap
funding_references = Array.wrap(meta.dig("fundingReferences", "fundingReference")).compact.map do |fr|
{
"funder_name" => fr["funderName"],
"funder_identifier" => normalize_id(parse_attributes(fr["funderIdentifier"])),
"funder_identifier_type" => parse_attributes(fr["funderIdentifier"], content: "funderIdentifierType"),
"award_number" => parse_attributes(fr["awardNumber"]),
"award_uri" => parse_attributes(fr["awardNumber"], content: "awardURI"),
"award_title" => fr["awardTitle"] }.compact
end
related_identifiers = Array.wrap(meta.dig("relatedIdentifiers", "relatedIdentifier")).map do |ri|
if ri["relatedIdentifierType"] == "DOI"
rid = ri["__content__"].downcase
else
rid = ri["__content__"]
end
{
"id" => rid,
"related_identifier_type" => ri["relatedIdentifierType"],
"relation_type" => ri["relationType"],
"resource_type_general" => ri["resourceTypeGeneral"]
}.compact
end
geo_location = Array.wrap(meta.dig("geoLocations", "geoLocation")).map do |gl|
if gl["geoLocationPoint"].is_a?(String) || gl["geoLocationBox"].is_a?(String)
nil
else
{
"geo_location_place" => gl["geoLocationPlace"],
"geo_location_point" => {
"point_latitude" => gl.dig("geoLocationPoint", "pointLatitude"),
"point_longitude" => gl.dig("geoLocationPoint", "pointLongitude")
}.compact.presence,
"geo_location_box" => {
"west_bound_longitude" => gl.dig("geoLocationBox", "westBoundLongitude"),
"east_bound_longitude" => gl.dig("geoLocationBox", "eastBoundLongitude"),
"south_bound_latitude" => gl.dig("geoLocationBox", "southBoundLatitude"),
"north_bound_latitude" => gl.dig("geoLocationBox", "northBoundLatitude")
}.compact.presence
}.compact
end
end
periodical = set_periodical(meta)
state = doi.present? ? "findable" : "not_found"
{ "id" => id,
"type" => type,
"additional_type" => additional_type,
"citeproc_type" => Bolognese::Utils::CR_TO_CP_TRANSLATIONS[additional_type.to_s.underscore.camelcase] || Bolognese::Utils::SO_TO_CP_TRANSLATIONS[type] || "article",
"bibtex_type" => Bolognese::Utils::CR_TO_BIB_TRANSLATIONS[additional_type.to_s.underscore.camelcase] || Bolognese::Utils::SO_TO_BIB_TRANSLATIONS[type] || "misc",
"ris_type" => Bolognese::Utils::CR_TO_RIS_TRANSLATIONS[additional_type.to_s.underscore.camelcase] || Bolognese::Utils::DC_TO_RIS_TRANSLATIONS[resource_type_general.to_s.dasherize] || "GEN",
"resource_type_general" => resource_type_general,
"doi" => doi,
"alternate_identifiers" => alternate_identifiers,
"url" => options.fetch(:url, nil),
"title" => title,
"creator" => get_authors(Array.wrap(meta.dig("creators", "creator"))),
"periodical" => periodical,
"publisher" => meta.fetch("publisher", "").strip.presence,
"service_provider" => "DataCite",
"funding_references" => funding_references,
"dates" => dates,
"date_published" => datacite_date(dates, "Issued") || meta.fetch("publicationYear", nil),
"date_modified" => datacite_date(dates, "Updated"),
"description" => description,
"rights" => rights,
"b_version" => meta.fetch("version", nil),
"keywords" => keywords,
"language" => meta.fetch("language", nil),
"geo_location" => geo_location,
"related_identifiers" => related_identifiers,
"b_format" => formats,
"size" => sizes,
"schema_version" => schema_version,
"state" => state
}
end
|