iplogic 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/iplogic.gemspec +8 -6
- data/lib/iplogic/cidr.rb +10 -1
- data/lib/iplogic/ip.rb +4 -0
- data/spec/cidr_spec.rb +126 -0
- data/spec/ip_spec.rb +0 -116
- metadata +9 -6
data/iplogic.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'iplogic'
|
3
|
-
s.version = '0.1.
|
3
|
+
s.version = '0.1.1'
|
4
4
|
|
5
5
|
s.authors = ["Jay Adkisson"]
|
6
6
|
s.date = '2010-11-13'
|
@@ -9,10 +9,11 @@ Gem::Specification.new do |s|
|
|
9
9
|
desc
|
10
10
|
|
11
11
|
s.email = %q{jay@causes.com}
|
12
|
-
s.extra_rdoc_files =
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
s.extra_rdoc_files = %w(
|
13
|
+
LICENSE
|
14
|
+
README.md
|
15
|
+
)
|
16
|
+
|
16
17
|
s.files = %w(
|
17
18
|
Rakefile
|
18
19
|
LICENSE
|
@@ -35,10 +36,11 @@ Gem::Specification.new do |s|
|
|
35
36
|
|
36
37
|
s.test_files = %w(
|
37
38
|
spec/ip_spec.rb
|
39
|
+
spec/cidr_spec.rb
|
38
40
|
spec/spec_helper.rb
|
39
41
|
spec/radix_spec.rb
|
40
42
|
)
|
41
43
|
|
42
|
-
s.add_development_dependency('rspec')
|
44
|
+
s.add_development_dependency('rspec', '~> 1.0')
|
43
45
|
end
|
44
46
|
|
data/lib/iplogic/cidr.rb
CHANGED
@@ -54,7 +54,7 @@ module IPLogic
|
|
54
54
|
end
|
55
55
|
|
56
56
|
def inspect
|
57
|
-
"#<CIDR [ #{
|
57
|
+
"#<CIDR [ #{self} ]>"
|
58
58
|
end
|
59
59
|
|
60
60
|
def netmask
|
@@ -67,6 +67,10 @@ module IPLogic
|
|
67
67
|
@size ||= (1 << inv_bits)
|
68
68
|
end
|
69
69
|
|
70
|
+
def include?(ip)
|
71
|
+
IP.wrap(ip).min(bits) == min
|
72
|
+
end
|
73
|
+
|
70
74
|
def min
|
71
75
|
@min ||= IP.wrap(
|
72
76
|
(ip.to_i >> inv_bits) << inv_bits
|
@@ -87,6 +91,11 @@ module IPLogic
|
|
87
91
|
end
|
88
92
|
alias rest rest_field
|
89
93
|
|
94
|
+
def to_s
|
95
|
+
"#{ip}/#{bits}"
|
96
|
+
end
|
97
|
+
alias to_str to_s
|
98
|
+
|
90
99
|
def significant_octets
|
91
100
|
4 - (bits / 8)
|
92
101
|
end
|
data/lib/iplogic/ip.rb
CHANGED
data/spec/cidr_spec.rb
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe CIDR do
|
4
|
+
it "parses an IP-ish and netmask-ish" do
|
5
|
+
r = CIDR('4.33.222.111', '255.255.240.0')
|
6
|
+
r.should be_a CIDR
|
7
|
+
r.inspect.should include '4.33.222.111/20'
|
8
|
+
|
9
|
+
r = CIDR(IP('4.33.222.111'), 20)
|
10
|
+
r.should be_a CIDR
|
11
|
+
r.inspect.should include '4.33.222.111/20'
|
12
|
+
|
13
|
+
r = CIDR('4.33.222.111', IP('255.255.240.0'))
|
14
|
+
r.should be_a CIDR
|
15
|
+
r.inspect.should include '4.33.222.111/20'
|
16
|
+
end
|
17
|
+
|
18
|
+
it "parses slash notation" do
|
19
|
+
r = CIDR('11.22.33.44/8')
|
20
|
+
r.should be_a CIDR
|
21
|
+
r.inspect.should include '11.22.33.44/8'
|
22
|
+
end
|
23
|
+
|
24
|
+
it "knows its bits" do
|
25
|
+
i = rand(33)
|
26
|
+
CIDR("1.1.1.1/#{i}").bits.
|
27
|
+
should == i
|
28
|
+
end
|
29
|
+
|
30
|
+
it "knows its ip" do
|
31
|
+
CIDR('11.22.33.44/20').ip.
|
32
|
+
should == IP('11.22.33.44')
|
33
|
+
end
|
34
|
+
|
35
|
+
it "knows its netmask" do
|
36
|
+
CIDR('11.22.33.44/20').netmask.
|
37
|
+
should == IP('255.255.240.0')
|
38
|
+
|
39
|
+
CIDR('11.22.33.44/8').netmask.
|
40
|
+
should == IP('255.0.0.0')
|
41
|
+
|
42
|
+
CIDR('11.22.33.44/32').netmask.
|
43
|
+
should == IP('255.255.255.255')
|
44
|
+
|
45
|
+
CIDR('1.1.1.1/0').netmask.
|
46
|
+
should == IP('0.0.0.0')
|
47
|
+
end
|
48
|
+
|
49
|
+
it "knows its min" do
|
50
|
+
CIDR('11.22.33.44/20').min.
|
51
|
+
should == IP('11.22.32.0')
|
52
|
+
|
53
|
+
CIDR('11.22.33.44/8').min.
|
54
|
+
should == IP('11.0.0.0')
|
55
|
+
|
56
|
+
CIDR('11.22.33.44/32').min.
|
57
|
+
should == IP('11.22.33.44')
|
58
|
+
|
59
|
+
CIDR('11.22.33.44/0').min.
|
60
|
+
should == IP('0.0.0.0')
|
61
|
+
end
|
62
|
+
|
63
|
+
it "knows its max" do
|
64
|
+
CIDR('11.22.33.44/20').max.
|
65
|
+
should == IP('11.22.47.255')
|
66
|
+
|
67
|
+
CIDR('11.22.33.44/8').max.
|
68
|
+
should == IP('11.255.255.255')
|
69
|
+
|
70
|
+
CIDR('11.22.33.44/32').max.
|
71
|
+
should == IP('11.22.33.44')
|
72
|
+
|
73
|
+
CIDR('11.22.33.44/0').max.
|
74
|
+
should == IP('255.255.255.255')
|
75
|
+
end
|
76
|
+
|
77
|
+
it "knows its rest field" do
|
78
|
+
CIDR('11.22.33.44/20').rest_field.
|
79
|
+
should == IP('0.0.1.44')
|
80
|
+
|
81
|
+
CIDR('11.22.33.44/8').rest_field.
|
82
|
+
should == IP('0.22.33.44')
|
83
|
+
|
84
|
+
CIDR('11.22.33.44/32').rest_field.
|
85
|
+
should == IP('0.0.0.0')
|
86
|
+
|
87
|
+
CIDR('11.22.33.44/0').rest_field.
|
88
|
+
should == IP('11.22.33.44')
|
89
|
+
end
|
90
|
+
|
91
|
+
it "knows its size" do
|
92
|
+
CIDR('11.22.33.44/20').size.
|
93
|
+
should == 0x1000
|
94
|
+
|
95
|
+
CIDR('11.22.33.44/8').size.
|
96
|
+
should == 0x1000000
|
97
|
+
|
98
|
+
CIDR('11.22.33.44/32').size.
|
99
|
+
should == 1
|
100
|
+
|
101
|
+
CIDR('11.22.33.44/0').size.
|
102
|
+
should == 0x100000000
|
103
|
+
end
|
104
|
+
|
105
|
+
it "is enumerable" do
|
106
|
+
r = CIDR('11.22.33.44/24')
|
107
|
+
r.should respond_to :each
|
108
|
+
CIDR.included_modules.should include Enumerable
|
109
|
+
|
110
|
+
a = r.to_a
|
111
|
+
|
112
|
+
a.size.should == r.size
|
113
|
+
a.first.should == r.first
|
114
|
+
a.last.should == r.last
|
115
|
+
end
|
116
|
+
|
117
|
+
it "tests inclusion" do
|
118
|
+
r = CIDR('11.22.33.44/24')
|
119
|
+
r.should respond_to :include?
|
120
|
+
r.should include '11.22.33.0'
|
121
|
+
r.should include IP('11.22.33.255')
|
122
|
+
r.should_not include IP('11.22.32.44').to_i
|
123
|
+
r.should_not include '11.22.34.44'
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
data/spec/ip_spec.rb
CHANGED
@@ -103,120 +103,4 @@ describe IP do
|
|
103
103
|
IP('1.2.3.4').should_not be_netmask
|
104
104
|
IP('0.255.0.0').should_not be_netmask
|
105
105
|
end
|
106
|
-
|
107
|
-
describe CIDR do
|
108
|
-
it "parses an IP-ish and netmask-ish" do
|
109
|
-
r = CIDR('4.33.222.111', '255.255.240.0')
|
110
|
-
r.should be_a CIDR
|
111
|
-
r.inspect.should include '4.33.222.111/20'
|
112
|
-
|
113
|
-
r = CIDR(IP('4.33.222.111'), 20)
|
114
|
-
r.should be_a CIDR
|
115
|
-
r.inspect.should include '4.33.222.111/20'
|
116
|
-
|
117
|
-
r = CIDR('4.33.222.111', IP('255.255.240.0'))
|
118
|
-
r.should be_a CIDR
|
119
|
-
r.inspect.should include '4.33.222.111/20'
|
120
|
-
end
|
121
|
-
|
122
|
-
it "parses slash notation" do
|
123
|
-
r = CIDR('11.22.33.44/8')
|
124
|
-
r.should be_a CIDR
|
125
|
-
r.inspect.should include '11.22.33.44/8'
|
126
|
-
end
|
127
|
-
|
128
|
-
it "knows its bits" do
|
129
|
-
i = rand(33)
|
130
|
-
CIDR("1.1.1.1/#{i}").bits.
|
131
|
-
should == i
|
132
|
-
end
|
133
|
-
|
134
|
-
it "knows its ip" do
|
135
|
-
CIDR('11.22.33.44/20').ip.
|
136
|
-
should == IP('11.22.33.44')
|
137
|
-
end
|
138
|
-
|
139
|
-
it "knows its netmask" do
|
140
|
-
CIDR('11.22.33.44/20').netmask.
|
141
|
-
should == IP('255.255.240.0')
|
142
|
-
|
143
|
-
CIDR('11.22.33.44/8').netmask.
|
144
|
-
should == IP('255.0.0.0')
|
145
|
-
|
146
|
-
CIDR('11.22.33.44/32').netmask.
|
147
|
-
should == IP('255.255.255.255')
|
148
|
-
|
149
|
-
CIDR('1.1.1.1/0').netmask.
|
150
|
-
should == IP('0.0.0.0')
|
151
|
-
end
|
152
|
-
|
153
|
-
it "knows its min" do
|
154
|
-
CIDR('11.22.33.44/20').min.
|
155
|
-
should == IP('11.22.32.0')
|
156
|
-
|
157
|
-
CIDR('11.22.33.44/8').min.
|
158
|
-
should == IP('11.0.0.0')
|
159
|
-
|
160
|
-
CIDR('11.22.33.44/32').min.
|
161
|
-
should == IP('11.22.33.44')
|
162
|
-
|
163
|
-
CIDR('11.22.33.44/0').min.
|
164
|
-
should == IP('0.0.0.0')
|
165
|
-
end
|
166
|
-
|
167
|
-
it "knows its max" do
|
168
|
-
CIDR('11.22.33.44/20').max.
|
169
|
-
should == IP('11.22.47.255')
|
170
|
-
|
171
|
-
CIDR('11.22.33.44/8').max.
|
172
|
-
should == IP('11.255.255.255')
|
173
|
-
|
174
|
-
CIDR('11.22.33.44/32').max.
|
175
|
-
should == IP('11.22.33.44')
|
176
|
-
|
177
|
-
CIDR('11.22.33.44/0').max.
|
178
|
-
should == IP('255.255.255.255')
|
179
|
-
end
|
180
|
-
|
181
|
-
it "knows its rest field" do
|
182
|
-
CIDR('11.22.33.44/20').rest_field.
|
183
|
-
should == IP('0.0.1.44')
|
184
|
-
|
185
|
-
CIDR('11.22.33.44/8').rest_field.
|
186
|
-
should == IP('0.22.33.44')
|
187
|
-
|
188
|
-
CIDR('11.22.33.44/32').rest_field.
|
189
|
-
should == IP('0.0.0.0')
|
190
|
-
|
191
|
-
CIDR('11.22.33.44/0').rest_field.
|
192
|
-
should == IP('11.22.33.44')
|
193
|
-
end
|
194
|
-
|
195
|
-
it "knows its size" do
|
196
|
-
CIDR('11.22.33.44/20').size.
|
197
|
-
should == 0x1000
|
198
|
-
|
199
|
-
CIDR('11.22.33.44/8').size.
|
200
|
-
should == 0x1000000
|
201
|
-
|
202
|
-
CIDR('11.22.33.44/32').size.
|
203
|
-
should == 1
|
204
|
-
|
205
|
-
CIDR('11.22.33.44/0').size.
|
206
|
-
should == 0x100000000
|
207
|
-
end
|
208
|
-
|
209
|
-
it "is enumerable" do
|
210
|
-
r = CIDR('11.22.33.44/24')
|
211
|
-
r.should respond_to :each
|
212
|
-
CIDR.included_modules.should include Enumerable
|
213
|
-
|
214
|
-
a = r.to_a
|
215
|
-
|
216
|
-
a.size.should == r.size
|
217
|
-
a.first.should == r.first
|
218
|
-
a.last.should == r.last
|
219
|
-
end
|
220
|
-
|
221
|
-
end
|
222
106
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: iplogic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jay Adkisson
|
@@ -24,12 +24,13 @@ dependencies:
|
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
26
|
requirements:
|
27
|
-
- -
|
27
|
+
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
hash:
|
29
|
+
hash: 15
|
30
30
|
segments:
|
31
|
+
- 1
|
31
32
|
- 0
|
32
|
-
version: "0"
|
33
|
+
version: "1.0"
|
33
34
|
type: :development
|
34
35
|
version_requirements: *id001
|
35
36
|
description: An IPv4 swiss-army chainsaw
|
@@ -52,6 +53,7 @@ files:
|
|
52
53
|
- lib/iplogic/cidr.rb
|
53
54
|
- lib/iplogic/ip.rb
|
54
55
|
- spec/ip_spec.rb
|
56
|
+
- spec/cidr_spec.rb
|
55
57
|
- spec/spec_helper.rb
|
56
58
|
- spec/radix_spec.rb
|
57
59
|
has_rdoc: true
|
@@ -90,5 +92,6 @@ specification_version: 3
|
|
90
92
|
summary: Because it's just a 32-bit integer.
|
91
93
|
test_files:
|
92
94
|
- spec/ip_spec.rb
|
95
|
+
- spec/cidr_spec.rb
|
93
96
|
- spec/spec_helper.rb
|
94
97
|
- spec/radix_spec.rb
|