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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 758de3bb2dd52b835b7fbfaebc9be527d5ea637b
4
- data.tar.gz: 7cec73e1e6aed0dfc5205acfc760b6c9bdbb6434
3
+ metadata.gz: 4114f152858982ac9340a1bbfccd71920bef9287
4
+ data.tar.gz: 0c836cc2bf31e3e252cd1f26b3c7d8bbd0aaa13d
5
5
  SHA512:
6
- metadata.gz: d868e062f491f696d7448e765e1b1722e3b844978594fcb0ff425650441a7b587bbafe08ab2b8a9af69bddd6eac8fb543500e7d9cfaedcb61897958ff0bcaa3d
7
- data.tar.gz: 55a8d58e43c4658ae556fe2416867884e2c5577d9cb53a6018a229c56e2158e45ffd3e199192f211d3ecd2680480a0157e2d929431e0d48acec5aea1b8fdf788
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.1"
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"
@@ -73,12 +73,10 @@ module IpRanges
73
73
 
74
74
  # Arguments
75
75
  # * An +Ip+ object
76
- # Extend the range if the pushed IP is the next, contiguous ip in the range. Otherwise return false and do nothing.
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
- @first = ip
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 "pushing" do
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 "empty range" do
60
- let(:empty) { described_class.new(:range => '') }
59
+ context "unshifting" do
60
+ context "when range is empty" do
61
+ let(:empty) { described_class.new(:range => '') }
61
62
 
62
- it "pushes" do
63
- res = empty.push(ip1)
64
- expect(res).to be_truthy
65
- end
63
+ it "unshifts" do
64
+ res = empty.unshift(ip1)
65
+ expect(res).to be_truthy
66
+ end
66
67
 
67
- it "sets first ip" do
68
- empty.push(ip1)
69
- expect(empty.first).to eq(ip1)
70
- end
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
- it "sets last ip" do
73
- empty.push(ip1)
74
- expect(empty.last).to eq(ip1)
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
- context "range with start" do
79
- let(:r1) { described_class.new(:range => ip1.to_s) }
81
+ context "when range has a start" do
82
+ let(:r1) { described_class.new(:range => ip2.to_s) }
80
83
 
81
- context "pushing next ip" do
82
- it "pushes" do
83
- res = r1.push(ip2)
84
- expect(res).to be_truthy
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
- it "sets last ip" do
88
- r1.push(ip2)
89
- expect(r1.last).to eq(ip2)
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 "pushing non-contiguous ip" do
94
- it "doesn't push" do
95
- res = r1.push(ip3)
96
- expect(res).to be_falsey
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
- it "doesn't set last ip" do
100
- r1.push(ip3)
101
- expect(r1.last).to eq(r1.first)
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
- context "pushing next ip" do
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 = range.push(ip3)
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
- range.push(ip3)
117
- expect(range.last).to eq(ip3)
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 "pushing non-contiguous ip" do
122
- it "doesn't push" do
123
- res = range.push(ip4)
124
- expect(res).to be_falsey
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
- it "doesn't set last ip" do
128
- range.push(ip4)
129
- expect(range.last).to eq(ip2)
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ip-ranges
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Salgado