ip-ranges 0.3.0 → 0.3.1

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
- SHA512:
3
- data.tar.gz: bfeb059f023c22d55817ad8cd86438de8c475921eca11f904f1a3bc5a3ce6126e93052698ee087bcd81ecb05ca7762471dbad11e9257947e6a044fff6323674a
4
- metadata.gz: 6fb1bd68e97989cb3a11633cd504f58a5ae6b55e00149117d5b59fda3855c939528c3a0167b925fbdf449352d21a713a799f8d2a2a63930e8aaea3a151a90a84
5
2
  SHA1:
6
- data.tar.gz: feb4ac595ab5177d3ad824d375111c7d93bab982
7
- metadata.gz: cf86626124601bd06170150e5b6ff1d26abca600
3
+ metadata.gz: 758de3bb2dd52b835b7fbfaebc9be527d5ea637b
4
+ data.tar.gz: 7cec73e1e6aed0dfc5205acfc760b6c9bdbb6434
5
+ SHA512:
6
+ metadata.gz: d868e062f491f696d7448e765e1b1722e3b844978594fcb0ff425650441a7b587bbafe08ab2b8a9af69bddd6eac8fb543500e7d9cfaedcb61897958ff0bcaa3d
7
+ data.tar.gz: 55a8d58e43c4658ae556fe2416867884e2c5577d9cb53a6018a229c56e2158e45ffd3e199192f211d3ecd2680480a0157e2d929431e0d48acec5aea1b8fdf788
@@ -13,18 +13,19 @@ GEM
13
13
  rdoc (3.9.4)
14
14
  rdoc-data (3.12)
15
15
  rdoc (> 2.5, < 4.0)
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)
16
+ rspec (3.3.0)
17
+ rspec-core (~> 3.3.0)
18
+ rspec-expectations (~> 3.3.0)
19
+ rspec-mocks (~> 3.3.0)
20
+ rspec-core (3.3.1)
21
+ rspec-support (~> 3.3.0)
22
+ rspec-expectations (3.3.0)
23
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
+ rspec-support (~> 3.3.0)
25
+ rspec-mocks (3.3.1)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.3.0)
28
+ rspec-support (3.3.0)
28
29
  ruby-debug (0.10.4)
29
30
  columnize (>= 0.1)
30
31
  ruby-debug-base (~> 0.10.4.0)
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.0"
39
+ s.version = "0.3.1"
40
40
  s.summary = "Compare and manipulate ranges of IP numbers"
41
41
  s.author = "David Salgado"
42
42
  s.email = "david@digitalronin.com"
@@ -4,7 +4,7 @@ module IpRanges
4
4
  # with other IP numbers, as well as a method to increment the IP number.
5
5
  class Ip
6
6
  # String representation of this Ip number
7
- #
7
+ #
8
8
  # Example:
9
9
  #
10
10
  # ip = Ip.new :number => '1.2.3.4'
@@ -15,7 +15,7 @@ module IpRanges
15
15
  # Arguments:
16
16
  #
17
17
  # * A hash containing a single key whose value is the IP number as a string
18
- #
18
+ #
19
19
  # Example:
20
20
  #
21
21
  # Ip.new(:number => '1.2.3.4')
@@ -95,10 +95,45 @@ module IpRanges
95
95
  end
96
96
  end
97
97
 
98
+ # Changes the 'number' property of this Ip
99
+ # to the number before this one.
100
+ #
101
+ # example:
102
+ #
103
+ # ip = Ip.new(:number => '1.2.3.4')
104
+ # ip.number => '1.2.3.4'
105
+ # ip.decrement
106
+ # ip.number => '1.2.3.3'
107
+ #
108
+ def decrement
109
+ a, b, c, d = tuples number
110
+ if d > 0
111
+ d -= 1
112
+ else
113
+ d = 255
114
+ if c > 0
115
+ c -= 1
116
+ else
117
+ c = 255
118
+ if b > 0
119
+ b -= 1
120
+ else
121
+ b = 255
122
+ if a > 0
123
+ a -= 1
124
+ else
125
+ raise "No more IPs"
126
+ end
127
+ end
128
+ end
129
+ end
130
+ @number = [a, b, c, d].join('.')
131
+ end
132
+
98
133
  # Changes the 'number' property of this Ip
99
134
  # to the next number after this one.
100
135
  #
101
- # example:
136
+ # example:
102
137
  #
