Class: RelatonIana::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) ⇒ Parser

Document parser initalization

Parameters:

  • xml (Nokogiri::XML::Element)


8
9
10
11
# File 'lib/relaton_iana/parser.rb', line 8

def initialize(xml, rootdoc)
  @xml = xml
  @rootdoc = rootdoc
end

Class Method Details

.parse(xml, rootdoc = nil) ⇒ RelatonIana::IanaBibliographicItem?

Initialize document parser and run it

Parameters:

  • xml (Nokogiri::XML::Element)

Returns:



20
21
22
# File 'lib/relaton_iana/parser.rb', line 20

def self.parse(xml, rootdoc = nil)
  new(xml, rootdoc).parse
end

Instance Method Details

#anchorString

Create anchor

Returns:

  • (String)

    anchor



83
84
85
# File 'lib/relaton_iana/parser.rb', line 83

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

#contributorArray<RelatonBib::Contribution>

Create contributor

Returns:

  • (Array<RelatonBib::Contribution>)

    contributor



124
125
126
127
128
129
130
# File 'lib/relaton_iana/parser.rb', line 124

def contributor
  org = RelatonBib::Organization.new(
    name: "Internet Assigned Numbers Authority", abbreviation: "IANA",
  )
  role = { type: "publisher" }
  [RelatonBib::ContributionInfo.new(entity: org, role: [role])]
end

#docnumberString

Create docnumber

Returns:

  • (String)

    docnumber



101
102
103
104
105
# File 'lib/relaton_iana/parser.rb', line 101

def docnumber
  dn = ""
  dn += "#{@rootdoc.docnumber}/" if @rootdoc
  dn + @xml["id"]
end

#parseRelatonIana::IanaBibliographicItem?

Parse document

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/relaton_iana/parser.rb', line 29

def parse # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  return unless @xml

  RelatonIana::IanaBibliographicItem.new(
    type: "standard",
    language: ["en"],
    script: ["Latn"],
    title: parse_title,
    link: parse_link,
    docid: parse_docid,
    docnumber: docnumber,
    date: parse_date,
    contributor: contributor,
  )
end

#parse_dateArray<RelatonBib::BibliographicDate>

Parse date

Returns:

  • (Array<RelatonBib::BibliographicDate>)

    date



112
113
114
115
116
117
# File 'lib/relaton_iana/parser.rb', line 112

def parse_date
  d = @xml.xpath("./xmlns:created|./xmlns:published|./xmlns:updated").map do |d|
    RelatonBib::BibliographicDate.new(type: d.name, on: d.text)
  end
  d.none? && @rootdoc ? @rootdoc.date : d
end

#parse_docidArra<RelatonBib::DocumentIdentifier>

Parse docidentifier

Returns:

  • (Arra<RelatonBib::DocumentIdentifier>)

    docidentifier



74
75
76
# File 'lib/relaton_iana/parser.rb', line 74

def parse_docid
  [RelatonBib::DocumentIdentifier.new(type: "IANA", id: pub_id, primary: true)]
end

Parse link

Returns:

  • (Array<RelatonBib::TypedUri>)

    link



61
62
63
64
65
66
67
# File 'lib/relaton_iana/parser.rb', line 61

def parse_link
  if @rootdoc then @rootdoc.link
  else
    uri = URI.join "https://www.iana.org/assignments/", @xml[:id]
    [RelatonBib::TypedUri.new(type: "self", content: uri.to_s)]
  end
end

#parse_titleRelatonBib::TypedTitleStringCollection

Parse title

Returns:

  • (RelatonBib::TypedTitleStringCollection)

    title



50
51
52
53
54
# File 'lib/relaton_iana/parser.rb', line 50

def parse_title
  content = @xml.at("./xmlns:title")&.text || @xml[:id]
  t = RelatonBib::TypedTitleString.new content: content
  RelatonBib::TypedTitleStringCollection.new [t]
end

#pub_idString

Generate PubID

Returns:

  • (String)

    PubID



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

def pub_id
  "IANA #{docnumber}"
end