Class: Kanrisuru::Core::IP::Parser::Maddress

Inherits:
Base
  • Object
show all
Defined in:
lib/kanrisuru/core/ip/parsers/maddress.rb

Class Method Summary collapse

Methods inherited from Base

ip_address_label_result_json, ip_address_label_result_parse, parse_address_info, parse_alias, parse_ip_maddr_name, parse_ip_row, parse_link, parse_rx, parse_tx, parse_valid

Class Method Details

.ip_maddress_result_json(rows) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kanrisuru/core/ip/parsers/maddress.rb', line 23

def ip_maddress_result_json(rows)
  rows.map do |row|
    maddress = Kanrisuru::Core::IP::IPMAddress.new(row['ifindex'], row['ifname'], [])

    entries = row['maddr'] || []
    entries.each do |entry|
      ipmaddress_entry = Kanrisuru::Core::IP::IPMAddressEntry.new
      entry.each do |key, value|
        ipmaddress_entry[key] = value
      end

      maddress.maddr << ipmaddress_entry
    end

    maddress
  end
end

.ip_maddress_result_parse(lines) ⇒ Object



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
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kanrisuru/core/ip/parsers/maddress.rb', line 41

def ip_maddress_result_parse(lines)
  rows = []
  current_row = nil

  lines.each.with_index do |line, _index|
    case line
    when /^\d+:\s/
      rows << current_row unless current_row.nil?

      current_row = Kanrisuru::Core::IP::IPMAddress.new
      parse_ip_maddr_name(current_row, line)
    when /^link/
      _, link = line.split

      entry = Kanrisuru::Core::IP::IPMAddressEntry.new
      entry.link = link

      current_row.maddr << entry
    when /^inet/
      values = line.split

      entry = Kanrisuru::Core::IP::IPMAddressEntry.new
      values.each.with_index do |value, index|
        case value
        when 'inet', 'inet6'
          entry.family = value
          entry.address = values[index + 1]
        when 'users'
          entry.users = values[index + 1].to_i
        end
      end

      current_row.maddr << entry
    end
  end

  rows << current_row
  rows
end

.parse(command, action, version) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/kanrisuru/core/ip/parsers/maddress.rb', line 9

def parse(command, action, version)
  return unless %w[show list].include?(action)

  if version >= Kanrisuru::Core::IP::IPROUTE2_JSON_VERSION
    begin
      ip_maddress_result_json(command.to_json)
    rescue JSON::ParserError
      ip_maddress_result_parse(command.to_a)
    end
  else
    ip_maddress_result_parse(command.to_a)
  end
end