ip-ranges 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile.lock +13 -9
- data/Rakefile +6 -4
- data/lib/ip-ranges/range.rb +35 -13
- data/spec/ip-ranges/ip_spec.rb +15 -15
- data/spec/ip-ranges/range_spec.rb +62 -35
- data/spec/ip_ranges_spec.rb +5 -5
- metadata +32 -55
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4b259f6d6fdd7a56ae0f743d4847dcaed64b4507
|
4
|
+
data.tar.gz: 32c8d867812c27cf93144a5fb8c030e2ac1f23f1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9fe1d1356fcf54a2e53fb59809d881176bbfc743ee01af6ebe80776ef9a3dd7cd04b21cc5d6f8f1360f0f6f55fa3532448708dcf21bf173c3517a4c80ef69fe7
|
7
|
+
data.tar.gz: 382d3042a30e8ae524dd311fcb20eda44f97cca341d84af27d51f074a86dcba7aa584aa2b2161fbbfc504a4633b82a30d0add5ef07414bf537e4e43d4d605d01
|
data/Gemfile.lock
CHANGED
@@ -2,7 +2,7 @@ GEM
|
|
2
2
|
remote: https://rubygems.org/
|
3
3
|
specs:
|
4
4
|
columnize (0.8.9)
|
5
|
-
diff-lcs (1.
|
5
|
+
diff-lcs (1.2.5)
|
6
6
|
gem-this (0.3.7)
|
7
7
|
gemcutter (0.7.1)
|
8
8
|
linecache (0.46)
|
@@ -13,14 +13,18 @@ GEM
|
|
13
13
|
rdoc (3.9.4)
|
14
14
|
rdoc-data (3.12)
|
15
15
|
rdoc (> 2.5, < 4.0)
|
16
|
-
rspec (
|
17
|
-
rspec-core (~>
|
18
|
-
rspec-expectations (~>
|
19
|
-
rspec-mocks (~>
|
20
|
-
rspec-core (
|
21
|
-
|
22
|
-
|
23
|
-
|
16
|
+
rspec (3.1.0)
|
17
|
+
rspec-core (~> 3.1.0)
|
18
|
+
rspec-expectations (~> 3.1.0)
|
19
|
+
rspec-mocks (~> 3.1.0)
|
20
|
+
rspec-core (3.1.7)
|
21
|
+
rspec-support (~> 3.1.0)
|
22
|
+
rspec-expectations (3.1.2)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.1.0)
|
25
|
+
rspec-mocks (3.1.3)
|
26
|
+
rspec-support (~> 3.1.0)
|
27
|
+
rspec-support (3.1.2)
|
24
28
|
ruby-debug (0.10.4)
|
25
29
|
columnize (>= 0.1)
|
26
30
|
ruby-debug-base (~> 0.10.4.0)
|
data/Rakefile
CHANGED
@@ -36,17 +36,18 @@ spec = Gem::Specification.new do |s|
|
|
36
36
|
|
37
37
|
# Change these as appropriate
|
38
38
|
s.name = "ip-ranges"
|
39
|
-
s.version = "0.
|
39
|
+
s.version = "0.2.0"
|
40
40
|
s.summary = "Compare and manipulate ranges of IP numbers"
|
41
41
|
s.author = "David Salgado"
|
42
42
|
s.email = "david@digitalronin.com"
|
43
43
|
s.homepage = "https://digitalronin.github.io/2011/09/10/ip-ranges-gem/"
|
44
44
|
|
45
45
|
s.has_rdoc = true
|
46
|
-
s.
|
47
|
-
s.
|
46
|
+
s.description = "Compare multiple IP ranges for overlaps, equivalence and containment"
|
47
|
+
# s.extra_rdoc_files = %w(README)
|
48
|
+
s.rdoc_options = %w(-x pkg)
|
48
49
|
|
49
|
-
# Add any extra files to include in the gem
|
50
|
+
# Add any extra files to include in the gem (like your README)
|
50
51
|
s.files = %w(Gemfile Gemfile.lock Rakefile README.markdown) + Dir.glob("{spec,lib}/**/*")
|
51
52
|
s.require_paths = ["lib"]
|
52
53
|
|
@@ -56,6 +57,7 @@ spec = Gem::Specification.new do |s|
|
|
56
57
|
|
57
58
|
# If your tests use any gems, include them here
|
58
59
|
s.add_development_dependency("rspec")
|
60
|
+
s.add_development_dependency("rdoc")
|
59
61
|
s.add_development_dependency("ruby-debug")
|
60
62
|
end
|
61
63
|
|
data/lib/ip-ranges/range.rb
CHANGED
@@ -11,18 +11,28 @@ module IpRanges
|
|
11
11
|
# Last IP number in the range, as an Ip object
|
12
12
|
attr_accessor :last
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
# Throttle (or don't) how fast we yield each_ip
|
15
|
+
attr_accessor :throttle
|
16
|
+
attr_accessor :sleep_time
|
17
17
|
|
18
18
|
BATCHSIZE = 250
|
19
|
-
PAUSE
|
19
|
+
PAUSE = 2
|
20
|
+
|
21
|
+
|
22
|
+
SINGLE_IP = /^\d+\.\d+\.\d+\.\d+$/
|
23
|
+
DOTTED_RANGE = /^(\d+\.\d+\.\d+\.\d+)\.\.(\d+\.\d+\.\d+\.\d+)$/
|
24
|
+
CIDR_RANGE = %r[/]
|
20
25
|
|
21
26
|
# Instatiate a range from a string representation which can be a single
|
22
27
|
# Ip number, a dotted range or a CIDR range
|
23
28
|
#
|
24
29
|
# Arguments:
|
25
|
-
# * A +Hash+ with a
|
30
|
+
# * A +Hash+ with a key ':range' whose value is a string defining the range.
|
31
|
+
#
|
32
|
+
# Optional Arguments (supply as hash keys):
|
33
|
+
# * ':throttle' true/false (default: true), sleep in each_ip after every batch_size IPs
|
34
|
+
# * ':batch_size' (for throttling - default: 250)
|
35
|
+
# * ':sleep_time' (for throttling - default: 2)
|
26
36
|
#
|
27
37
|
# Examples:
|
28
38
|
#
|
@@ -31,19 +41,24 @@ module IpRanges
|
|
31
41
|
# IpRanges::Range.new :range => '1.2.3.0/24' => a range containing 256 Ip numbers (1.2.3.0 .. 1.2.3.255)
|
32
42
|
#
|
33
43
|
def initialize(params)
|
34
|
-
|
44
|
+
@throttle = params.fetch(:throttle, true)
|
45
|
+
@sleep_time = params.fetch(:sleep_time, PAUSE)
|
46
|
+
@batch_size = params.fetch(:batch_size, BATCHSIZE)
|
47
|
+
|
48
|
+
string = params.fetch(:range).gsub(' ', '')
|
49
|
+
|
35
50
|
case string
|
36
51
|
when SINGLE_IP
|
37
52
|
@first = Ip.new :number => string
|
38
|
-
@last
|
53
|
+
@last = Ip.new :number => string
|
39
54
|
when DOTTED_RANGE
|
40
55
|
@first = Ip.new :number => $1
|
41
|
-
@last
|
56
|
+
@last = Ip.new :number => $2
|
42
57
|
when CIDR_RANGE
|
43
58
|
begin
|
44
|
-
cidr
|
59
|
+
cidr = NetAddr::CIDR.create(string)
|
45
60
|
@first = Ip.new :number => cidr.first
|
46
|
-
@last
|
61
|
+
@last = Ip.new :number => cidr.last
|
47
62
|
rescue Exception => e
|
48
63
|
puts "Error creating CIDR range from: #{string}"
|
49
64
|
raise e
|
@@ -51,6 +66,13 @@ module IpRanges
|
|
51
66
|
end
|
52
67
|
end
|
53
68
|
|
69
|
+
# Return the number of IPs within the range
|
70
|
+
def count
|
71
|
+
rtn = 0
|
72
|
+
each_ip {|ip| rtn += 1}
|
73
|
+
rtn
|
74
|
+
end
|
75
|
+
|
54
76
|
def to_s
|
55
77
|
if empty?
|
56
78
|
''
|
@@ -136,13 +158,13 @@ module IpRanges
|
|
136
158
|
def throttle
|
137
159
|
@counter += 1
|
138
160
|
if @counter % BATCHSIZE == 0
|
139
|
-
take_a_nap
|
161
|
+
take_a_nap if throttle
|
140
162
|
end
|
141
163
|
end
|
142
164
|
|
143
165
|
def take_a_nap
|
144
|
-
log "Counter: #{$counter}, sleeping for #{
|
145
|
-
sleep
|
166
|
+
log "Counter: #{$counter}, sleeping for #{sleep_time} seconds" if $verbose
|
167
|
+
sleep sleep_time
|
146
168
|
end
|
147
169
|
|
148
170
|
end
|
data/spec/ip-ranges/ip_spec.rb
CHANGED
@@ -8,87 +8,87 @@ describe IpRanges::Ip do
|
|
8
8
|
it "duplicates with a different object" do
|
9
9
|
@ip.number = "1.200.3.4"
|
10
10
|
ip2 = @ip.dup
|
11
|
-
ip2.object_id.
|
11
|
+
expect(ip2.object_id).to_not eq(@ip.object_id)
|
12
12
|
end
|
13
13
|
|
14
14
|
it "duplicates" do
|
15
15
|
@ip.number = "1.200.3.4"
|
16
16
|
ip2 = @ip.dup
|
17
|
-
ip2.
|
17
|
+
expect(ip2).to eq(@ip)
|
18
18
|
end
|
19
19
|
|
20
20
|
it "is greater than or equal to" do
|
21
21
|
@ip.number = "1.200.3.4"
|
22
22
|
eq = IpRanges::Ip.new :number => "1.200.3.4"
|
23
23
|
lt = IpRanges::Ip.new :number => "1.200.3.3"
|
24
|
-
[eq, lt].each {|test| (@ip >= test).
|
24
|
+
[eq, lt].each {|test| expect(@ip >= test).to be_truthy}
|
25
25
|
end
|
26
26
|
|
27
27
|
it "is equivalent" do
|
28
28
|
@ip.number = "1.200.3.4"
|
29
29
|
ip2 = IpRanges::Ip.new :number => "1.200.3.4"
|
30
|
-
@ip.
|
30
|
+
expect(@ip).to eq(ip2)
|
31
31
|
end
|
32
32
|
|
33
33
|
it "renders to string" do
|
34
34
|
@ip.number = "1.200.3.4"
|
35
|
-
@ip.to_s.
|
35
|
+
expect(@ip.to_s).to eq("1.200.3.4")
|
36
36
|
end
|
37
37
|
|
38
38
|
it "knows 1.2.3.255 < 1.2.4.0" do
|
39
39
|
@ip.number = '1.2.3.255'
|
40
40
|
ip2 = IpRanges::Ip.new :number => "1.2.4.0"
|
41
|
-
(@ip > ip2).
|
41
|
+
expect((@ip > ip2)).to be_falsey
|
42
42
|
end
|
43
43
|
|
44
44
|
it "knows 1.2.4.0 >= 1.2.3.255" do
|
45
45
|
@ip.number = "1.2.4.0"
|
46
46
|
ip2 = IpRanges::Ip.new :number => '1.2.3.255'
|
47
|
-
(@ip >= ip2).
|
47
|
+
expect((@ip >= ip2)).to be_truthy
|
48
48
|
end
|
49
49
|
|
50
50
|
it "knows 1.200.3.4 > 1.199.3.4" do
|
51
51
|
@ip.number = "1.200.3.4"
|
52
52
|
ip2 = IpRanges::Ip.new :number => '1.199.3.4'
|
53
|
-
(@ip > ip2).
|
53
|
+
expect((@ip > ip2)).to be_truthy
|
54
54
|
end
|
55
55
|
|
56
56
|
it "knows 1.2.3.4 > 1.2.2.4" do
|
57
57
|
@ip.number = "1.2.3.4"
|
58
58
|
ip2 = IpRanges::Ip.new :number => '1.2.2.4'
|
59
|
-
(@ip > ip2).
|
59
|
+
expect((@ip > ip2)).to be_truthy
|
60
60
|
end
|
61
61
|
|
62
62
|
it "knows 1.2.3.4 > 1.2.3.3" do
|
63
63
|
@ip.number = "1.2.3.4"
|
64
64
|
ip2 = IpRanges::Ip.new :number => '1.2.3.3'
|
65
|
-
(@ip > ip2).
|
65
|
+
expect((@ip > ip2)).to be_truthy
|
66
66
|
end
|
67
67
|
|
68
68
|
it "knows 1.2.3.3 <= 1.2.3.4" do
|
69
69
|
@ip.number = "1.2.3.3"
|
70
70
|
ip2 = IpRanges::Ip.new :number => '1.2.3.4'
|
71
|
-
(@ip > ip2).
|
71
|
+
expect((@ip > ip2)).to be_falsey
|
72
72
|
end
|
73
73
|
|
74
74
|
it "increments when 2nd tuple hits 255" do
|
75
75
|
@ip.number = "1.255.255.255"
|
76
|
-
@ip.increment.
|
76
|
+
expect(@ip.increment).to eq("2.0.0.0")
|
77
77
|
end
|
78
78
|
|
79
79
|
it "increments when 3rd tuple hits 255" do
|
80
80
|
@ip.number = "1.2.255.255"
|
81
|
-
@ip.increment.
|
81
|
+
expect(@ip.increment).to eq("1.3.0.0")
|
82
82
|
end
|
83
83
|
|
84
84
|
it "increments when last tuple hits 255" do
|
85
85
|
@ip.number = "1.2.3.255"
|
86
|
-
@ip.increment.
|
86
|
+
expect(@ip.increment).to eq("1.2.4.0")
|
87
87
|
end
|
88
88
|
|
89
89
|
it "increments last tuple" do
|
90
90
|
@ip.number = '1.2.3.4'
|
91
|
-
@ip.increment.
|
91
|
+
expect(@ip.increment).to eq("1.2.3.5")
|
92
92
|
end
|
93
93
|
|
94
94
|
end
|
@@ -1,24 +1,51 @@
|
|
1
1
|
require 'spec/spec_helper'
|
2
2
|
|
3
3
|
describe IpRanges::Range do
|
4
|
-
|
4
|
+
let(:range_str) { '1.2.3.4' }
|
5
|
+
subject(:range) { described_class.new(:range => range_str, :throttle => false) }
|
6
|
+
|
7
|
+
describe "#count" do
|
8
|
+
context "with one ip" do
|
9
|
+
let(:range_str) { '1.2.3.4' }
|
10
|
+
|
11
|
+
it "counts" do
|
12
|
+
expect(range.count).to eq(1)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "with dotted range" do
|
17
|
+
let(:range_str) { '1.2.3.4 .. 1.2.3.6' }
|
18
|
+
|
19
|
+
it "counts" do
|
20
|
+
expect(range.count).to eq(3)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
context "with cidr range" do
|
26
|
+
let(:range_str) { '1.2.3.0/24' }
|
27
|
+
|
28
|
+
it "counts" do
|
29
|
+
expect(range.count).to eq(256)
|
30
|
+
end
|
31
|
+
end
|
5
32
|
end
|
6
33
|
|
7
34
|
describe "#to_s" do
|
8
35
|
|
9
36
|
it "prints empty string" do
|
10
37
|
range = described_class.new(:range => '')
|
11
|
-
range.to_s.
|
38
|
+
expect(range.to_s).to eq('')
|
12
39
|
end
|
13
40
|
|
14
41
|
it "prints one ip" do
|
15
42
|
range = described_class.new(:range => '1.2.3.4')
|
16
|
-
range.to_s.
|
43
|
+
expect(range.to_s).to eq('1.2.3.4')
|
17
44
|
end
|
18
45
|
|
19
46
|
it "prints dotted range" do
|
20
47
|
range = described_class.new(:range => '1.2.3.4 .. 1.2.3.6')
|
21
|
-
range.to_s.
|
48
|
+
expect(range.to_s).to eq('1.2.3.4..1.2.3.6')
|
22
49
|
end
|
23
50
|
|
24
51
|
end
|
@@ -34,17 +61,17 @@ describe IpRanges::Range do
|
|
34
61
|
|
35
62
|
it "pushes" do
|
36
63
|
res = empty.push(ip1)
|
37
|
-
res.
|
64
|
+
expect(res).to be_truthy
|
38
65
|
end
|
39
66
|
|
40
67
|
it "sets first ip" do
|
41
68
|
empty.push(ip1)
|
42
|
-
empty.first.
|
69
|
+
expect(empty.first).to eq(ip1)
|
43
70
|
end
|
44
71
|
|
45
72
|
it "sets last ip" do
|
46
73
|
empty.push(ip1)
|
47
|
-
empty.last.
|
74
|
+
expect(empty.last).to eq(ip1)
|
48
75
|
end
|
49
76
|
end
|
50
77
|
|
@@ -54,24 +81,24 @@ describe IpRanges::Range do
|
|
54
81
|
context "pushing next ip" do
|
55
82
|
it "pushes" do
|
56
83
|
res = r1.push(ip2)
|
57
|
-
res.
|
84
|
+
expect(res).to be_truthy
|
58
85
|
end
|
59
86
|
|
60
87
|
it "sets last ip" do
|
61
88
|
r1.push(ip2)
|
62
|
-
r1.last.
|
89
|
+
expect(r1.last).to eq(ip2)
|
63
90
|
end
|
64
91
|
end
|
65
92
|
|
66
93
|
context "pushing non-contiguous ip" do
|
67
94
|
it "doesn't push" do
|
68
95
|
res = r1.push(ip3)
|
69
|
-
res.
|
96
|
+
expect(res).to be_falsey
|
70
97
|
end
|
71
98
|
|
72
99
|
it "doesn't set last ip" do
|
73
100
|
r1.push(ip3)
|
74
|
-
r1.last.
|
101
|
+
expect(r1.last).to eq(r1.first)
|
75
102
|
end
|
76
103
|
end
|
77
104
|
end
|
@@ -82,24 +109,24 @@ describe IpRanges::Range do
|
|
82
109
|
context "pushing next ip" do
|
83
110
|
it "pushes" do
|
84
111
|
res = range.push(ip3)
|
85
|
-
res.
|
112
|
+
expect(res).to be_truthy
|
86
113
|
end
|
87
114
|
|
88
115
|
it "sets last ip" do
|
89
116
|
range.push(ip3)
|
90
|
-
range.last.
|
117
|
+
expect(range.last).to eq(ip3)
|
91
118
|
end
|
92
119
|
end
|
93
120
|
|
94
121
|
context "pushing non-contiguous ip" do
|
95
122
|
it "doesn't push" do
|
96
123
|
res = range.push(ip4)
|
97
|
-
res.
|
124
|
+
expect(res).to be_falsey
|
98
125
|
end
|
99
126
|
|
100
127
|
it "doesn't set last ip" do
|
101
128
|
range.push(ip4)
|
102
|
-
range.last.
|
129
|
+
expect(range.last).to eq(ip2)
|
103
130
|
end
|
104
131
|
end
|
105
132
|
end
|
@@ -110,41 +137,41 @@ describe IpRanges::Range do
|
|
110
137
|
it "knows when range a contains range b" do
|
111
138
|
ipr1 = IpRanges::Range.new :range => '1.2.3.3..1.2.3.5'
|
112
139
|
ipr2 = IpRanges::Range.new :range => '1.2.3.4..1.2.3.5'
|
113
|
-
ipr1.contains_range?(ipr2).
|
140
|
+
expect(ipr1.contains_range?(ipr2)).to be_truthy
|
114
141
|
end
|
115
142
|
|
116
143
|
it "knows when range a does not contain range b" do
|
117
144
|
ipr1 = IpRanges::Range.new :range => '1.2.3.4..1.2.3.5'
|
118
145
|
ipr2 = IpRanges::Range.new :range => '1.2.3.5..1.2.3.6'
|
119
|
-
ipr1.contains_range?(ipr2).
|
146
|
+
expect(ipr1.contains_range?(ipr2)).to be_falsey
|
120
147
|
end
|
121
148
|
|
122
149
|
it "knows there is an overlap" do
|
123
150
|
ipr1 = IpRanges::Range.new :range => '1.2.3.4'
|
124
151
|
ipr2 = IpRanges::Range.new :range => '1.2.3.4'
|
125
|
-
ipr1.overlaps_range?(ipr2).
|
152
|
+
expect(ipr1.overlaps_range?(ipr2)).to be_truthy
|
126
153
|
end
|
127
154
|
|
128
155
|
it "knows there is no overlap" do
|
129
156
|
ipr1 = IpRanges::Range.new :range => '1.2.3.4'
|
130
157
|
ipr2 = IpRanges::Range.new :range => '1.2.3.5'
|
131
|
-
ipr1.overlaps_range?(ipr2).
|
158
|
+
expect(ipr1.overlaps_range?(ipr2)).to be_falsey
|
132
159
|
end
|
133
160
|
|
134
161
|
it "accepts Ip object for contains_ip" do
|
135
162
|
ipr = IpRanges::Range.new :range => '1.2.3.4..1.2.3.6'
|
136
163
|
ip = IpRanges::Ip.new :number => '1.2.3.5'
|
137
|
-
ipr.contains_ip?(ip).
|
164
|
+
expect(ipr.contains_ip?(ip)).to be_truthy
|
138
165
|
end
|
139
166
|
|
140
167
|
it "knows it doesn't contain ip numbers" do
|
141
168
|
ipr = IpRanges::Range.new :range => '1.2.3.4..1.2.3.6'
|
142
|
-
%w(1.2.3.3 1.2.3.7).each {|num| ipr.contains_ip?(num).
|
169
|
+
%w(1.2.3.3 1.2.3.7).each {|num| expect(ipr.contains_ip?(num)).to be_falsey}
|
143
170
|
end
|
144
171
|
|
145
172
|
it "knows it contains an ip number" do
|
146
173
|
ipr = IpRanges::Range.new :range => '1.2.3.4..1.2.3.6'
|
147
|
-
%w(1.2.3.4 1.2.3.5 1.2.3.6).each {|num| ipr.contains_ip?(num).
|
174
|
+
%w(1.2.3.4 1.2.3.5 1.2.3.6).each {|num| expect(ipr.contains_ip?(num)).to be_truthy}
|
148
175
|
end
|
149
176
|
|
150
177
|
end
|
@@ -152,8 +179,8 @@ describe IpRanges::Range do
|
|
152
179
|
it "knows it equals another range" do
|
153
180
|
dotted = IpRanges::Range.new :range => '1.2.3.0..1.2.3.255'
|
154
181
|
cidr = IpRanges::Range.new :range => '1.2.3.0/24'
|
155
|
-
(dotted == cidr).
|
156
|
-
(cidr == dotted).
|
182
|
+
expect(dotted == cidr).to be_truthy
|
183
|
+
expect(cidr == dotted).to be_truthy
|
157
184
|
end
|
158
185
|
|
159
186
|
it "iterates over all ips" do
|
@@ -165,56 +192,56 @@ describe IpRanges::Range do
|
|
165
192
|
]
|
166
193
|
arr = []
|
167
194
|
ipr.each_ip {|ip| arr << ip}
|
168
|
-
arr.
|
195
|
+
expect(arr).to eq(expected)
|
169
196
|
end
|
170
197
|
|
171
198
|
it "returns last ip from cidr range" do
|
172
199
|
ipr = IpRanges::Range.new :range => '1.2.3.0/24'
|
173
|
-
ipr.last.to_s.
|
200
|
+
expect(ipr.last.to_s).to eq('1.2.3.255')
|
174
201
|
end
|
175
202
|
|
176
203
|
it "returns first ip from cidr range" do
|
177
204
|
ipr = IpRanges::Range.new :range => '1.2.3.0/24'
|
178
|
-
ipr.first.to_s.
|
205
|
+
expect(ipr.first.to_s).to eq('1.2.3.0')
|
179
206
|
end
|
180
207
|
|
181
208
|
it "instantiates from a cidr range" do
|
182
209
|
ipr = IpRanges::Range.new :range => '1.2.3.0/24'
|
183
|
-
ipr.
|
210
|
+
expect(ipr).to be_kind_of(IpRanges::Range)
|
184
211
|
end
|
185
212
|
|
186
213
|
it "returns last ip from dotted range" do
|
187
214
|
ipr = IpRanges::Range.new :range => '1.2.3.4 .. 1.2.3.5'
|
188
|
-
ipr.last.to_s.
|
215
|
+
expect(ipr.last.to_s).to eq('1.2.3.5')
|
189
216
|
end
|
190
217
|
|
191
218
|
it "returns first ip from dotted range" do
|
192
219
|
ipr = IpRanges::Range.new :range => '1.2.3.4 .. 1.2.3.5'
|
193
|
-
ipr.first.to_s.
|
220
|
+
expect(ipr.first.to_s).to eq('1.2.3.4')
|
194
221
|
end
|
195
222
|
|
196
223
|
it "instantiates from a dotted range" do
|
197
224
|
ipr = IpRanges::Range.new :range => '1.2.3.4 .. 1.2.3.5'
|
198
|
-
ipr.
|
225
|
+
expect(ipr).to be_kind_of(IpRanges::Range)
|
199
226
|
end
|
200
227
|
|
201
228
|
it "returns last ip" do
|
202
229
|
ipr = IpRanges::Range.new :range => '1.2.3.4'
|
203
|
-
ipr.last.to_s.
|
230
|
+
expect(ipr.last.to_s).to eq('1.2.3.4')
|
204
231
|
end
|
205
232
|
|
206
233
|
it "returns first ip" do
|
207
234
|
ipr = IpRanges::Range.new :range => '1.2.3.4'
|
208
|
-
ipr.first.to_s.
|
235
|
+
expect(ipr.first.to_s).to eq('1.2.3.4')
|
209
236
|
end
|
210
237
|
|
211
238
|
it "instantiates from a single IP" do
|
212
239
|
ipr = IpRanges::Range.new :range => '1.2.3.4'
|
213
|
-
ipr.
|
240
|
+
expect(ipr).to be_kind_of(IpRanges::Range)
|
214
241
|
end
|
215
242
|
|
216
243
|
it "instantiates" do
|
217
244
|
ipr = IpRanges::Range.new :range => ''
|
218
|
-
ipr.
|
245
|
+
expect(ipr).to be_kind_of(IpRanges::Range)
|
219
246
|
end
|
220
247
|
end
|
data/spec/ip_ranges_spec.rb
CHANGED
@@ -8,30 +8,30 @@ describe IpRanges do
|
|
8
8
|
it "explains range a equals range b" do
|
9
9
|
list = %w(1.2.3.0..1.2.3.255 1.2.3.0/24)
|
10
10
|
overlaps = check_for_overlaps(list)
|
11
|
-
overlaps[0].
|
11
|
+
expect(overlaps[0]).to eq("1.2.3.0..1.2.3.255 equals range 1.2.3.0/24")
|
12
12
|
end
|
13
13
|
|
14
14
|
it "explains range a is contained by range b" do
|
15
15
|
list = %w(1.2.3.4..1.2.3.5 1.2.3.4..1.2.3.8)
|
16
16
|
overlaps = check_for_overlaps(list)
|
17
|
-
overlaps[0].
|
17
|
+
expect(overlaps[0]).to eq("1.2.3.4..1.2.3.5 is contained by range 1.2.3.4..1.2.3.8")
|
18
18
|
end
|
19
19
|
|
20
20
|
it "explains range a contains range b" do
|
21
21
|
list = %w(1.2.3.4..1.2.3.8 1.2.3.4..1.2.3.5)
|
22
22
|
overlaps = check_for_overlaps(list)
|
23
|
-
overlaps[0].
|
23
|
+
expect(overlaps[0]).to eq("1.2.3.4..1.2.3.8 contains range 1.2.3.4..1.2.3.5")
|
24
24
|
end
|
25
25
|
|
26
26
|
it "explains overlap" do
|
27
27
|
list = %w(1.2.3.4..1.2.3.8 1.2.3.5..1.2.3.9)
|
28
28
|
overlaps = check_for_overlaps(list)
|
29
|
-
overlaps[0].
|
29
|
+
expect(overlaps[0]).to eq("1.2.3.4..1.2.3.8 overlaps with 1.2.3.5..1.2.3.9")
|
30
30
|
end
|
31
31
|
|
32
32
|
it "counts the overlaps" do
|
33
33
|
list = %w(1.2.3.4..1.2.3.8 1.2.3.4..1.2.3.5 1.2.3.8)
|
34
|
-
check_for_overlaps(list).size.
|
34
|
+
expect(check_for_overlaps(list).size).to eq(2)
|
35
35
|
end
|
36
36
|
|
37
37
|
it "takes a list" do
|
metadata
CHANGED
@@ -1,13 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ip-ranges
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 3
|
10
|
-
version: 0.1.3
|
4
|
+
version: 0.2.0
|
11
5
|
platform: ruby
|
12
6
|
authors:
|
13
7
|
- David Salgado
|
@@ -15,60 +9,53 @@ autorequire:
|
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
11
|
|
18
|
-
date: 2014-
|
12
|
+
date: 2014-12-19 00:00:00 Z
|
19
13
|
dependencies:
|
20
14
|
- !ruby/object:Gem::Dependency
|
21
|
-
|
22
|
-
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
|
-
none: false
|
15
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
16
|
requirements:
|
25
17
|
- - ~>
|
26
18
|
- !ruby/object:Gem::Version
|
27
|
-
hash: 3
|
28
|
-
segments:
|
29
|
-
- 1
|
30
|
-
- 5
|
31
|
-
- 0
|
32
19
|
version: 1.5.0
|
33
|
-
|
20
|
+
version_requirements: *id001
|
34
21
|
prerelease: false
|
22
|
+
type: :runtime
|
35
23
|
name: netaddr
|
36
24
|
- !ruby/object:Gem::Dependency
|
37
|
-
|
38
|
-
version_requirements: &id002 !ruby/object:Gem::Requirement
|
39
|
-
none: false
|
25
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
26
|
requirements:
|
41
|
-
-
|
27
|
+
- &id003
|
28
|
+
- ">="
|
42
29
|
- !ruby/object:Gem::Version
|
43
|
-
hash: 3
|
44
|
-
segments:
|
45
|
-
- 0
|
46
30
|
version: "0"
|
47
|
-
|
31
|
+
version_requirements: *id002
|
48
32
|
prerelease: false
|
33
|
+
type: :development
|
49
34
|
name: rspec
|
50
35
|
- !ruby/object:Gem::Dependency
|
36
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- *id003
|
39
|
+
version_requirements: *id004
|
40
|
+
prerelease: false
|
51
41
|
type: :development
|
52
|
-
|
53
|
-
|
42
|
+
name: rdoc
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
54
45
|
requirements:
|
55
|
-
-
|
56
|
-
|
57
|
-
hash: 3
|
58
|
-
segments:
|
59
|
-
- 0
|
60
|
-
version: "0"
|
61
|
-
requirement: *id003
|
46
|
+
- *id003
|
47
|
+
version_requirements: *id005
|
62
48
|
prerelease: false
|
49
|
+
type: :development
|
63
50
|
name: ruby-debug
|
64
|
-
description:
|
51
|
+
description: Compare multiple IP ranges for overlaps, equivalence and containment
|
65
52
|
email: david@digitalronin.com
|
66
53
|
executables: []
|
67
54
|
|
68
55
|
extensions: []
|
69
56
|
|
70
|
-
extra_rdoc_files:
|
71
|
-
|
57
|
+
extra_rdoc_files: []
|
58
|
+
|
72
59
|
files:
|
73
60
|
- Gemfile
|
74
61
|
- Gemfile.lock
|
@@ -84,36 +71,26 @@ files:
|
|
84
71
|
homepage: https://digitalronin.github.io/2011/09/10/ip-ranges-gem/
|
85
72
|
licenses: []
|
86
73
|
|
74
|
+
metadata: {}
|
75
|
+
|
87
76
|
post_install_message:
|
88
77
|
rdoc_options:
|
89
|
-
-
|
90
|
-
-
|
78
|
+
- -x
|
79
|
+
- pkg
|
91
80
|
require_paths:
|
92
81
|
- lib
|
93
82
|
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
-
none: false
|
95
83
|
requirements:
|
96
|
-
-
|
97
|
-
- !ruby/object:Gem::Version
|
98
|
-
hash: 3
|
99
|
-
segments:
|
100
|
-
- 0
|
101
|
-
version: "0"
|
84
|
+
- *id003
|
102
85
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
-
none: false
|
104
86
|
requirements:
|
105
|
-
-
|
106
|
-
- !ruby/object:Gem::Version
|
107
|
-
hash: 3
|
108
|
-
segments:
|
109
|
-
- 0
|
110
|
-
version: "0"
|
87
|
+
- *id003
|
111
88
|
requirements: []
|
112
89
|
|
113
90
|
rubyforge_project:
|
114
|
-
rubygems_version:
|
91
|
+
rubygems_version: 2.0.14
|
115
92
|
signing_key:
|
116
|
-
specification_version:
|
93
|
+
specification_version: 4
|
117
94
|
summary: Compare and manipulate ranges of IP numbers
|
118
95
|
test_files: []
|
119
96
|
|