Class: Kanrisuru::Core::IP::Parser::Address

Inherits:
Base
  • Object
show all
Defined in:
lib/kanrisuru/core/ip/parsers/address.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_address_result_json(rows) ⇒ Object



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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/kanrisuru/core/ip/parsers/address.rb', line 53

def ip_address_result_json(rows)
  result = []
  rows.each do |row|
    next if row['ifindex'].instance_of?(NilClass)

    new_row = Kanrisuru::Core::IP::IPAddressProperty.new(
      row['ifindex'], row['ifname'], row['flags'], row['mtu'], row['qdisc'],
      row['operstate'], row['group'], row['txqlen'], row['link_type'], row['address'], []
    )

    if row.key?('stats64')
      rx = row['stats64']['rx']
      tx = row['stats64']['tx']

      new_row[:stats] = Kanrisuru::Core::IP::IPStats.new(
        Kanrisuru::Core::IP::IPStatRX.new(
          rx['bytes'], rx['packets'], rx['errors'],
          rx['dropped'], rx['over_errors'], rx['multicast']
        ),
        Kanrisuru::Core::IP::IPStatTX.new(
          tx['bytes'], tx['packets'], tx['errors'],
          tx['dropped'], tx['carrier_errors'], tx['collisions']
        )
      )
    end

    addr_info = row['addr_info'] || []

    new_row[:address_info] = addr_info.map do |address|
      dynamic = address['dynamic'] == true || address['dynamic'] == 'true'

      Kanrisuru::Core::IP::IPAddressInfo.new(
        address['family'],
        IPAddr.new(address['local']),
        address['broadcast'],
        address['scope'],
        dynamic,
        address['valid_life_time'], address['preferred_life_time']
      )
    end

    result << new_row
  end

  result
end

.ip_address_result_parse(lines) ⇒ Object



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
# File 'lib/kanrisuru/core/ip/parsers/address.rb', line 23

def ip_address_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::IPAddressProperty.new
      current_row.address_info = []

      parse_ip_row(current_row, line)
    when /^link/
      parse_link(current_row, line)
    when /^inet/
      parse_address_info(current_row, line)
    when /^valid_lft/
      parse_valid(current_row, line)
    when /^RX:/
      parse_rx(current_row, lines[index + 1])
    when /^TX:/
      parse_tx(current_row, lines[index + 1])
    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/address.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_address_result_json(command.to_json)
    rescue JSON::ParserError
      ip_address_result_parse(command.to_a)
    end
  else
    ip_address_result_parse(command.to_a)
  end
end