103
138
  # ip = Ip.new(:number => '1.2.3.4')
104
139
  # ip.number => '1.2.3.4'
@@ -1,94 +1,128 @@
1
1
  require 'spec/spec_helper'
2
2
 
3
3
  describe IpRanges::Ip do
4
- before do
5
- @ip = IpRanges::Ip.new
6
- end
4
+ let(:number) { "1.200.3.4" }
5
+ subject(:ip) { described_class.new(:number => number) }
7
6
 
8
- it "duplicates with a different object" do
9
- @ip.number = "1.200.3.4"
10
- ip2 = @ip.dup
11
- expect(ip2.object_id).to_not eq(@ip.object_id)
12
- end
7
+ context "duplicating" do
8
+ let(:dup) { ip.dup }
13
9
 
14
- it "duplicates" do
15
- @ip.number = "1.200.3.4"
16
- ip2 = @ip.dup
17
- expect(ip2).to eq(@ip)
18
- end
19
-
20
- it "is greater than or equal to" do
21
- @ip.number = "1.200.3.4"
22
- eq = IpRanges::Ip.new :number => "1.200.3.4"
23
- lt = IpRanges::Ip.new :number => "1.200.3.3"
24
- [eq, lt].each {|test| expect(@ip >= test).to be_truthy}
25
- end
10
+ it "duplicates as a different object" do
11
+ expect(dup.object_id).to_not eq(ip.object_id)
12
+ end
26
13
 
27
- it "is equivalent" do
28
- @ip.number = "1.200.3.4"
29
- ip2 = IpRanges::Ip.new :number => "1.200.3.4"
30
- expect(@ip).to eq(ip2)
14
+ it "considers duplicate to be equivalent" do
15
+ expect(dup).to eq(ip)
16
+ end
31
17
  end
32
18
 
33
19
  it "renders to string" do
34
- @ip.number = "1.200.3.4"
35
- expect(@ip.to_s).to eq("1.200.3.4")
20
+ expect(ip.to_s).to eq(number)
36
21
  end
37
22
 
38
- it "knows 1.2.3.255 < 1.2.4.0" do
39
- @ip.number = '1.2.3.255'
40
- ip2 = IpRanges::Ip.new :number => "1.2.4.0"
41
- expect((@ip > ip2)).to be_falsey
42
- end
23
+ context "comparing" do
24
+ let(:eql) { IpRanges::Ip.new :number => number }
25
+ let(:lt) { IpRanges::Ip.new :number => "1.200.3.3" }
43
26
 
44
- it "knows 1.2.4.0 >= 1.2.3.255" do
45
- @ip.number = "1.2.4.0"
46
- ip2 = IpRanges::Ip.new :number => '1.2.3.255'
47
- expect((@ip >= ip2)).to be_truthy
48
- end
27
+ it "is greater than or equal to" do
28
+ [eql, lt].each {|test| expect(ip >= test).to be_truthy}
29
+ end
49
30
 
50
- it "knows 1.200.3.4 > 1.199.3.4" do
51
- @ip.number = "1.200.3.4"
52
- ip2 = IpRanges::Ip.new :number => '1.199.3.4'
53
- expect((@ip > ip2)).to be_truthy
54
- end
31
+ it "is equivalent" do
32
+ expect(ip).to eq(eql)
33
+ end
55
34
 
56
- it "knows 1.2.3.4 > 1.2.2.4" do
57
- @ip.number = "1.2.3.4"
58
- ip2 = IpRanges::Ip.new :number => '1.2.2.4'
59
- expect((@ip > ip2)).to be_truthy
60
- end
35
+ context "when last tuple rolls over" do
36
+ let(:number) { "1.2.3.255" }
37
+ let(:bigger) { described_class.new(:number => '1.2.4.0') }
61
38
 
62
- it "knows 1.2.3.4 > 1.2.3.3" do
63
- @ip.number = "1.2.3.4"
64
- ip2 = IpRanges::Ip.new :number => '1.2.3.3'
65
- expect((@ip > ip2)).to be_truthy
66
- end
39
+ specify { expect((ip > bigger)).to be_falsey }
40
+ specify { expect((bigger >= ip)).to be_truthy }
41
+ end
67
42
 
