ip-ranges 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +1 -1
- data/lib/ip-ranges/range.rb +22 -4
- data/spec/ip-ranges/range_spec.rb +132 -44
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4114f152858982ac9340a1bbfccd71920bef9287
|
4
|
+
data.tar.gz: 0c836cc2bf31e3e252cd1f26b3c7d8bbd0aaa13d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9373cda93264ff8f9f8f9d2c8aa556399cced3e68bd6a4a7e1d399a526beb816e863833a6fc96cb27d5020bc7d7b74022275d786b396219b34090d41e542145b
|
7
|
+
data.tar.gz: 03596811b9ebc59e4b0eccd21726d224ecc481fe3ac9565e784e149cca4ce7c2121a7ba8c0b21cb6e379bac8a4782a0f5938a65638764a8471ccb7492b412d04
|
data/Rakefile
CHANGED
@@ -36,7 +36,7 @@ spec = Gem::Specification.new do |s|
|
|
36
36
|
|
37
37
|
# Change these as appropriate
|
38
38
|
s.name = "ip-ranges"
|
39
|
-
s.version = "0.3.
|
39
|
+
s.version = "0.3.2"
|
40
40
|
s.summary = "Compare and manipulate ranges of IP numbers"
|
41
41
|
s.author = "David Salgado"
|
42
42
|
s.email = "david@digitalronin.com"
|
data/lib/ip-ranges/range.rb
CHANGED
@@ -73,12 +73,10 @@ module IpRanges
|
|
73
73
|
|
74
74
|
# Arguments
|
75
75
|
# * An +Ip+ object
|
76
|
-
# Extend the range if the
|
76
|
+
# Extend the range if the supplied IP is the next, contiguous ip in the range. Otherwise return false and do nothing.
|
77
77
|
def push(ip)
|
78
78
|
if empty?
|
79
|
-
|
80
|
-
@last = ip
|
81
|
-
true
|
79
|
+
set_single_value(ip)
|
82
80
|
elsif ip.to_s == @last.dup.increment
|
83
81
|
@last = ip
|
84
82
|
true
|
@@ -87,6 +85,20 @@ module IpRanges
|
|
87
85
|
end
|
88
86
|
end
|
89
87
|
|
88
|
+
# Arguments
|
89
|
+
# * An +Ip+ object
|
90
|
+
# Extend the range at the start if the supplied IP is the previous, contiguous ip in the range. Otherwise return false and do nothing.
|
91
|
+
def unshift(ip)
|
92
|
+
if empty?
|
93
|
+
set_single_value(ip)
|
94
|
+
elsif ip.to_s == @first.dup.decrement
|
95
|
+
@first = ip
|
96
|
+
true
|
97
|
+
else
|
98
|
+
false
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
90
102
|
# Returns the last Ip object in this range.
|
91
103
|
def last
|
92
104
|
@last
|
@@ -137,6 +149,12 @@ module IpRanges
|
|
137
149
|
|
138
150
|
private
|
139
151
|
|
152
|
+
def set_single_value(ip)
|
153
|
+
@first = ip
|
154
|
+
@last = ip
|
155
|
+
true
|
156
|
+
end
|
157
|
+
|
140
158
|
def empty?
|
141
159
|
@first.nil? && @last.nil?
|
142
160
|
end
|
@@ -50,83 +50,171 @@ describe IpRanges::Range do
|
|
50
50
|
|
51
51
|
end
|
52
52
|
|
53
|
-
context "
|
53
|
+
context "extending" do
|
54
54
|
let(:ip1) { IpRanges::Ip.new(:number => '1.1.1.1') }
|
55
55
|
let(:ip2) { IpRanges::Ip.new(:number => '1.1.1.2') }
|
56
56
|
let(:ip3) { IpRanges::Ip.new(:number => '1.1.1.3') }
|
57
57
|
let(:ip4) { IpRanges::Ip.new(:number => '1.1.1.4') }
|
58
58
|
|
59
|
-
context "
|
60
|
-
|
59
|
+
context "unshifting" do
|
60
|
+
context "when range is empty" do
|
61
|
+
let(:empty) { described_class.new(:range => '') }
|
61
62
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
63
|
+
it "unshifts" do
|
64
|
+
res = empty.unshift(ip1)
|
65
|
+
expect(res).to be_truthy
|
66
|
+
end
|
66
67
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
68
|
+
it "sets first ip" do
|
69
|
+
expect {
|
70
|
+
empty.unshift(ip1)
|
71
|
+
}.to change(empty, :first).from(nil).to(ip1)
|
72
|
+
end
|
71
73
|
|
72
|
-
|
73
|
-
|
74
|
-
|
74
|
+
it "sets last ip" do
|
75
|
+
expect {
|
76
|
+
empty.unshift(ip1)
|
77
|
+
}.to change(empty, :last).from(nil).to(ip1)
|
78
|
+
end
|
75
79
|
end
|
76
|
-
end
|
77
80
|
|
78
|
-
|
79
|
-
|
81
|
+
context "when range has a start" do
|
82
|
+
let(:r1) { described_class.new(:range => ip2.to_s) }
|
80
83
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
84
|
+
context "unshifting previous ip" do
|
85
|
+
it "unshifts" do
|
86
|
+
res = r1.unshift(ip1)
|
87
|
+
expect(res).to be_truthy
|
88
|
+
end
|
89
|
+
|
90
|
+
it "doesn't affect last ip" do
|
91
|
+
expect {
|
92
|
+
r1.unshift(ip1)
|
93
|
+
}.to_not change(r1, :last).from(ip2)
|
94
|
+
end
|
85
95
|
end
|
86
96
|
|
87
|
-
|
88
|
-
|
89
|
-
|
97
|
+
context "unshifting non-contiguous ip" do
|
98
|
+
it "doesn't unshift" do
|
99
|
+
res = r1.unshift(ip4)
|
100
|
+
expect(res).to be_falsey
|
101
|
+
end
|
102
|
+
|
103
|
+
it "doesn't set first ip" do
|
104
|
+
expect {
|
105
|
+
r1.unshift(ip4)
|
106
|
+
}.to_not change(r1, :first).from(ip2)
|
107
|
+
end
|
90
108
|
end
|
91
109
|
end
|
92
110
|
|
93
|
-
context "
|
94
|
-
|
95
|
-
|
96
|
-
|
111
|
+
context "when range has start and end" do
|
112
|
+
let(:range) { described_class.new(:range => [ip2.to_s, ip3.to_s].join('..')) }
|
113
|
+
|
114
|
+
context "unshifting previous ip" do
|
115
|
+
it "unshifts" do
|
116
|
+
expect(range.unshift(ip1)).to be_truthy
|
117
|
+
end
|
118
|
+
|
119
|
+
it "sets first ip" do
|
120
|
+
expect {
|
121
|
+
range.unshift(ip1)
|
122
|
+
}.to change(range, :first).from(ip2).to(ip1)
|
123
|
+
end
|
97
124
|
end
|
98
125
|
|
99
|
-
|
100
|
-
|
101
|
-
|
126
|
+
context "unshifting non-contiguous ip" do
|
127
|
+
it "doesn't unshift" do
|
128
|
+
expect(range.unshift(ip4)).to be_falsey
|
129
|
+
end
|
130
|
+
|
131
|
+
it "doesn't set first ip" do
|
132
|
+
expect {
|
133
|
+
range.unshift(ip4)
|
134
|
+
}.to_not change(range, :first).from(ip2)
|
135
|
+
end
|
102
136
|
end
|
103
137
|
end
|
104
138
|
end
|
105
139
|
|
106
|
-
context "range with start and end" do
|
107
|
-
let(:range) { described_class.new(:range => [ip1.to_s, ip2.to_s].join('..')) }
|
108
140
|
|
109
|
-
|
141
|
+
context "pushing" do
|
142
|
+
context "when range is empty" do
|
143
|
+
let(:empty) { described_class.new(:range => '') }
|
144
|
+
|
110
145
|
it "pushes" do
|
111
|
-
res =
|
146
|
+
res = empty.push(ip1)
|
112
147
|
expect(res).to be_truthy
|
113
148
|
end
|
114
149
|
|
150
|
+
it "sets first ip" do
|
151
|
+
expect {
|
152
|
+
empty.push(ip1)
|
153
|
+
}.to change(empty, :first).from(nil).to(ip1)
|
154
|
+
end
|
155
|
+
|
115
156
|
it "sets last ip" do
|
116
|
-
|
117
|
-
|
157
|
+
expect {
|
158
|
+
empty.push(ip1)
|
159
|
+
}.to change(empty, :last).from(nil).to(ip1)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
context "when range has a start" do
|
164
|
+
let(:r1) { described_class.new(:range => ip1.to_s) }
|
165
|
+
|
166
|
+
context "pushing next ip" do
|
167
|
+
it "pushes" do
|
168
|
+
res = r1.push(ip2)
|
169
|
+
expect(res).to be_truthy
|
170
|
+
end
|
171
|
+
|
172
|
+
it "sets last ip" do
|
173
|
+
expect {
|
174
|
+
r1.push(ip2)
|
175
|
+
}.to change(r1, :last).from(ip1).to(ip2)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
context "pushing non-contiguous ip" do
|
180
|
+
it "doesn't push" do
|
181
|
+
res = r1.push(ip3)
|
182
|
+
expect(res).to be_falsey
|
183
|
+
end
|
184
|
+
|
185
|
+
it "doesn't set last ip" do
|
186
|
+
expect {
|
187
|
+
r1.push(ip3)
|
188
|
+
}.to_not change(r1, :last).from(ip1)
|
189
|
+
end
|
118
190
|
end
|
119
191
|
end
|
120
192
|
|
121
|
-
context "
|
122
|
-
|
123
|
-
|
124
|
-
|
193
|
+
context "when range has start and end" do
|
194
|
+
let(:range) { described_class.new(:range => [ip1.to_s, ip2.to_s].join('..')) }
|
195
|
+
|
196
|
+
context "pushing next ip" do
|
197
|
+
it "pushes" do
|
198
|
+
expect(range.push(ip3)).to be_truthy
|
199
|
+
end
|
200
|
+
|
201
|
+
it "sets last ip" do
|
202
|
+
expect {
|
203
|
+
range.push(ip3)
|
204
|
+
}.to change(range, :last).from(ip2).to(ip3)
|
205
|
+
end
|
125
206
|
end
|
126
207
|
|
127
|
-
|
128
|
-
|
129
|
-
|
208
|
+
context "pushing non-contiguous ip" do
|
209
|
+
it "doesn't push" do
|
210
|
+
expect(range.push(ip4)).to be_falsey
|
211
|
+
end
|
212
|
+
|
213
|
+
it "doesn't set last ip" do
|
214
|
+
expect {
|
215
|
+
range.push(ip4)
|
216
|
+
}.to_not change(range, :last).from(ip2)
|
217
|
+
end
|
130
218
|
end
|
131
219
|
end
|
132
220
|
end
|