Class: Kanrisuru::Core::IP::Parser::Rule

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



23
24
25
26
27
28
29
30
31
# File 'lib/kanrisuru/core/ip/parsers/rule.rb', line 23

def ip_rule_result_json(rows)
  rows.map do |row|
    Kanrisuru::Core::IP::IPRule.new(
      row['priority'],
      row['src'],
      row['table']
    )
  end
end

.ip_rule_result_parse(lines) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kanrisuru/core/ip/parsers/rule.rb', line 33

def ip_rule_result_parse(lines)
  rows = []
  lines.each do |line|
    _, priority, string = line.split(/(\d+):\s/)
    values = string.split(/\s/)

    rule = Kanrisuru::Core::IP::IPRule.new(priority)

    values.each.with_index do |value, index|
      case value
      when 'from'
        rule.source = values[index + 1]
      when 'lookup'
        rule.table = values[index + 1]
      end
    end

    rows << rule
  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/rule.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_rule_result_json(command.to_json)
    rescue JSON::ParserError
      ip_rule_result_parse(command.to_a)
    end
  else
    ip_rule_result_parse(command.to_a)
  end
end