ip_address 0.1 → 0.1.2

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.
Files changed (4) hide show
  1. data.tar.gz.sig +0 -0
  2. data/lib/ip_address.rb +63 -21
  3. metadata +57 -55
  4. metadata.gz.sig +1 -1
data.tar.gz.sig CHANGED
Binary file
@@ -8,12 +8,51 @@ end
8
8
 
9
9
  # This class represents an IP address.
10
10
  class IPAddress
11
- # Things that match this are (mostly) IPs.
12
- IP_REGEXP = /^(?:\d{1,3}\.){3}\d{1,3}$/
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
- # Is _addr_ a string representation of an IP (without a netmask)?
15
- def self.is_an_ip?(addr)
16
- !!( IP_REGEXP =~ addr and addr.split(".").all?{|x| x.to_i < 256} )
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 not (0..3) === index
100
+ if !((0..3) === index)
62
101
  raise ArgumentError, "there are four parts to an IP address"
63
- elsif not (0..256) === val
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
- case ip
120
- when /^((?:\d+\.){3}\d+)(?:\/(\d+))?$/
121
- m = $~
122
- int_ip = array_to_int(m[1].split(".").map{|x| x.to_i})
123
- nmask = (m[2] || 32).to_i
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
- return int_ip, nmask
166
+ quads = ip.split('.').map{|q| q.to_i}
167
+ return [array_to_int(quads), mask]
126
168
 
127
- when Array
128
- return array_to_int(ip), 32
169
+ elsif self.class.is_an_array_ip?(ip)
170
+ return array_to_int(ip), 32
129
171
 
130
- when Integer
131
- return ip, 32
172
+ elsif self.class.is_an_integer_ip?(ip)
173
+ return ip, 32
132
174
 
133
- when self.class
134
- return ip.to_i, ip.netmask
175
+ elsif self.class.is_an_ip_instance?(ip)
176
+ return ip.to_i, ip.netmask
135
177
 
136
- else
137
- raise NotAnIPError.new(ip)
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
- hash: 9
5
- prerelease: false
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
- -----BEGIN CERTIFICATE-----
11
+ cert_chain:
12
+ - ! '-----BEGIN CERTIFICATE-----
13
+
18
14
  MIIDQDCCAiigAwIBAgIBADANBgkqhkiG9w0BAQUFADBGMRgwFgYDVQQDDA90aGUu
15
+
19
16
  bWFnaWNhbC5rYXQxFTATBgoJkiaJk/IsZAEZFgVnbWFpbDETMBEGCgmSJomT8ixk
20
- ARkWA2NvbTAeFw0xMDExMjcwMTQwMTRaFw0xMTExMjcwMTQwMTRaMEYxGDAWBgNV
17
+
18
+ ARkWA2NvbTAeFw0xMTAyMTUwNDU0MDZaFw0xMjAyMTUwNDU0MDZaMEYxGDAWBgNV
19
+
21
20
  BAMMD3RoZS5tYWdpY2FsLmthdDEVMBMGCgmSJomT8ixkARkWBWdtYWlsMRMwEQYK
21
+
22
22
  CZImiZPyLGQBGRYDY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA
23
- vVnssXKFL/qdWKIjTbLKZNz+gg78DICoQSVxeO63aSaYMxFtbv0KGXPZDkiFF1Jl
24
- m9ZIh+iVmoo9UcENk6CqY1BiMTMZ9QnHPdIFOsw3eBujh1wJ9cXybhaoma2apiIS
25
- CtPYPbTs1ucJ8tZUv2jddnj3hlFB2LRjMCquWs70uYhjOAwLkxAi6mijLyeQ+NCk
26
- X+wPgYF8mTylt2VbQAQEDtmchURo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gNVHQ4EFgQUTkdCjc2gkF28POXx+iuTDgmaR20wDQYJKoZIhvcNAQEFBQAD
37
+
38
+ ggEBAHV/0Mp6m577vyZMxXGdBUcXHAxtd8n4FexczZlZm5NUXwGGVFAeFAG9jY0U
39
+
40
+ A/uWI3gIGoE1GOh+iDKnq56epx+wMcWIsLyu7qSbVBgEGmlbYZ9A8LUkk8FQ+CK1
41
+
42
+ v5dQUsPRBBYHR/GmQQekkAKumU2n0m1tYLOdKcsi0f3Rjx5XcpUtvgDd4RHiVLcx
43
+
44
+ ezvazGDhHTO5Jx5qF3wQ9yJqEoqb+G+tTOBi/OnKOhLKghp7diil8dwBx8jaVw2+
45
+
46
+ vfPcHDqz0OtHW/g2q3Y849LoINY8X27EvP0AaCrkrSUhX6736VvUh0gAPBdBQQfj
47
+
48
+ Rx1K02ODN+9Ezaa/+0cgt+bNFLM=
49
+
36
50
  -----END CERTIFICATE-----
37
51
 
38
- date: 2010-11-26 00:00:00 -05:00
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/ip_address
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
- hash: 3
67
- segments:
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
- hash: 3
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.3.7
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�$.���:{���a0��&G�����_JZd����ݴ�S�9[��*�w���r�-Z��Ĥ�Tp>/p�@T"���%��7th�C���;��Z��������;��m�P]iܩ�����>�wi���44�`����$���]+��O��t/QtY��]%~����A)�<C-3�c"&�ޱ�/�憄���R�J[+ r�گ�Q5n ���T�ڎ�!Cl�?�\|ڞ�$ ����DGi�Z��ҥ`�,WU������|
1
+ &7[�y��^q��P�9��/hp�a��.b��!d9X���ߢ7q��;Z>�ij1� a�������m�/"NIB`���`G{ohOs{3��<�E�n��s����S'��gu*w�S�;�e ]a���C��.ŋ+�IdnkW���De^�������;�rr%����&�����[Z�-����Z%�m��R�߽Fg&f�h_����e.�X���]���w6�:j>V�������ԡi�����C$*ն