Module: Bolognese::DataciteUtils

Included in:
MetadataUtils
Defined in:
lib/bolognese/datacite_utils.rb

Instance Method Summary collapse

Instance Method Details

#datacite_errors(xml: nil, schema_version: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/bolognese/datacite_utils.rb', line 13

def datacite_errors(xml: nil, schema_version: nil)
  schema_version = schema_version.to_s.start_with?("http://datacite.org/schema/kernel") ? schema_version : "http://datacite.org/schema/kernel-4"
  kernel = schema_version.to_s.split("/").last
  filepath = File.expand_path("../../../resources/#{kernel}/metadata.xsd", __FILE__)
  schema = Nokogiri::XML::Schema(open(filepath))

  schema.validate(Nokogiri::XML(xml, nil, 'UTF-8')).map { |error| error.to_s }.unwrap
rescue Nokogiri::XML::SyntaxError => e
  e.message
end

#datacite_xmlObject



5
6
7
8
9
10
11
# File 'lib/bolognese/datacite_utils.rb', line 5

def datacite_xml
  @datacite_xml ||= Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
    xml.resource(root_attributes) do
      insert_work(xml)
    end
  end.to_xml
end

#insert_alternate_identifiers(xml) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/bolognese/datacite_utils.rb', line 107

def insert_alternate_identifiers(xml)
  return xml unless alternate_identifiers.present?

  xml.alternateIdentifiers do
    Array.wrap(alternate_identifiers).each do |alternate_identifier|
      xml.alternateIdentifier(alternate_identifier["name"], 'alternateIdentifierType' => alternate_identifier["type"])
    end
  end
end

#insert_contributors(xml) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bolognese/datacite_utils.rb', line 56

def insert_contributors(xml)
  return xml unless editor.present?

  xml.contributors do
    Array.wrap(editor).each do |contributor|
      xml.contributor("contributorType" => "Editor") do
        insert_person(xml, contributor, "contributor")
      end
    end
  end
end

#insert_creators(xml) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/bolognese/datacite_utils.rb', line 46

def insert_creators(xml)
  xml.creators do
    Array.wrap(creator).each do |au|
      xml.creator do
        insert_person(xml, au, "creator")
      end
    end
  end
end

#insert_date(xml, date, date_type) ⇒ Object



124
125
126
# File 'lib/bolognese/datacite_utils.rb', line 124

def insert_date(xml, date, date_type)
  xml.date(date, 'dateType' => date_type)
end

#insert_dates(xml) ⇒ Object



117
118
119
120
121
122
# File 'lib/bolognese/datacite_utils.rb', line 117

def insert_dates(xml)
  xml.dates do
    insert_date(xml, date_published, 'Issued') if date_published.present?
    insert_date(xml, date_modified, 'Updated') if date_modified.present?
  end
end

#insert_descriptions(xml) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/bolognese/datacite_utils.rb', line 193

def insert_descriptions(xml)
  return xml unless description.present? || periodical && periodical["title"].present?

  xml.descriptions do
    if periodical && periodical["title"].present?
      xml.description(periodical["title"], 'descriptionType' => "SeriesInformation")
    end

    Array.wrap(description).each do |des|
      if des.is_a?(Hash)
        d = des
      else
        d = {}
        d["text"] = des
        d["type"] = "Abstract"
      end

      xml.description(d["text"], 'descriptionType' => d["type"] || "Abstract")
    end
  end
end

#insert_funding_reference(xml, funding_reference) ⇒ Object



140
141
142
143
# File 'lib/bolognese/datacite_utils.rb', line 140

def insert_funding_reference(xml, funding_reference)
  xml.funderName(funding_reference["funder_name"]) if funding_reference["funder_name"].present?
  xml.funderIdentifier(funding_reference["funder_identifier"], "funderIdentifierType" => "Crossref Funder ID") if funding_reference["funder_identifier"].present?
end

#insert_funding_references(xml) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/bolognese/datacite_utils.rb', line 128

def insert_funding_references(xml)
  return xml unless Array.wrap(funding_references).present?

  xml.fundingReferences do
    Array.wrap(funding_references).each do |funding_reference|
      xml.fundingReference do
        insert_funding_reference(xml, funding_reference)
      end
    end
  end
end

#insert_identifier(xml) ⇒ Object



42
43
44
# File 'lib/bolognese/datacite_utils.rb', line 42

def insert_identifier(xml)
  xml.identifier(doi, 'identifierType' => "DOI")
