better_ipaddr 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/better_ipaddr/classes.rb +121 -8
- data/lib/better_ipaddr/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7b4aa183d3f44f56ff13ead56db0d6a6644d96be
|
4
|
+
data.tar.gz: b8d6cfb47a73be0c2d8ccb7baee79ef9c9e88142
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1dc675f631f316c5b265f7d6a30e194ef03c4717148c977c5c28c1b18e0596ce640bc42973e3307d225e794842e82a9e8f54ed42303c20756eaccbb5ec5eee12
|
7
|
+
data.tar.gz: f68835b94ceb6ec6aec81db9136146c38c45a309d4a57f90ca603985e58790c5d3480f898bc6442fae8e989246ce0661487e296e5ac289ed7d9b9b25a7479d6f
|
@@ -11,21 +11,134 @@ class IPAddr
|
|
11
11
|
cls.extend BetterIpaddr::ClassMethods
|
12
12
|
end
|
13
13
|
|
14
|
+
# Create an IPAddr from the given object.
|
15
|
+
#
|
16
|
+
# Returns nil if the object is of a type that can't be converted to an
|
17
|
+
# IPAddr.
|
18
|
+
#
|
19
|
+
# @param address [Integer, IPAddr, String]
|
20
|
+
# @param mask [Integer, IPAddr, String, Nil]
|
21
|
+
# @param family [Integer]
|
22
|
+
# @return [IPAddr, Nil]
|
14
23
|
def self.[](address, mask = nil, family: self::FAMILY)
|
15
|
-
|
24
|
+
prefix_length = mask && object_to_prefix_length(mask, family)
|
25
|
+
|
26
|
+
case address
|
16
27
|
when Integer
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
28
|
+
from_integer(address, prefix_length, family: family)
|
29
|
+
when IPAddr
|
30
|
+
from_ipaddr(address, prefix_length, family: family)
|
31
|
+
when String
|
32
|
+
from_string(address, prefix_length, family: family)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Create an IPAddr from an Integer.
|
37
|
+
#
|
38
|
+
# @param address [Integer]
|
39
|
+
# @param mask [Integer, String] a netmask or prefix length
|
40
|
+
# @param family [Integer, Nil]
|
41
|
+
# @return [IPAddr]
|
42
|
+
def self.from_integer(address, prefix_length, family: self::FAMILY)
|
43
|
+
new(address, family).mask(prefix_length || FAMILY_TO_BIT_LENGTH[family])
|
44
|
+
end
|
45
|
+
|
46
|
+
# Create an IPAddr from an IPAddr.
|
47
|
+
#
|
48
|
+
# @param address [IPAddr]
|
49
|
+
# @param mask [Integer, String] a netmask or prefix length
|
50
|
+
# @param family [Integer, Nil]
|
51
|
+
# @return [IPAddr]
|
52
|
+
def self.from_ipaddr(address, prefix_length, family: self::FAMILY)
|
53
|
+
new(address.to_i, family).mask(prefix_length || address.prefix_length)
|
54
|
+
end
|
55
|
+
|
56
|
+
# Create an IPAddr from a String.
|
57
|
+
#
|
58
|
+
# @param address [String]
|
59
|
+
# @param mask [Integer, String] a netmask or prefix length
|
60
|
+
# @param family [Integer, Nil]
|
61
|
+
# @return [IPAddr]
|
62
|
+
def self.from_string(address, mask = nil, family: self::FAMILY)
|
63
|
+
if mask
|
64
|
+
new(address, family).mask(mask)
|
24
65
|
else
|
25
66
|
new(address, family)
|
26
67
|
end
|
27
68
|
end
|
28
69
|
|
70
|
+
# Convert an object to a prefix length.
|
71
|
+
#
|
72
|
+
# @param mask [Integer, String]
|
73
|
+
# @param family [Integer, Nil]
|
74
|
+
# @return [Integer]
|
75
|
+
def self.object_to_prefix_length(mask, family = self::FAMILY)
|
76
|
+
case mask
|
77
|
+
when Integer
|
78
|
+
integer_to_prefix_length(mask, family)
|
79
|
+
when String
|
80
|
+
string_to_prefix_length(mask, family)
|
81
|
+
when IPAddr
|
82
|
+
ipaddr_to_prefix_length(mask, family)
|
83
|
+
else
|
84
|
+
raise ArgumentError, "Can't convert #{mask.class} to prefix length"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# Convert an integer to a prefix length.
|
89
|
+
#
|
90
|
+
# If the integer is within the range of possible prefix lengths, returns the
|
91
|
+
# same integer. Otherwise it assumes that the given integer is the integer
|
92
|
+
# representation of a netmask.
|
93
|
+
#
|
94
|
+
# Returns nil if the integer can't be converted.
|
95
|
+
#
|
96
|
+
# @param mask [Integer]
|
97
|
+
# @return [Integer]
|
98
|
+
def self.integer_to_prefix_length(mask, family = self::FAMILY)
|
99
|
+
if valid_prefix_length?(mask)
|
100
|
+
mask
|
101
|
+
else
|
102
|
+
NETMASK_TO_PREFIX_LENGTH[family][mask] ||
|
103
|
+
(raise ArgumentError, "Can't convert #{mask} to prefix length")
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# Convert a netmask represented as an IPAddr to a prefix length.
|
108
|
+
#
|
109
|
+
# Returns nil if the IPAddr can't be converted.
|
110
|
+
#
|
111
|
+
# @param mask [IPAddr]
|
112
|
+
# @return [Integer]
|
113
|
+
def self.ipaddr_to_prefix_length(mask, family = self::FAMILY)
|
114
|
+
NETMASK_TO_PREFIX_LENGTH[family][mask.to_i]
|
115
|
+
end
|
116
|
+
|
117
|
+
# Convert a string to a prefix length.
|
118
|
+
#
|
119
|
+
# Accepts the decimal representations of integers as well as netmasks in
|
120
|
+
# dotted quad notation.
|
121
|
+
#
|
122
|
+
# Returns nil if the string can't be converted.
|
123
|
+
#
|
124
|
+
# @param mask [String]
|
125
|
+
# @return [Integer]
|
126
|
+
def self.string_to_prefix_length(mask, family = self::FAMILY)
|
127
|
+
if mask =~ /^\d+$/
|
128
|
+
integer_to_prefix_length(mask.to_i, family)
|
129
|
+
else
|
130
|
+
NETMASK_TO_PREFIX_LENGTH[family][new(mask).to_i]
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
# Return true if the given number is a valid prefix length, false otherwise.
|
135
|
+
#
|
136
|
+
# @param prefix_length [Integer]
|
137
|
+
# @return [Boolean]
|
138
|
+
def self.valid_prefix_length?(prefix_length, family: self::FAMILY)
|
139
|
+
0 <= prefix_length && prefix_length <= FAMILY_TO_BIT_LENGTH[family]
|
140
|
+
end
|
141
|
+
|
29
142
|
# Convert the given string to an IPAddr subclass.
|
30
143
|
#
|
31
144
|
# @param address [String] the string to convert
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: better_ipaddr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Miller
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-09-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|