relaton 3.1.5 → 3.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/relaton/cli.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "fileutils"
2
+ require "moxml"
2
3
  require "thor"
3
4
  require "thor/hollaback"
4
5
  require_relative "cli/util"
@@ -61,25 +62,32 @@ module Relaton
61
62
  RelatonDb.instance.db dir
62
63
  end
63
64
 
64
- # @param content [Nokogiri::XML::Document]
65
- # @return [RelatonBib::BibliographicItem,
66
- # RelatonIsoBib::IsoBibliongraphicItem]
67
- def parse_xml(doc)
68
- doc.remove_namespaces! if doc.respond_to?(:remove_namespaces!)
65
+ # Pick the flavor processor from the first docidentifier, in any
66
+ # namespace, and hand it the XML with no namespaces (relaton#581).
67
+ #
68
+ # @param xml [String] a bibdata or bibitem
69
+ # @return [Relaton::Bib::ItemData, nil] nil when the source is not XML
70
+ def parse_xml(xml)
71
+ return unless xml
72
+
73
+ doc = Moxml.new.parse(xml)
74
+ plain = without_namespaces(doc)
69
75
  if (proc = Cli.processor(doc))
70
- proc.from_xml(doc.to_s)
76
+ proc.from_xml(plain)
71
77
  else
72
- Relaton::Bib::Item.from_xml(doc.to_s) rescue nil
78
+ Relaton::Bib::Item.from_xml(plain) rescue nil
73
79
  end
80
+ rescue Moxml::ParseError
81
+ nil
74
82
  end
75
83
 
76
- # @param doc [Nokogiri::XML::Element]
77
- # @return [RelatonIso::Processor, RelatonIec::Processor,
78
- # RelatonNist::Processor, RelatonIetf::Processot,
79
- # RelatonItu::Processor, RelatonGb::Processor,
80
- # RelatonOgc::Processor, RelatonCalconnect::Processor]
84
+ # The processor named by the first docidentifier's type, else by the
85
+ # first word of its text.
86
+ #
87
+ # @param doc [Moxml::Document]
88
+ # @return [Relaton::Core::Processor, nil]
81
89
  def processor(doc)
82
- docid = doc.at "docidentifier"
90
+ docid = doc.xpath("//*[local-name()='docidentifier']").first
83
91
  proc = get_proc docid
84
92
  return proc if proc
85
93
 
@@ -88,15 +96,66 @@ module Relaton
88
96
 
89
97
  private
90
98
 
91
- # @param doc [Nokogiri::XML::Element]
92
- # @return [RelatonIso::Processor, RelatonIec::Processor,
93
- # RelatonNist::Processor, RelatonIetf::Processot,
94
- # RelatonItu::Processor, RelatonGb::Processor,
95
- # RelatonOgc::Processor, RelatonCalconnect::Processor]
99
+ # The root element as XML, with every element and attribute in no
100
+ # namespace (`xml:` attributes keep their prefix). The lutaml-model
101
+ # mappings are namespace-free: they skip a prefixed element, and a
102
+ # nested `relation/bibitem` in a default namespace raises in
103
+ # lutaml-model 0.8.175 (it calls `mappings_for` on `Bib::ItemData`).
104
+ # Moxml cannot drop a namespace in place, so this builds a copy.
105
+ #
106
+ # @param doc [Moxml::Document]
107
+ # @return [String]
108
+ def without_namespaces(doc)
109
+ plain = Moxml.new.create_document
110
+ plain.add_child plain_element(plain, doc.root)
111
+ plain.root.to_xml
112
+ end
113
+
114
+ # @param doc [Moxml::Document] the document the copy belongs to
115
+ # @param src [Moxml::Element]
116
+ # @return [Moxml::Element]
117
+ def plain_element(doc, src)
118
+ elm = doc.create_element(src.name)
119
+ # An attribute in no namespace wins over a prefixed one of the
120
+ # same local name (`type` over `a:type`).
121
+ src.attributes.sort_by { |a| a.namespace ? 0 : 1 }.each do |attr|
122
+ elm[plain_name(attr)] = attr.value
123
+ end
124
+ src.children.each { |child| elm.add_child plain_node(doc, child) }
125
+ elm
126
+ end
127
+
128
+ # @param attr [Moxml::Attribute]
129
+ # @return [String] the local name; an `xml:` attribute keeps its prefix
130
+ def plain_name(attr)
131
+ attr.namespace&.prefix == "xml" ? "xml:#{attr.name}" : attr.name
132
+ end
133
+
134
+ # An entity reference (` `) stays as its text: lutaml-model cannot
135
+ # read an entity it has no declaration for, the way `XmlTree.escape`
136
+ # escapes its `&`. A processing instruction becomes an empty text.
137
+ #
138
+ # @param doc [Moxml::Document]
139
+ # @param src [Moxml::Node]
140
+ # @return [Moxml::Node]
141
+ def plain_node(doc, src)
142
+ case src
143
+ when Moxml::Element then plain_element(doc, src)
144
+ when Moxml::Cdata then doc.create_cdata(src.content)
145
+ when Moxml::Text then doc.create_text(src.content)
146
+ when Moxml::Comment then doc.create_comment(src.content)
147
+ when Moxml::EntityReference then doc.create_text("&#{src.name};")
148
+ else doc.create_text("")
149
+ end
150
+ end
151
+
152
+ # @param docid [Moxml::Element, nil]
153
+ # @return [Relaton::Core::Processor, nil]
96
154
  def get_proc(docid)
