Class: Kanrisuru::Core::IP::Parser::Route

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



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

def ip_route_result_json(rows)
  rows.map do |row|
    Kanrisuru::Core::IP::IPRoute.new(
      row['dst'],
      row['gateway'],
      row['dev'],
      row['protocol'],
      row['scope'],
      row['prefsrc'],
      row['metric'],
      row['flags']
    )
  end
end

.ip_route_result_parse(lines) ⇒ Object



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
68
69
70
# File 'lib/kanrisuru/core/ip/parsers/route.rb', line 38

def ip_route_result_parse(lines)
  rows = []

  lines.map do |line|
    values = line.split(/\s/)

    ip_route = Kanrisuru::Core::IP::IPRoute.new(values[0])
    ip_route.flags = []

    values.each.with_index do |value, index|
      case value
      when 'via'
        ip_route.gateway = values[index + 1]
      when 'dev'
        ip_route.device = values[index + 1]
      when 'proto'
        ip_route.protocol = values[index + 1]
      when 'src'
        ip_route.preferred_source = values[index + 1]
      when 'scope'
        ip_route.scope = values[index + 1]
      when 'flags'
        ip_route.flags = values[index + 1]
      when 'metric'
        ip_route.metric = values[index + 1]
      end
    end

    rows << ip_route
  end

  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/route.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_route_result_json(command.to_json)
    rescue JSON::ParserError
      ip_route_result_parse(command.to_a)
    end
  else
    ip_route_result_parse(command.to_a)
  end
end