end

#insert_person(xml, person, type) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/bolognese/datacite_utils.rb', line 68

def insert_person(xml, person, type)
  person_name = person["familyName"].present? ? [person["familyName"], person["givenName"]].compact.join(", ") : person["name"]

  xml.send(type + "Name", person_name)
  xml.givenName(person["givenName"]) if person["givenName"].present?
  xml.familyName(person["familyName"]) if person["familyName"].present?
  xml.nameIdentifier(person["id"], 'schemeURI' => 'http://orcid.org/', 'nameIdentifierScheme' => 'ORCID') if person["id"].present?
end

#insert_publication_year(xml) ⇒ Object



91
92
93
# File 'lib/bolognese/datacite_utils.rb', line 91

def insert_publication_year(xml)
  xml.publicationYear(publication_year)
end

#insert_publisher(xml) ⇒ Object



87
88
89
# File 'lib/bolognese/datacite_utils.rb', line 87

def insert_publisher(xml)
  xml.publisher(publisher || periodical && periodical["title"])
end


161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/bolognese/datacite_utils.rb', line 161

def insert_related_identifiers(xml)
  return xml unless related_identifiers.present?

  xml.relatedIdentifiers do
    related_identifiers.each do |related_identifier|
      attributes = {
        'relatedIdentifierType' => related_identifier["related_identifier_type"],
        'relationType' => related_identifier["relation_type"],
        'resourceTypeGeneral' => related_identifier["resource_type_general"] }.compact
      xml.relatedIdentifier(related_identifier["id"], attributes)
    end
  end
end

#insert_resource_type(xml) ⇒ Object



100
101
102
103
104
105
# File 'lib/bolognese/datacite_utils.rb', line 100

def insert_resource_type(xml)
  return xml unless type.present?

  xml.resourceType(res_type["__content__"],
    'resourceTypeGeneral' => res_type["resource_type_general"])
end

#insert_rights_list(xml) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/bolognese/datacite_utils.rb', line 175

def insert_rights_list(xml)
  return xml unless rights.present?

  xml.rightsList do
    Array.wrap(rights).each do |lic|
      if lic.is_a?(Hash)
        l = lic
      else
        l = {}
        l["name"] = lic
        l["id"] = normalize_id(lic)
      end

      xml.rights(l["name"], { 'rightsURI' => l["id"] }.compact)
    end
  end
end

#insert_subjects(xml) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/bolognese/datacite_utils.rb', line 145

def insert_subjects(xml)
  return xml unless keywords.present?

  xml.subjects do
    keywords.each do |subject|
      xml.subject(subject)
    end
  end
end

#insert_title(xml) ⇒ Object



83
84
85
# File 'lib/bolognese/datacite_utils.rb', line 83

def insert_title(xml)
  xml.title(title)
end

#insert_titles(xml) ⇒ Object



77
78
79
80
81
# File 'lib/bolognese/datacite_utils.rb', line 77

def insert_titles(xml)
  xml.titles do
    insert_title(xml)
  end
end

#insert_version(xml) ⇒ Object



155
156
157
158
159
# File 'lib/bolognese/datacite_utils.rb', line 155

def insert_version(xml)
  return xml unless b_version.present?

  xml.version(b_version)
end

#insert_work(xml) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bolognese/datacite_utils.rb', line 24

def insert_work(xml)
  insert_identifier(xml)
  insert_creators(xml)
  insert_titles(xml)
  insert_publisher(xml)
  insert_publication_year(xml)
  insert_resource_type(xml)
  insert_alternate_identifiers(xml)
  insert_subjects(xml)
  insert_contributors(xml)
  insert_funding_references(xml)
  insert_dates(xml)
  insert_related_identifiers(xml)
  insert_version(xml)
  insert_rights_list(xml)
  insert_descriptions(xml)
end

#res_typeObject



95
96
97
98
# File 'lib/bolognese/datacite_utils.rb', line 95

def res_type
  { "resource_type_general" => resource_type_general || ::SO_TO_DC_TRANSLATIONS[type] || "Other",
    "__content__" => additional_type || type }
end

#root_attributesObject



215
216
217
218
219
# File 'lib/bolognese/datacite_utils.rb', line 215

def root_attributes
  { :'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
    :'xsi:schemaLocation' => 'http://datacite.org/schema/kernel-4 http://schema.datacite.org/meta/kernel-4/metadata.xsd',
    :'xmlns' => 'http://datacite.org/schema/kernel-4' }
end