Module: Bolognese::DataciteUtils
- Included in:
- MetadataUtils
- Defined in:
- lib/bolognese/datacite_utils.rb
Instance Method Summary collapse
- #datacite_errors(xml: nil, schema_version: nil) ⇒ Object
- #datacite_xml ⇒ Object
- #insert_alternate_identifiers(xml) ⇒ Object
- #insert_contributors(xml) ⇒ Object
- #insert_creators(xml) ⇒ Object
- #insert_dates(xml) ⇒ Object
- #insert_descriptions(xml) ⇒ Object
- #insert_funding_references(xml) ⇒ Object
- #insert_identifier(xml) ⇒ Object
- #insert_person(xml, person, type) ⇒ Object
- #insert_publication_year(xml) ⇒ Object
- #insert_publisher(xml) ⇒ Object
- #insert_related_identifiers(xml) ⇒ Object
- #insert_resource_type(xml) ⇒ Object
- #insert_rights_list(xml) ⇒ Object
- #insert_subjects(xml) ⇒ Object
- #insert_titles(xml) ⇒ Object
- #insert_version(xml) ⇒ Object
- #insert_work(xml) ⇒ Object
- #root_attributes ⇒ Object
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.("../../../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. end |
#datacite_xml ⇒ Object
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
109 110 111 112 113 114 115 116 117 |
# File 'lib/bolognese/datacite_utils.rb', line 109 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["alternateIdentifier"], 'alternateIdentifierType' => alternate_identifier["alternateIdentifierType"]) 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 contributor.present? xml.contributors do Array.wrap(contributor).each do |con| xml.contributor("contributorType" => con["contributorType"] || "Other") do insert_person(xml, con, "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_dates(xml) ⇒ Object
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/bolognese/datacite_utils.rb', line 119 def insert_dates(xml) return xml unless Array.wrap(dates).present? xml.dates do Array.wrap(dates).each do |date| attributes = { 'dateType' => date["dateType"] || "Issued", 'dateInformation' => date["dateInformation"] }.compact xml.date(date["date"], attributes) end end end |
#insert_descriptions(xml) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/bolognese/datacite_utils.rb', line 209 def insert_descriptions(xml) return xml unless descriptions.present? || periodical && periodical["title"].present? xml.descriptions do if periodical && periodical["title"].present? xml.description(periodical["title"], 'descriptionType' => "SeriesInformation") end Array.wrap(descriptions).each do |description| if description.is_a?(Hash) d = description else d = {} d["description"] = description d["descriptionType"] = "Abstract" end attributes = { 'lang' => d["lang"], 'descriptionType' => d["descriptionType"] || "Abstract" }.compact xml.description(d["description"], attributes) end end end |
#insert_funding_references(xml) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/bolognese/datacite_utils.rb', line 130 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 xml.funderName(funding_reference["funderName"]) xml.funderIdentifier(funding_reference["funderIdentifier"], { "funderIdentifierType" => funding_reference["funderIdentifierType"] }.compact) if funding_reference["funderIdentifier"].present? xml.awardNumber(funding_reference["awardNumber"], { "awardURI" => funding_reference["awardUri"] }.compact) if funding_reference["awardNumber"].present? || funding_reference["awardUri"].present? xml.awardTitle(funding_reference["awardTitle"]) if funding_reference["awardTitle"].present? 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 76 |
# 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"] attributes = person["type"].present? ? { "nameType" => person["type"] + "al" } : {} xml.send(type + "Name", person_name, attributes) 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
98 99 100 |
# File 'lib/bolognese/datacite_utils.rb', line 98 def insert_publication_year(xml) xml.publicationYear(publication_year) end |
#insert_publisher(xml) ⇒ Object
94 95 96 |
# File 'lib/bolognese/datacite_utils.rb', line 94 def insert_publisher(xml) xml.publisher(publisher || periodical && periodical["title"]) end |
#insert_related_identifiers(xml) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/bolognese/datacite_utils.rb', line 170 def (xml) return xml unless .present? xml. do .each do || attributes = { 'relatedIdentifierType' => ["relatedIdentifierType"], 'relationType' => ["relationType"], 'resourceTypeGeneral' => ["resourceTypeGeneral"] }.compact attributes.merge({ 'relatedMetadataScheme' => ["relatedMetadataSchema"], 'schemeURI' => ["schemeUri"], 'schemeType' => ["schemeType"]}.compact) if %w(HasMetadata IsMetadataFor).include?(["relationType"]) xml.(["relatedIdentifier"], attributes) end end end |
#insert_resource_type(xml) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/bolognese/datacite_utils.rb', line 102 def insert_resource_type(xml) return xml unless types["schemaOrg"].present? xml.resourceType(types["resourceType"] || types["schemaOrg"], 'resourceTypeGeneral' => types["resourceTypeGeneral"] || Metadata::SO_TO_DC_TRANSLATIONS[types["schemaOrg"]] || "Other") end |
#insert_rights_list(xml) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/bolognese/datacite_utils.rb', line 189 def insert_rights_list(xml) return xml unless rights_list.present? xml.rightsList do Array.wrap(rights_list).each do |rights| if rights.is_a?(Hash) r = rights else r = {} r["rights"] = rights r["rightsUri"] = normalize_id(rights) end attributes = { 'rightsURI' => r["rightsUri"], 'lang' => r["lang"] }.compact xml.rights(r["rights"], attributes) end end end |
#insert_subjects(xml) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/bolognese/datacite_utils.rb', line 145 def insert_subjects(xml) return xml unless subjects.present? xml.subjects do subjects.each do |subject| if subject.is_a?(Hash) s = subject else s = {} s["subject"] = subject end attributes = { "subjectScheme" => s["subjectScheme"], "schemeURI" => s["schemeUri"], "valueURI" => s["valueUri"], "lang" => s["lang"] }.compact xml.subject(s["subject"], attributes) end end end |
#insert_titles(xml) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/bolognese/datacite_utils.rb', line 78 def insert_titles(xml) xml.titles do Array.wrap(titles).each do |title| if title.is_a?(Hash) t = title else t = {} t["title"] = title end attributes = { 'titleType' => t["titleType"], 'lang' => t["lang"] }.compact xml.title(t["title"], attributes) end end end |
#insert_version(xml) ⇒ Object
164 165 166 167 168 |
# File 'lib/bolognese/datacite_utils.rb', line 164 def insert_version(xml) return xml unless version.present? xml.version(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) (xml) insert_version(xml) insert_rights_list(xml) insert_descriptions(xml) end |
#root_attributes ⇒ Object
233 234 235 236 237 |
# File 'lib/bolognese/datacite_utils.rb', line 233 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 |