97
- return unless docid && docid[:type]
155
+ type = docid&.[]("type")
156
+ return unless type
98
157
 
99
- Relaton::Db::Registry.instance.by_type(docid[:type])
158
+ Relaton::Db::Registry.instance.by_type(type)
100
159
  end
101
160
  end
102
161
  end
@@ -0,0 +1,145 @@
1
+ require "lutaml/model"
2
+ require "lutaml/xml"
3
+ require "moxml"
4
+
5
+ module Relaton
6
+ # The namespace of a `relaton-collection` document.
7
+ class CollectionNamespace < Lutaml::Xml::W3c::XmlNamespace
8
+ uri "https://open.ribose.com/relaton-xml"
9
+ element_form_default :qualified
10
+ end
11
+
12
+ # The lutaml-model XML mapping of `Relaton::Bibcollection` (relaton#459).
13
+ #
14
+ # A collection is read in any namespace (a collection an older relaton
15
+ # wrote, or one with no namespace at all), and written in
16
+ # `CollectionNamespace`.
17
+ #
18
+ # The title and the author keep their inline markup (`<em>`, …) as a
19
+ # String, as the model always held them. An item element carries no type
20
+ # discriminator, and its class is a flavor's (chosen by its
21
+ # docidentifier), so `relation` is mapped with custom methods: each item
22
+ # is read by `Bibdata.from_xml` or `Bibcollection.from_xml`, and written
23
+ # by its own `to_xml`.
24
+ #
25
+ # The markup and each item's XML go into the tree as an `XmlRawFragment`,
26
+ # exactly as written: a copy element by element into lutaml's data model
27
+ # loses foreign namespaces (MathML, `xlink:`, `xml:lang`), named entities
28
+ # and comments, and the pretty-printer adds whitespace to mixed content.
29
+ class CollectionXml < Lutaml::Model::Serializable
30
+ model Bibcollection
31
+
32
+ attribute :title, :string, collection: true
33
+ attribute :author, :string, collection: true
34
+ attribute :doctype, :string
35
+
36
+ xml do
37
+ root "relaton-collection"
38
+ namespace CollectionNamespace
39
+
40
+ map_attribute "type", to: :doctype
41
+ map_element "title", to: :title,
42
+ with: { from: :title_from_xml, to: :title_to_xml }
43
+ map_element "contributor", to: :author,
44
+ with: { from: :author_from_xml, to: :author_to_xml }
45
+ map_element "relation", to: :items,
46
+ with: { from: :items_from_xml, to: :items_to_xml }
47
+ end
48
+
49
+ def title_from_xml(model, value)
50
+ model.title = XmlTree.inner_xml first(value)
51
+ end
52
+
53
+ def title_to_xml(model, parent, _doc)
54
+ parent.add_child XmlTree.element("title", model.title) if model.title
55
+ end
56
+
57
+ def author_from_xml(model, value)
58
+ contributor = Array(value).find do |c|
59
+ child(c, "role")&.attributes&.values&.any? do |a|
60
+ a.name == "type" && a.value == "author"
61
+ end
62
+ end
63
+ model.author = XmlTree.inner_xml child(child(contributor, "organization"), "name")
64
+ end
65
+
66
+ def author_to_xml(model, parent, _doc)
67
+ return unless model.author
68
+
69
+ role = XmlTree.element("role")
70
+ role.add_attribute XmlTree::XML::XmlAttribute.new("type", "author")
71
+ org = XmlTree.element("organization")
72
+ org.add_child XmlTree.element("name", model.author)
73
+ parent.add_child XmlTree.element("contributor").add_child(role).add_child(org)
74
+ end
75
+
76
+ def items_from_xml(model, value)
77
+ model.items = Array(value).filter_map do |relation|
78
+ el = relation.element_children.first
79
+ next unless el
80
+
81
+ if el.unprefixed_name == "relaton-collection"
82
+ Bibcollection.from_xml el.to_xml
83
+ else
84
+ Bibdata.from_xml el.to_xml
85
+ end
86
+ end
87
+ end
88
+
89
+ def items_to_xml(model, parent, _doc)
90
+ model.items.each do |item|
91
+ relation = XmlTree.element("relation")
92
+ relation.add_attribute XmlTree::XML::XmlAttribute.new("type", "partOf")
93
+ relation.add_child XmlTree.raw(item.to_xml(model.item_options || {}))
94
+ parent.add_child relation
95
+ end
96
+ end
97
+
98
+ private
99
+
100
+ def first(value)
101
+ value.is_a?(Array) ? value.first : value
102
+ end
103
+
104
+ # @return [Lutaml::Xml::XmlElement, nil] the first child element named
105
+ # `name`, in any namespace
106
+ def child(element, name)
107
+ element&.element_children&.find { |c| c.unprefixed_name == name }
108
+ end
109
+
110
+ # Builds the elements the mapper writes, in the collection namespace.
111
+ module XmlTree
112
+ XML = Lutaml::Xml::DataModel
113
+
114
+ module_function
115
+
116
+ # @param name [String]
117
+ # @param markup [String, nil] the content: text with inline markup
118
+ # @return [Lutaml::Xml::DataModel::XmlElement]
119
+ def element(name, markup = nil)
120
+ el = XML::XmlElement.new(name, CollectionNamespace)
121
+ markup ? el.add_child(raw(escape(markup))) : el
122
+ end
123
+
124
+ # @param xml [String] XML written as it is
125
+ # @return [Lutaml::Xml::DataModel::XmlRawFragment]
126
+ def raw(xml)
127
+ XML::XmlRawFragment.new xml.strip
128
+ end
129
+
130
+ # @param element [Lutaml::Xml::XmlElement, nil] a parsed element
131
+ # @return [String, nil] its content, as markup
132
+ def inner_xml(element)
133
+ element&.inner_xml
134
+ end
135
+
136
+ # Escapes a bare `&` (plain text from YAML), leaving the XML entity
137
+ # references (`&amp;`, `&lt;`, `&#123;`, `&#x1f;`) and inline markup
138
+ # as they are. An HTML entity (`&nbsp;`) is not defined in XML, so its
139
+ # `&` is escaped too: the fragment parser cannot read it.
140
+ def escape(str)
141
+ str.gsub(/&(?!(?:amp|lt|gt|quot|apos);|#[0-9]+;|#x[0-9a-fA-F]+;)/, "&amp;")
142
+ end
143
+ end
144
+ end
145
+ end
@@ -13,7 +13,7 @@ module Relaton
13
13
  relaton/oasis relaton/doi relaton/jis relaton/xsf
14
14
  relaton/ccsds relaton/etsi relaton/isbn relaton/plateau
15
15
  relaton/oiml relaton/jcgm relaton/easc relaton/gost relaton/adobe
16
- relaton/iala relaton/evs relaton/sae
16
+ relaton/iala relaton/evs relaton/sae relaton/arxiv
17
17
  ].freeze
18
18
 
19
19
  include Singleton
@@ -74,7 +74,11 @@ module Relaton
74
74
  # @param type [String]
75
75
  # @return [Relaton::Core::Processor]
76
76
  def by_type(type)
77
- processors.values.detect { |v| v.prefix == type&.upcase }
77
+ # Case-insensitive: a prefix can be mixed case (`arXiv`, `Adobe`),
78
+ # and the CLI upcases `--type`.
79
+ return unless type
80
+
81
+ processors.values.detect { |v| v.prefix.casecmp?(type.to_s) }
78
82
  end
79
83
 
80
84
  def [](stdclass)
@@ -3,10 +3,21 @@
3
3
  Relaton::Doi is a Ruby gem that implements the
4
4
  https://github.com/metanorma/metanorma-model-iso#iso-bibliographic-item[IsoBibliographicItem model].
5
5
 
6
- You can use it to retrieve metadata of Standards from https://crossref.org, and
7
- access such metadata through the `BibliographicItem` object.
6
+ You can use it to retrieve metadata of Standards from https://crossref.org and
7
+ https://datacite.org, and access such metadata through the `BibliographicItem` object.
8
8
 
9
- To collect metadata this gem makes a few requests to the CrossRef API. It may take a few seconds to get the metadata.
9
+ `Relaton::Doi.get` first asks https://doi.org/ra/ which registration agency holds the
10
+ DOI, then fetches the metadata from the Crossref API or the DataCite API. A DOI of
11
+ another agency (mEDRA, JaLC, …) is not supported. It may take a few seconds to get the
12
+ metadata.
13
+
14
+ [source,ruby]
15
+ ----
16
+ Relaton::Doi.get "doi:10.13140/RG.2.1.4576.8809"
17
+ [relaton-doi] INFO: (doi:10.13140/RG.2.1.4576.8809) Fetching from api.datacite.org ...
18
+ [relaton-doi] INFO: (doi:10.13140/RG.2.1.4576.8809) Found: `10.13140/rg.2.1.4576.8809`
19
+ => #<Relaton::Bib::ItemData:0x...
20
+ ----
10
21
 
11
22
  == Usage
12
23
 
@@ -31,20 +31,10 @@ module Relaton
31
31
  end
32
32
 
33
33
  #
34
- # The DOI itself (`<prefix>/<suffix>`), as the Crossref API takes it.
35
- #
36
- # A parsed pubid gives it from its components. A String may carry a
37
- # `doi:` scheme (in any case) or a `doi.org` URL: Relaton::Db keys every
38
- # form Pubid::Doi reads as one DOI, so all of them must reach the same
39
- # DOI here, or a miss on one form is cached for the others.
40
- #
41
- # @param [String, Pubid::Doi::Identifier] doi
42
- # @return [String]
34
+ # @see Relaton::Doi.doi_of
43
35
  #
44
36
  def doi_of(doi)
45
- return "#{doi.prefix}/#{doi.suffix}" unless doi.is_a?(String)
46
-
47
- doi.sub(%r{\A(?:doi:|https?://(?:dx\.)?doi\.org/)}i, "")
37
+ Doi.doi_of(doi)
48
38
  end
49
39
 
50
40
  #
@@ -0,0 +1,40 @@
1
+ module Relaton
2
+ module Doi
3
+ #
4
+ # Fetch a DataCite-registered DOI from the DataCite REST API
5
+ # (relaton-doi#20). The API is public and needs no key.
6
+ #
7
+ module Datacite
8
+ extend self
9
+
10
+ URL = "https://api.datacite.org/dois/%<doi>s".freeze
11
+
12
+ #
13
+ # @param [String, Pubid::Doi::Identifier] doi
14
+ #
15
+ # @return [Bib::ItemData, nil]
16
+ #
17
+ def get(doi)
18
+ key = doi.to_s
19
+ Util.info "Fetching from api.datacite.org ...", key: key
20
+ attributes = get_by_id(Doi.doi_of(doi))
21
+ if attributes
22
+ Util.info "Found: `#{attributes['doi']}`", key: key
23
+ DataciteParser.parse attributes
24
+ else
25
+ Util.info "Not found.", key: key
26
+ nil
27
+ end
28
+ end
29
+
30
+ #
31
+ # @param [String] id the bare DOI
32
+ #
33
+ # @return [Hash, nil] the record's `attributes`
34
+ #
35
+ def get_by_id(id)
36
+ Http.get_json(format(URL, doi: CGI.escape(id)), "DataCite")&.dig("data", "attributes")
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,301 @@
1
+ module Relaton
2
+ module Doi
3
+ #
4
+ # Map a DataCite record (the `data.attributes` of
5
+ # `GET api.datacite.org/dois/<doi>`, DataCite Metadata Schema 4) to a
6
+ # generic Bib::ItemData.
7
+ #
8
+ class DataciteParser # rubocop:disable Metrics/ClassLength
9
+ # DataCite `types.resourceTypeGeneral` → Relaton item type.
10
+ RESOURCE_TYPES = {
11
+ "Audiovisual" => "audiovisual",
12
+ "Book" => "book",
13
+ "BookChapter" => "inbook",
14
+ "ConferencePaper" => "inproceedings",
15
+ "ConferenceProceeding" => "proceedings",
16
+ "Dataset" => "dataset",
17
+ "Dissertation" => "thesis",
18
+ "Image" => "graphic_work",
19
+ "Journal" => "journal",
20
+ "JournalArticle" => "article",
21
+ "Report" => "techreport",
22
+ "Software" => "software",
23
+ "Standard" => "standard",
24
+ }.freeze
25
+
26
+ # DataCite `types.bibtex` → Relaton item type, for the general types
27
+ # the table above leaves open (`Text`, `Other`, …).
28
+ BIBTEX_TYPES = {
29
+ "article" => "article",
30
+ "book" => "book",
31
+ "inbook" => "inbook",
32
+ "incollection" => "incollection",
33
+ "inproceedings" => "inproceedings",
34
+ "manual" => "manual",
35
+ "mastersthesis" => "thesis",
36
+ "phdthesis" => "thesis",
37
+ "proceedings" => "proceedings",
38
+ "techreport" => "techreport",
39
+ "unpublished" => "unpublished",
40
+ }.freeze
41
+
42
+ # DataCite `dateType` → Relaton date type. The other DataCite types
43
+ # (Accepted, Available, Collected, Copyrighted, Submitted, Valid, …)
44
+ # have no Relaton counterpart.
45
+ DATE_TYPES = {
46
+ "Issued" => "issued",
47
+ "Created" => "created",
48
+ "Updated" => "updated",
49
+ }.freeze
50
+
51
+ # DataCite `relationType` → Relaton relation type. Any other type is
52
+ # `related`, with the DataCite type as the description.
53
+ RELATION_TYPES = {
54
+ "IsCitedBy" => "isCitedIn",
55
+ "Cites" => "cites",
56
+ "IsReferencedBy" => "isCitedIn",
57
+ "References" => "cites",
58
+ "IsSupplementTo" => "complementOf",
59
+ "IsSupplementedBy" => "hasComplement",
60
+ "IsContinuedBy" => "hasSuccessor",
61
+ "Continues" => "successorOf",
62
+ "IsDescribedBy" => "describedBy",
63
+ "Describes" => "describes",
64
+ "IsDocumentedBy" => "describedBy",
65
+ "Documents" => "describes",
66
+ "HasVersion" => "hasEdition",
67
+ "IsVersionOf" => "editionOf",
68
+ "IsNewVersionOf" => "updates",
69
+ "IsPreviousVersionOf" => "updatedBy",
70
+ "IsPartOf" => "partOf",
71
+ "HasPart" => "hasPart",
72
+ "IsPublishedIn" => "includedIn",
73
+ "IsDerivedFrom" => "derivedFrom",
74
+ "IsSourceOf" => "derives",
75
+ "IsReviewedBy" => "hasReview",
76
+ "Reviews" => "reviewOf",
77
+ "IsObsoletedBy" => "obsoletedBy",
78
+ "Obsoletes" => "obsoletes",
79
+ "IsRequiredBy" => "complementOf",
80
+ "Requires" => "hasComplement",
81
+ "IsTranslationOf" => "translatedFrom",
82
+ "HasTranslation" => "hasTranslation",
83
+ "IsVariantFormOf" => "manifestationOf",
84
+ "IsOriginalFormOf" => "hasManifestation",
85
+ }.freeze
86
+
87
+ # DataCite's standard values for an unknown property (`(:unav)`
88
+ # unavailable, `(:unkn)` unknown, …). They are not names.
89
+ PLACEHOLDER = /\A\(:[a-z]+\)\z/
90
+
91
+ ATTRS = %i[type fetched title docidentifier date source abstract
92
+ contributor language relation].freeze
93
+
94
+ #
95
+ # @param [Hash] src the record's `attributes`
96
+ #
97
+ # @return [Bib::ItemData]
98
+ #
99
+ def self.parse(src)
100
+ new(src).parse
101
+ end
102
+
103
+ def initialize(src)
104
+ @src = src
105
+ end
106
+
107
+ def parse
108
+ Bib::ItemData.new(**ATTRS.to_h { |a| [a, send("parse_#{a}")] })
109
+ end
110
+
111
+ private
112
+
113
+ def parse_type
114
+ types = @src["types"] || {}
115
+ RESOURCE_TYPES[types["resourceTypeGeneral"]] ||
116
+ BIBTEX_TYPES[types["bibtex"]] || "misc"
117
+ end
118
+
119
+ def parse_fetched
120
+ Date.today.to_s
121
+ end
122
+
123
+ def parse_title
124
+ Array(@src["titles"]).filter_map do |t|
125
+ next if t["title"].to_s.strip.empty?
126
+
127
+ type = t["titleType"] == "Subtitle" ? "subtitle" : "main"
128
+ Bib::Title.new(type: type, content: t["title"].strip, language: t["lang"])
129
+ end
130
+ end
131
+
132
+ def parse_docidentifier
133
+ [Bib::Docidentifier.new(type: "DOI", content: @src["doi"], primary: true)]
134
+ end
135
+
136
+ def parse_date
137
+ dates = Array(@src["dates"]).filter_map do |d|
138
+ type = DATE_TYPES[d["dateType"]]
139
+ create_date(type, d["date"].to_s.strip) if type
140
+ end
141
+ if dates.none? { |d| d.type == "issued" } && @src["publicationYear"]
142
+ dates.unshift Bib::Date.new(type: "issued", at: @src["publicationYear"].to_s)
143
+ end
144
+ dates
145
+ end
146
+
147
+ #
148
+ # A DataCite date is free text: a date, an RKMS-ISO8601 range
149
+ # (`2019-01-01/2020-02-02`, either end may be open), or prose
150
+ # (`Spring 2019`). A date Relaton cannot read is skipped, never raised.
151
+ #
152
+ def create_date(type, value)
153
+ return if value.empty?
154
+
155
+ if value.include?("/")
156
+ from, to = value.split("/", 2).map { |v| v.strip.empty? ? nil : v.strip }
157
+ Bib::Date.new(type: type, from: from, to: to) if from || to
158
+ else
159
+ Bib::Date.new(type: type, at: value)
160
+ end
161
+ rescue ::Date::Error, ArgumentError
162
+ nil
163
+ end
164
+
165
+ def parse_source
166
+ return [] unless @src["url"]
167
+
168
+ [Bib::Uri.new(type: "src", content: @src["url"])]
169
+ end
170
+
171
+ def parse_abstract
172
+ Array(@src["descriptions"]).filter_map do |d|
173
+ next unless d["descriptionType"] == "Abstract" && d["description"]
174
+
175
+ Bib::Abstract.new(content: d["description"], language: d["lang"])
176
+ end
177
+ end
178
+
179
+ def parse_language
180
+ Array(@src["language"])
181
+ end
182
+
183
+ def parse_contributor
184
+ contribs = Array(@src["creators"]).filter_map { |c| contributor(entity(c), "author") }
185
+ contribs += Array(@src["contributors"]).filter_map { |c| typed_contributor c }
186
+ contribs << contributor(create_org(publisher_name), "publisher") if publisher_name
187
+ contribs
188
+ end
189
+
190
+ def publisher_name
191
+ pub = @src["publisher"]
192
+ name = pub.is_a?(Hash) ? pub["name"] : pub
193
+ name unless name.nil? || name.match?(PLACEHOLDER)
194
+ end
195
+
196
+ # DataCite `contributorType` → Relaton role. The other types
197
+ # (ContactPerson, DataCollector, HostingInstitution, ProjectMember, …)
198
+ # have no Relaton role; an author role would make a citation name them
199
+ # as authors, so they are left out.
200
+ CONTRIBUTOR_ROLES = {
201
+ "Editor" => "editor",
202
+ "Distributor" => "distributor",
203
+ "Sponsor" => "enabler",
204
+ "RightsHolder" => "owner",
205
+ }.freeze
206
+
207
+ def typed_contributor(contrib)
208
+ role = CONTRIBUTOR_ROLES[contrib["contributorType"]]
209
+ contributor(entity(contrib), role) if role
210
+ end
211
+
212
+ def contributor(entity, type)
213
+ return unless entity
214
+
215
+ role = [Bib::Contributor::Role.new(type: type)]
216
+ if entity.is_a?(Bib::Person)
217
+ Bib::Contributor.new(role: role, person: entity)
218
+ else
219
+ Bib::Contributor.new(role: role, organization: entity)
220
+ end
221
+ end
222
+
223
+ #
224
+ # DataCite marks a name `Personal` or `Organizational`, but the type is
225
+ # optional. An untyped name is a person when it has name parts or reads
226
+ # `Family, Given`.
227
+ #
228
+ def entity(name)
229
+ if organization?(name)
230
+ create_org(name["name"]) unless name["name"].to_s.strip.empty?
231
+ elsif person_name_parts(name).first
232
+ create_person(name)
233
+ end
234
+ end
235
+
236
+ def organization?(name)
237
+ return true if name["nameType"] == "Organizational"
238
+ return false if name["nameType"] == "Personal"
239
+
240
+ !name["familyName"] && !name["name"].to_s.include?(",")
241
+ end
242
+
243
+ def create_person(name)
244
+ Bib::Person.new(
245
+ name: create_person_name(name),
246
+ affiliation: Array(name["affiliation"]).filter_map { |a| affiliation a },
247
+ identifier: orcid(name),
248
+ )
249
+ end
250
+
251
+ #
252
+ # @return [Array<String, nil>] the family and given names
253
+ #
254
+ def person_name_parts(name)
255
+ family = name["familyName"].to_s.strip
256
+ return [family, name["givenName"]&.strip] unless family.empty?
257
+
258
+ family, given = name["name"].to_s.split(",", 2).map(&:strip)
259
+ [family.to_s.empty? ? nil : family, given]
260
+ end
261
+
262
+ def create_person_name(name)
263
+ family, given = person_name_parts(name)
264
+ given = nil if given.to_s.empty?
265
+ Bib::FullName.new(
266
+ surname: Bib::LocalizedString.new(content: family),
267
+ forename: given ? [Bib::FullNameType::Forename.new(content: given)] : [],
268
+ )
269
+ end
270
+
271
+ def affiliation(aff)
272
+ org = aff.is_a?(Hash) ? aff["name"] : aff
273
+ Bib::Affiliation.new(organization: create_org(org)) if org
274
+ end
275
+
276
+ def orcid(name)
277
+ Array(name["nameIdentifiers"]).filter_map do |id|
278
+ next unless id["nameIdentifierScheme"]&.casecmp?("ORCID") && id["nameIdentifier"]
279
+
280
+ Bib::Person::Identifier.new(type: "orcid", content: id["nameIdentifier"])
281
+ end
282
+ end
283
+
284
+ def create_org(name)
285
+ Bib::Organization.new(name: [Bib::TypedLocalizedString.new(content: name)])
286
+ end
287
+
288
+ def parse_relation
289
+ Array(@src["relatedIdentifiers"]).filter_map do |rel|
290
+ next unless rel["relatedIdentifierType"] == "DOI" && rel["relatedIdentifier"]
291
+
292
+ docid = Bib::Docidentifier.new(type: "DOI", content: rel["relatedIdentifier"])
293
+ bibitem = Bib::ItemBase.new(docidentifier: [docid])
294
+ type = RELATION_TYPES[rel["relationType"]]
295
+ desc = Bib::LocalizedMarkedUpString.new(content: rel["relationType"]) unless type
296
+ Bib::Relation.new(type: type || "related", description: desc, bibitem: bibitem)
297
+ end
298
+ end
299
+ end
300
+ end
301
+ end