Class: Relaton::Iana::Parser
- Inherits:
-
Object
- Object
- Relaton::Iana::Parser
- Defined in:
- lib/relaton/iana/parser.rb
Class Method Summary collapse
-
.parse(xml, rootdoc = nil, errors = {}) ⇒ Relaton::Iana::ItemData?
Initialize document parser and run it.
Instance Method Summary collapse
-
#anchor ⇒ String
Create anchor.
-
#contributor ⇒ Array<Relaton::Bib::Contributor>
Create contributor.
-
#docnumber ⇒ String
Create docnumber.
-
#initialize(xml, rootdoc, errors = {}) ⇒ Parser
constructor
Document parser initalization.
-
#parse ⇒ Relaton::Iana::ItemData?
Parse document.
-
#parse_date ⇒ Array<Relaton::Bib::Date>
Parse date.
-
#parse_docid ⇒ Arra<RelatonBib::DocumentIdentifier>
Parse docidentifier.
-
#parse_source ⇒ Array<Relaton::Bib::Uri>
Parse link.
-
#parse_title ⇒ Array<Relaton::Bib::Title>
Parse title.
-
#pub_id ⇒ String
Generate PubID.
Constructor Details
#initialize(xml, rootdoc, errors = {}) ⇒ Parser
Document parser initalization
9 10 11 12 13 |
# File 'lib/relaton/iana/parser.rb', line 9 def initialize(xml, rootdoc, errors = {}) @xml = xml @rootdoc = rootdoc @errors = errors end |
Class Method Details
.parse(xml, rootdoc = nil, errors = {}) ⇒ Relaton::Iana::ItemData?
Initialize document parser and run it
22 23 24 |
# File 'lib/relaton/iana/parser.rb', line 22 def self.parse(xml, rootdoc = nil, errors = {}) new(xml, rootdoc, errors).parse end |
Instance Method Details
#anchor ⇒ String
Create anchor
92 93 94 |
# File 'lib/relaton/iana/parser.rb', line 92 def anchor docnumber.upcase.gsub("/", "__") end |
#contributor ⇒ Array<Relaton::Bib::Contributor>
Create contributor
137 138 139 140 141 142 143 |
# File 'lib/relaton/iana/parser.rb', line 137 def contributor orgname = Bib::TypedLocalizedString.new(content: "Internet Assigned Numbers Authority") abbrev = Bib::LocalizedString.new(content: "IANA") org = Bib::Organization.new(name: [orgname], abbreviation: abbrev) role = Bib::Contributor::Role.new(type: "publisher") [Bib::Contributor.new(organization: org, role: [role])] end |
#docnumber ⇒ String
Create docnumber
110 111 112 113 114 115 116 |
# File 'lib/relaton/iana/parser.rb', line 110 def docnumber dn = "" dn += "#{@rootdoc.docnumber}/" if @rootdoc id = @xml["id"].to_s @errors[:docnumber] &&= id.empty? dn + id end |
#parse ⇒ Relaton::Iana::ItemData?
Parse document
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/relaton/iana/parser.rb', line 31 def parse # rubocop:disable Metrics/MethodLength, Metrics/AbcSize return unless @xml Relaton::Iana::ItemData.new( type: "standard", language: ["en"], script: ["Latn"], title: parse_title, source: parse_source, docidentifier: parse_docid, docnumber: docnumber, date: parse_date, contributor: contributor, ) end |
#parse_date ⇒ Array<Relaton::Bib::Date>
Parse date
123 124 125 126 127 128 129 130 |
# File 'lib/relaton/iana/parser.rb', line 123 def parse_date d = @xml.xpath("./xmlns:created|./xmlns:published|./xmlns:updated").map do |dt| Bib::Date.new(type: dt.name, at: dt.text) end result = d.none? && @rootdoc ? @rootdoc.date : d @errors[:date] &&= result.empty? result end |
#parse_docid ⇒ Arra<RelatonBib::DocumentIdentifier>
Parse docidentifier
81 82 83 84 85 |
# File 'lib/relaton/iana/parser.rb', line 81 def parse_docid result = [Bib::Docidentifier.new(type: "IANA", content: pub_id, primary: true)] @errors[:docid] &&= result.empty? result end |
#parse_source ⇒ Array<Relaton::Bib::Uri>
Parse link
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/relaton/iana/parser.rb', line 64 def parse_source result = if @rootdoc then @rootdoc.source elsif @xml[:id] uri = URI.join "https://www.iana.org/assignments/", @xml[:id] [Bib::Uri.new(type: "src", content: uri.to_s)] else [] end @errors[:source] &&= result.empty? result end |
#parse_title ⇒ Array<Relaton::Bib::Title>
Parse title
52 53 54 55 56 57 |
# File 'lib/relaton/iana/parser.rb', line 52 def parse_title content = @xml.at("./xmlns:title")&.text || @xml[:id] result = [Bib::Title.new(content: content, language: "en", script: "Latn")] @errors[:title] &&= result.empty? result end |
#pub_id ⇒ String
Generate PubID
101 102 103 |
# File 'lib/relaton/iana/parser.rb', line 101 def pub_id "IANA #{docnumber}" end |