ip_address 0.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +0 -0
- data/lib/ip_address.rb +63 -21
- metadata +57 -55
- metadata.gz.sig +1 -1
data.tar.gz.sig
CHANGED
Binary file
|
data/lib/ip_address.rb
CHANGED
@@ -8,12 +8,51 @@ end
|
|
8
8
|
|
9
9
|
# This class represents an IP address.
|
10
10
|
class IPAddress
|
11
|
-
|
12
|
-
|
11
|
+
class << self
|
12
|
+
# Is _addr_ an IP address?
|
13
|
+
def is_an_ip?(addr)
|
14
|
+
%w{
|
15
|
+
is_an_ip_instance? is_a_string_ip? is_an_array_ip?
|
16
|
+
is_an_integer_ip?
|
17
|
+
}.each do |m|
|
18
|
+
return true if send(m, addr)
|
19
|
+
end
|
20
|
+
|
21
|
+
false
|
22
|
+
end
|
23
|
+
|
24
|
+
def is_an_ip_instance?(addr)
|
25
|
+
addr.is_a?(IPAddress)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Is _addr_ a string representation of an IP (without a netmask)?
|
29
|
+
def is_a_string_ip?(addr)
|
30
|
+
addr, mask = /^(\d+(?:\.\d+){3})(?:\/(\d+))?$/.match(addr).captures
|
31
|
+
|
32
|
+
# nil.to_i is 0.
|
33
|
+
return false unless (0..32) === mask.to_i
|
34
|
+
|
35
|
+
is_an_array_ip?( addr.split(".").map{|x| x.to_i} )
|
36
|
+
end
|
13
37
|
|
14
|
-
|
15
|
-
|
16
|
-
|
38
|
+
# Is this an array representation of an IP?
|
39
|
+
def is_an_array_ip?(addr)
|
40
|
+
addr.length == 4 and addr.all?{|a| (0..255).include?(a)}
|
41
|
+
end
|
42
|
+
|
43
|
+
# Is _addr_ and integer representation of an IP?
|
44
|
+
def is_an_integer_ip?(addr)
|
45
|
+
addr.integer? and (0 ... 256**4).include?(addr)
|
46
|
+
end
|
47
|
+
|
48
|
+
%w{
|
49
|
+
is_an_ip_instance? is_a_string_ip? is_an_array_ip? is_an_integer_ip?
|
50
|
+
}.each do |meth_name|
|
51
|
+
_m = instance_method(meth_name)
|
52
|
+
define_method(meth_name){ |*a,&b|
|
53
|
+
_m.bind(self).(*a, &b) rescue false
|
54
|
+
}
|
55
|
+
end
|
17
56
|
end
|
18
57
|
|
19
58
|
# our netmask
|
@@ -58,9 +97,9 @@ class IPAddress
|
|
58
97
|
# ip[3] = 8
|
59
98
|
# ip #=> #<IPAddress: 12.4.97.8>
|
60
99
|
def []=(index, val)
|
61
|
-
if
|
100
|
+
if !((0..3) === index)
|
62
101
|
raise ArgumentError, "there are four parts to an IP address"
|
63
|
-
elsif
|
102
|
+
elsif !((0..256) === val)
|
64
103
|
raise ArgumentError, "each of the IP parts is between 0 and 256"
|
65
104
|
end
|
66
105
|
|
@@ -97,6 +136,7 @@ class IPAddress
|
|
97
136
|
end
|
98
137
|
|
99
138
|
def ==(ip)
|
139
|
+
return false unless self.class.is_an_ip?(ip)
|
100
140
|
ip = self.class.new(ip) unless ip.is_a? self.class
|
101
141
|
|
102
142
|
ip.to_i == to_i and ip.netmask == @netmask
|
@@ -116,25 +156,27 @@ class IPAddress
|
|
116
156
|
# Fixnum (also described there) and a netmask.
|
117
157
|
# @return [Fixnum, Fixnum] something like [201613576, 32]
|
118
158
|
def any_to_int_and_netmask(ip)
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
159
|
+
if self.class.is_a_string_ip?(ip)
|
160
|
+
if ip =~ /^(.+)\/(.+)$/
|
161
|
+
ip, mask = $1, $2.to_i
|
162
|
+
else
|
163
|
+
mask = 32
|
164
|
+
end
|
124
165
|
|
125
|
-
|
166
|
+
quads = ip.split('.').map{|q| q.to_i}
|
167
|
+
return [array_to_int(quads), mask]
|
126
168
|
|
127
|
-
|
128
|
-
|
169
|
+
elsif self.class.is_an_array_ip?(ip)
|
170
|
+
return array_to_int(ip), 32
|
129
171
|
|
130
|
-
|
131
|
-
|
172
|
+
elsif self.class.is_an_integer_ip?(ip)
|
173
|
+
return ip, 32
|
132
174
|
|
133
|
-
|
134
|
-
|
175
|
+
elsif self.class.is_an_ip_instance?(ip)
|
176
|
+
return ip.to_i, ip.netmask
|
135
177
|
|
136
|
-
|
137
|
-
|
178
|
+
else
|
179
|
+
raise NotAnIPError.new(ip)
|
138
180
|
end
|
139
181
|
end
|
140
182
|
|
metadata
CHANGED
@@ -1,87 +1,89 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: ip_address
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
version: "0.1"
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- katmagic
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
|
-
cert_chain:
|
16
|
-
-
|
17
|
-
|
11
|
+
cert_chain:
|
12
|
+
- ! '-----BEGIN CERTIFICATE-----
|
13
|
+
|
18
14
|
MIIDQDCCAiigAwIBAgIBADANBgkqhkiG9w0BAQUFADBGMRgwFgYDVQQDDA90aGUu
|
15
|
+
|
19
16
|
bWFnaWNhbC5rYXQxFTATBgoJkiaJk/IsZAEZFgVnbWFpbDETMBEGCgmSJomT8ixk
|
20
|
-
|
17
|
+
|
18
|
+
ARkWA2NvbTAeFw0xMTAyMTUwNDU0MDZaFw0xMjAyMTUwNDU0MDZaMEYxGDAWBgNV
|
19
|
+
|
21
20
|
BAMMD3RoZS5tYWdpY2FsLmthdDEVMBMGCgmSJomT8ixkARkWBWdtYWlsMRMwEQYK
|
21
|
+
|
22
22
|
CZImiZPyLGQBGRYDY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
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|
49
|
+
|
36
50
|
-----END CERTIFICATE-----
|
37
51
|
|
38
|
-
|
52
|
+
'
|
53
|
+
date: 2011-02-14 00:00:00.000000000 -05:00
|
39
54
|
default_executable:
|
40
55
|
dependencies: []
|
41
|
-
|
42
56
|
description: Easily manipulate IPv4 addresses.
|
43
57
|
email: the.magical.kat@gmail.com
|
44
58
|
executables: []
|
45
|
-
|
46
59
|
extensions: []
|
47
|
-
|
48
60
|
extra_rdoc_files: []
|
49
|
-
|
50
|
-
files:
|
61
|
+
files:
|
51
62
|
- lib/ip_address.rb
|
52
63
|
has_rdoc: true
|
53
|
-
homepage: https://github.com/katmagic/
|
54
|
-
licenses:
|
64
|
+
homepage: https://github.com/katmagic/IPAddress
|
65
|
+
licenses:
|
55
66
|
- Unlicense (http://unlicense.org)
|
56
67
|
post_install_message:
|
57
68
|
rdoc_options: []
|
58
|
-
|
59
|
-
require_paths:
|
69
|
+
require_paths:
|
60
70
|
- lib
|
61
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
72
|
none: false
|
63
|
-
requirements:
|
64
|
-
- -
|
65
|
-
- !ruby/object:Gem::Version
|
66
|
-
|
67
|
-
|
68
|
-
- 0
|
69
|
-
version: "0"
|
70
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
78
|
none: false
|
72
|
-
requirements:
|
73
|
-
- -
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
|
76
|
-
segments:
|
77
|
-
- 0
|
78
|
-
version: "0"
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
79
83
|
requirements: []
|
80
|
-
|
81
84
|
rubyforge_project: ip_address
|
82
|
-
rubygems_version: 1.
|
85
|
+
rubygems_version: 1.5.2
|
83
86
|
signing_key:
|
84
87
|
specification_version: 3
|
85
88
|
summary: Work with IP addresses.
|
86
89
|
test_files: []
|
87
|
-
|
metadata.gz.sig
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Z
|
1
|
+
&7[�y��^q��P�9��/hp�a��.b��!d9X���ߢ7q��;Z>�ij1� a�������m�/"NIB`���`G{ohOs�{3��<�E�n��s����S'��g�u*w�S�;�e��]a���C��.ŋ+�Id�nkW���De^�������;�rr%����&�����[�Z�-����Z%�m��R�߽F�g&f�h�_����e.�X���]���w6�:j>V�������ԡi�����C$*ն
|