Class: Relaton::Iana::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton/iana/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml, rootdoc, errors = {}) ⇒ Parser

Document parser initalization

Parameters:

  • xml (Nokogiri::XML::Element)


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

Parameters:

  • xml (Nokogiri::XML::Element)

Returns:



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

#anchorString

Create anchor

Returns:

  • (String)

    anchor



92
93
94
# File 'lib/relaton/iana/parser.rb', line 92

def anchor
  docnumber.upcase.gsub("/", "__")
end

#contributorArray<Relaton::Bib::Contributor>

Create contributor

Returns:

  • (Array<Relaton::Bib::Contributor>)

    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

#docnumberString

Create docnumber

Returns:

  • (String)

    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

#parseRelaton::Iana::ItemData?

Parse document

Returns:



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_dateArray<Relaton::Bib::Date>

Parse date

Returns:

  • (Array<Relaton::Bib::Date>)

    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_docidArra<RelatonBib::DocumentIdentifier>

Parse docidentifier

Returns:

  • (Arra<RelatonBib::DocumentIdentifier>)

    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_sourceArray<Relaton::Bib::Uri>

Parse link

Returns:

  • (Array<Relaton::Bib::Uri>)

    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_titleArray<Relaton::Bib::Title>

Parse title

Returns:

  • (Array<Relaton::Bib::Title>)

    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_idString

Generate PubID

Returns:

  • (String)

    PubID



101
102
103
# File 'lib/relaton/iana/parser.rb', line 101

def pub_id
  "IANA #{docnumber}"
end