68
- it "knows 1.2.3.3 <= 1.2.3.4" do
69
- @ip.number = "1.2.3.3"
70
- ip2 = IpRanges::Ip.new :number => '1.2.3.4'
71
- expect((@ip > ip2)).to be_falsey
72
- end
43
+ context "when 2nd tuple is bigger" do
44
+ let(:number) { '1.199.3.4' }
45
+ let(:bigger) { described_class.new(:number => '1.200.3.4') }
73
46
 
74
- it "increments when 2nd tuple hits 255" do
75
- @ip.number = "1.255.255.255"
76
- expect(@ip.increment).to eq("2.0.0.0")
77
- end
47
+ specify { expect((ip > bigger)).to be_falsey }
48
+ specify { expect((bigger >= ip)).to be_truthy }
49
+ end
78
50
 
79
- it "increments when 3rd tuple hits 255" do
80
- @ip.number = "1.2.255.255"
81
- expect(@ip.increment).to eq("1.3.0.0")
82
- end
51
+ context "when 3rd tuple is bigger" do
52
+ let(:number) { '1.2.2.4' }
53
+ let(:bigger) { described_class.new(:number => '1.2.3.4') }
83
54
 
84
- it "increments when last tuple hits 255" do
85
- @ip.number = "1.2.3.255"
86
- expect(@ip.increment).to eq("1.2.4.0")
55
+ specify { expect((ip > bigger)).to be_falsey }
56
+ specify { expect((bigger >= ip)).to be_truthy }
57
+ end
58
+
59
+ context "when last tuple is bigger" do
60
+ let(:number) { '1.2.3.3' }
61
+ let(:bigger) { described_class.new(:number => '1.2.3.4') }
62
+
63
+ specify { expect((ip > bigger)).to be_falsey }
64
+ specify { expect((bigger >= ip)).to be_truthy }
65
+ end
87
66
  end
88
67
 
89
- it "increments last tuple" do
90
- @ip.number = '1.2.3.4'
91
- expect(@ip.increment).to eq("1.2.3.5")
68
+ context "incrementing" do
69
+ context "when there are no more numbers" do
70
+ let(:number) { '255.255.255.255' }
71
+
72
+ it "barfs" do
73
+ expect { ip.increment }.to raise_error(RuntimeError)
74
+ end
75
+ end
76
+
77
+ context "when 2nd tuple hits 255" do
78
+ let(:number) { '1.255.255.255' }
79
+
80
+ specify { expect(ip.increment).to eq("2.0.0.0") }
81
+ end
82
+
83
+ context "when 3rd tuple hits 255" do
84
+ let(:number) { '1.2.255.255' }
85
+
86
+ specify { expect(ip.increment).to eq("1.3.0.0") }
87
+ end
88
+
89
+ context "when last tuple hits 255" do
90
+ let(:number) { '1.2.3.255' }
91
+
92
+ specify { expect(ip.increment).to eq("1.2.4.0") }
93
+ end
94
+
95
+ specify { expect(ip.increment).to eq("1.200.3.5") }
92
96
  end
93
97
 
98
+
99
+ context "decrementing" do
100
+ context "when there are no more numbers" do
101
+ let(:number) { '0.0.0.0' }
102
+
103
+ it "barfs" do
104
+ expect { ip.decrement }.to raise_error(RuntimeError)
105
+ end
106
+ end
107
+
108
+ context "when 2nd tuple hits 0" do
109
+ let(:number) { '2.0.0.0' }
110
+
111
+ specify { expect(ip.decrement).to eq("1.255.255.255") }
112
+ end
113
+
114
+ context "when 3rd tuple hits 0" do
115
+ let(:number) { '1.2.0.0' }
116
+
117
+ specify { expect(ip.decrement).to eq("1.1.255.255") }
118
+ end
119
+
120
+ context "when last tuple hits 0" do
121
+ let(:number) { '1.2.3.0' }
122
+
123
+ specify { expect(ip.decrement).to eq("1.2.2.255") }
124
+ end
125
+
126
+ specify { expect(ip.decrement).to eq("1.200.3.3") }
127
+ end
94
128
  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.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Salgado
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2014-12-19 00:00:00 Z
12
+ date: 2015-06-26 00:00:00 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  requirement: &id001 !ruby/object:Gem::Requirement
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  requirements: []
89
89
 
90
90
  rubyforge_project:
91
- rubygems_version: 2.0.14
91
+ rubygems_version: 2.0.15
92
92
  signing_key:
93
93
  specification_version: 4
94
94
  summary: Compare and manipulate ranges of IP numbers