iplogic 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/iplogic.gemspec +4 -4
- data/lib/iplogic.rb +1 -1
- data/lib/iplogic/cidr.rb +27 -6
- data/spec/cidr_spec.rb +31 -0
- metadata +3 -3
data/iplogic.gemspec
CHANGED
@@ -10,6 +10,10 @@ Gem::Specification.new do |s|
|
|
10
10
|
An IPv4 swiss-army chainsaw
|
11
11
|
desc
|
12
12
|
|
13
|
+
s.summary = <<-sum
|
14
|
+
Because it's just a 32-bit integer.
|
15
|
+
sum
|
16
|
+
|
13
17
|
s.email = %q{jay@causes.com}
|
14
18
|
s.extra_rdoc_files = %w(
|
15
19
|
LICENSE
|
@@ -32,10 +36,6 @@ Gem::Specification.new do |s|
|
|
32
36
|
s.require_paths = ["lib"]
|
33
37
|
s.rubygems_version = '1.3.7'
|
34
38
|
|
35
|
-
s.summary = <<-sum
|
36
|
-
Because it's just a 32-bit integer.
|
37
|
-
sum
|
38
|
-
|
39
39
|
s.test_files = %w(
|
40
40
|
spec/ip_spec.rb
|
41
41
|
spec/cidr_spec.rb
|
data/lib/iplogic.rb
CHANGED
data/lib/iplogic/cidr.rb
CHANGED
@@ -2,6 +2,7 @@ module IPLogic
|
|
2
2
|
class CIDR
|
3
3
|
include Enumerable
|
4
4
|
|
5
|
+
SHORTENED_IP_REGEX = /^(\d{1,3}(\.\d{1,3}){1,3})\/(\d+)$/
|
5
6
|
class << self
|
6
7
|
def wrap(*args)
|
7
8
|
return args.first if args.first.is_a? CIDR
|
@@ -11,10 +12,8 @@ module IPLogic
|
|
11
12
|
bits = case bits
|
12
13
|
when Integer
|
13
14
|
bits
|
14
|
-
when String
|
15
|
+
when String, IP
|
15
16
|
netmask_to_bits(IP.wrap(bits))
|
16
|
-
when IP
|
17
|
-
netmask_to_bits(bits)
|
18
17
|
else
|
19
18
|
if bits.respond_to? :to_i
|
20
19
|
bits.to_i
|
@@ -23,18 +22,40 @@ module IPLogic
|
|
23
22
|
end
|
24
23
|
end
|
25
24
|
|
26
|
-
|
25
|
+
ip = parse_shortened_ip(ip) if ip.is_a? String
|
26
|
+
|
27
|
+
return new(ip, bits)
|
28
|
+
|
27
29
|
elsif args.size == 1
|
28
30
|
arg = args.first
|
29
|
-
|
30
|
-
|
31
|
+
|
32
|
+
return arg if arg.is_a? CIDR
|
33
|
+
|
34
|
+
# one argument means it's gotta be a string to parse
|
35
|
+
format_error(arg) unless arg.is_a? String
|
36
|
+
|
37
|
+
if arg =~ SHORTENED_IP_REGEX
|
38
|
+
new(parse_shortened_ip($1), $3)
|
39
|
+
elsif (parts = arg.split('/')).size == 2
|
40
|
+
ip = parse_shortened_ip(parts[0])
|
41
|
+
mask = IP.wrap(parts[1])
|
42
|
+
return new(ip, netmask_to_bits(mask))
|
31
43
|
else
|
32
44
|
format_error(arg)
|
33
45
|
end
|
34
46
|
end
|
35
47
|
end
|
48
|
+
|
36
49
|
private
|
50
|
+
# helper for formats like 11.22.33/24
|
51
|
+
# just adds some .0's to the end
|
52
|
+
def parse_shortened_ip(ip_str)
|
53
|
+
dots = ip_str.count('.')
|
54
|
+
IP.wrap(ip_str + '.0'*(3 - dots))
|
55
|
+
end
|
56
|
+
|
37
57
|
def netmask_to_bits(netmask)
|
58
|
+
raise FormatError, "CIDR: #{netmask} is not a netmask" unless netmask.netmask?
|
38
59
|
# TODO: there's probably a clever mathy way to do this,
|
39
60
|
# but this works just fine.
|
40
61
|
netmask.to_i.to_s(2) =~ /^(1*)0*$/
|
data/spec/cidr_spec.rb
CHANGED
@@ -21,6 +21,37 @@ describe CIDR do
|
|
21
21
|
r.inspect.should include '11.22.33.44/8'
|
22
22
|
end
|
23
23
|
|
24
|
+
it "parses slash notation with a netmask" do
|
25
|
+
r = CIDR('11.22.33.44/255.255.255.0')
|
26
|
+
r.ip.should be_a IP
|
27
|
+
r.ip.to_s.should == '11.22.33.44'
|
28
|
+
r.bits.should == 24
|
29
|
+
r.netmask.should == '255.255.255.0'
|
30
|
+
end
|
31
|
+
|
32
|
+
it "parses shortened slash notation" do
|
33
|
+
r = CIDR('11.22.33/24')
|
34
|
+
r.ip.should be_a IP
|
35
|
+
r.ip.to_s.should == '11.22.33.0'
|
36
|
+
r.bits.should == 24
|
37
|
+
r.netmask.should == '255.255.255.0'
|
38
|
+
end
|
39
|
+
|
40
|
+
it "parses shortened slash notation with a netmask" do
|
41
|
+
r = CIDR('11.22/255.255.0.0')
|
42
|
+
r.ip.should be_a IP
|
43
|
+
r.ip.to_s.should == '11.22.0.0'
|
44
|
+
r.bits.should == 16
|
45
|
+
r.netmask.should == '255.255.0.0'
|
46
|
+
end
|
47
|
+
|
48
|
+
it "supports wrapping" do
|
49
|
+
r = CIDR('11.22.33.44/24')
|
50
|
+
wrapped = CIDR(r)
|
51
|
+
wrapped.should be_a CIDR
|
52
|
+
r.object_id.should == wrapped.object_id
|
53
|
+
end
|
54
|
+
|
24
55
|
it "knows its bits" do
|
25
56
|
i = rand(33)
|
26
57
|
CIDR("1.1.1.1/#{i}").bits.
|
metadata
CHANGED