Module: IANA::Protocol

Defined in:
lib/iana/protocol.rb

Defined Under Namespace

Classes: Protocol

Class Method Summary collapse

Class Method Details

.load(pathname) ⇒ Object

load IANA protocols list from XML file: www.iana.org/assignments/protocol-numbers/protocol-numbers.xml

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/iana/protocol.rb', line 16

def self.load(pathname)
  raise ArgumentError, 'nil pathname' if pathname.nil?
  raise ArgumentError, 'invalid pathname class' if pathname.class != String
  raise ArgumentError, 'empty pathname' if pathname.empty?

  protocols = {}
  updated = nil

  begin
    f = File.new(pathname, 'r')
    doc = Nokogiri::XML(f)
    updated = doc.css('registry/updated').text
    doc.css('registry/registry/record').each do |r|
      # range
      value = r.css('value').text
      if value =~ /-/ then
        low,high = value.split('-').map(&:to_i)
      else
        low = value.to_i
        high = low
      end

      # name
      name = r.css('name').text
      name = nil if !name.nil? && name.empty?

      # description
      description = r.css('description').text
      description = nil if !description.nil? && description.empty?

      # references
      xref = []
      r.css('xref').each do |x|
        data = x['data']
        xref << data if !data.nil? && !data.empty?
      end

      if low == high then
        protocols[low] = Protocol.new(name, description, xref)
      else
        # create an entry for each range element
        (high-low+1).times do |i|
          protocols[low+i] = Protocol.new(name, description, xref)
        end
      end
    end
  ensure
    f.close if !f.nil?
  end

  return protocols, updated
end