net-ip 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/net/ip/route.rb CHANGED
@@ -1,28 +1,86 @@
1
1
  module Net
2
2
  module IP
3
3
  class Route
4
- def initialize(data)
5
- parts = data.split(" ")
6
- @dest = parts.first
7
- parts.each do |part|
8
- name, value = part.split(" ")
9
- if name == "metric"
10
- @metric = value
11
- elsif name == "scope"
12
- @scope = value
13
- elsif name == "proto"
14
- @proto = value
15
- elsif name == "src"
16
- @src = value
17
- end
18
- end
4
+ attr_accessor :type, :prefix, :dev, :scope, :metric,
5
+ :proto, :src, :via, :weight, :table, :error
6
+
7
+ def to_s
8
+ str = ""
9
+ str << type << " " if type != "unicast"
10
+ str << prefix << " "
11
+
12
+ str << "via #{via} " if via
13
+
14
+ str << "dev #{dev} " if dev
15
+
16
+ str << " table #{table} " if table
17
+ str << " proto #{proto} " if proto
18
+ str << " scope #{scope} " if scope
19
+
20
+ str << " src #{src} " if src
21
+ str << " metric #{metric} " if metric
22
+
23
+ str << " error #{error}" if error
24
+
25
+ str
19
26
  end
20
27
 
21
28
  def self.all
22
- `ip route`.split("\n").collect {|line| new(line)}
29
+ parse(`ip route show table 0`)
23
30
  end
24
31
 
25
32
  private
33
+
34
+ def self.parse_line(line)
35
+ route = new
36
+
37
+ if line =~ /^(unicast|unreachable|blackhole|prohibit|local|broadcast|throw|nat|via|anycast|multicast)\s+/
38
+ route.type = $1
39
+ line = line[$1.length..-1]
40
+ else
41
+ route.type = "unicast"
42
+ end
43
+
44
+ route.prefix = line.split.first
45
+ route.dev = $1 if line =~ /\s+dev\s+([^\s]+)/
46
+ route.scope = $1 if line =~ /\s+scope\s+([^\s]+)/
47
+ route.metric = $1 if line =~ /\s+metric\s+([^\s]+)/
48
+ route.proto = $1 if line =~ /\s+proto\s+([^\s]+)/
49
+ route.src = $1 if line =~ /\s+src\s+([^\s]+)/
50
+ route.via = $1 if line =~ /\s+via\s+([^\s]+)/
51
+ route.weight = $1 if line =~ /\s+weight\s+([^\s]+)/
52
+ route.table = $1 if line =~ /\s+table\s+([^\s]+)/
53
+ route.error = $1 if line =~ /\s+error\s+([^\s]+)/
54
+
55
+ route
56
+ end
57
+
58
+ def self.parse_data(data, &block)
59
+ in_default = false
60
+ data.each_line do |line|
61
+ if in_default == true
62
+ if line.start_with? "\t"
63
+ yield(parse_line(line.strip.gsub("nexthop", "default")))
64
+ else
65
+ in_default = false
66
+ end
67
+ elsif line.strip == "default"
68
+ in_default = true
69
+ end
70
+
71
+ unless in_default
72
+ yield(parse_line(line.strip))
73
+ end
74
+ end
75
+ end
76
+
77
+ def self.parse(data)
78
+ list = []
79
+ parse_data(data) do |route|
80
+ list << route
81
+ end
82
+ list
83
+ end
26
84
  end
27
85
  end
28
86
  end
@@ -1,5 +1,5 @@
1
1
  module Net
2
2
  module IP
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
data/net-ip.gemspec CHANGED
@@ -19,4 +19,5 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_development_dependency "bundler", "~> 1.3"
21
21
  spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec"
22
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: net-ip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-07 00:00:00.000000000 Z
12
+ date: 2013-08-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
46
62
  description:
47
63
  email:
48
64
  - jbussdieker@gmail.com
@@ -74,7 +90,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
74
90
  version: '0'
75
91
  segments:
76
92
  - 0
77
- hash: 1964098260122734981
93
+ hash: 898382037184904844
78
94
  required_rubygems_version: !ruby/object:Gem::Requirement
79
95
  none: false
80
96
  requirements:
@@ -83,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
99
  version: '0'
84
100
  segments:
85
101
  - 0
86
- hash: 1964098260122734981
102
+ hash: 898382037184904844
87
103
  requirements: []
88
104
  rubyforge_project:
89
105
  rubygems_version: 1.8.25