Class: IPLogic::IP
Constant Summary collapse
- FormatError =
Class.new(ArgumentError)
- MAXIP =
IP.new(0xFFFFFFFF)
Instance Attribute Summary collapse
-
#int ⇒ Integer
(also: #to_i, #to_int)
readonly
Get the integer representation of this IP address.
Class Method Summary collapse
-
.max ⇒ IP
The maximum IP address: 255.255.255.255.
-
.rand(cidr = CIDR.all) ⇒ IP
A random IP address.
-
.wrap(arg) ⇒ IP
(also: [])
Basic idempotent wrapper for creating addresses.
Instance Method Summary collapse
-
#+(int_ish) ⇒ IP
The result of adding int_ish to the underlying integer.
-
#-(int_ish) ⇒ IP
The result of subtracting int_ish from the underlying integer.
-
#<=>(other) ⇒ Object
IP addresses are ordered in the usual way.
-
#assert_netmask! ⇒ Object
raises an error unless this IP address is a valid netmask.
- #eql?(other) ⇒ Boolean (also: #==)
-
#hash ⇒ String
an IP uses its string representation as its hash.
-
#initialize(int, extra = {}) ⇒ IP
constructor
A new instance of IP.
- #inspect ⇒ Object
- #max(netmask) ⇒ Object
-
#netmask? ⇒ Boolean
test if this IP address is a valid netmask.
-
#octets ⇒ Array
(also: #parts)
An array of the four octets.
-
#prefix(netmask) ⇒ IP
(also: #min)
given a netmask, returns the network prefix.
-
#rest_field(netmask) ⇒ IP
(also: #rest)
given a netmask, returns the “rest field” - the bits not covered by the netmask.
-
#succ ⇒ IP
This allows IP “ranges” with (ip1..ip2).
-
#to_s ⇒ String
The usual string representation of the ip address.
Constructor Details
#initialize(int, extra = {}) ⇒ IP
Returns a new instance of IP.
86 87 88 |
# File 'lib/iplogic/ip.rb', line 86 def initialize(int, extra={}) @int = int end |
Instance Attribute Details
#int ⇒ Integer (readonly) Also known as: to_i, to_int
Get the integer representation of this IP address
80 81 82 |
# File 'lib/iplogic/ip.rb', line 80 def int @int end |
Class Method Details
.max ⇒ IP
Returns The maximum IP address: 255.255.255.255.
92 93 94 |
# File 'lib/iplogic/ip.rb', line 92 def self.max MAXIP end |
.rand(cidr = CIDR.all) ⇒ IP
Returns a random IP address. Useful for mocks / tests.
61 62 63 64 |
# File 'lib/iplogic/ip.rb', line 61 def rand(cidr=CIDR.all) cidr = CIDR.wrap(cidr) cidr.min + Kernel.rand(cidr.size) end |
.wrap(arg) ⇒ IP Also known as: []
Basic idempotent wrapper for creating addresses.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/iplogic/ip.rb', line 32 def wrap(arg) return arg if arg.is_a? IP int = case arg when Array octets_to_int(arg) when String octets_to_int(arg.split('.')) when Fixnum arg when nil 0 else raise FormatError, "IP: Unable to parse #{arg.inspect}" end unless (0..0xFFFFFFFF).include? int raise FormatError, "IP: Address #{arg.inspect} out of range" end return new(int) end |
Instance Method Details
#+(int_ish) ⇒ IP
Returns the result of adding int_ish to the underlying integer.
136 137 138 |
# File 'lib/iplogic/ip.rb', line 136 def +(int_ish) IP.wrap(int + int_ish.to_i) end |
#-(int_ish) ⇒ IP
Returns the result of subtracting int_ish from the underlying integer.
142 143 144 |
# File 'lib/iplogic/ip.rb', line 142 def -(int_ish) self + (-int_ish.to_i) end |
#<=>(other) ⇒ Object
IP addresses are ordered in the usual way
130 131 132 |
# File 'lib/iplogic/ip.rb', line 130 def <=>(other) self.int <=> other.int end |
#assert_netmask! ⇒ Object
raises an error unless this IP address is a valid netmask.
188 189 190 191 192 |
# File 'lib/iplogic/ip.rb', line 188 def assert_netmask! raise ArgumentError, <<-msg.strip unless netmask? #{self.inspect} is not a valid netmask. msg end |
#eql?(other) ⇒ Boolean Also known as: ==
123 124 125 |
# File 'lib/iplogic/ip.rb', line 123 def eql?(other) self.int == IP.wrap(other).int end |
#hash ⇒ String
an IP uses its string representation as its hash
119 120 121 |
# File 'lib/iplogic/ip.rb', line 119 def hash self.to_s.hash end |
#inspect ⇒ Object
113 114 115 |
# File 'lib/iplogic/ip.rb', line 113 def inspect "#<IP [ #{self} ]>" end |
#max(netmask) ⇒ Object
163 164 165 |
# File 'lib/iplogic/ip.rb', line 163 def max(netmask) CIDR.wrap(self, netmask).max end |
#netmask? ⇒ Boolean
test if this IP address is a valid netmask.
180 181 182 183 184 185 |
# File 'lib/iplogic/ip.rb', line 180 def netmask? maxint32 = 0xFFFFFFFF (0..32).any? do |i| (int-1) + (1 << i) == maxint32 end end |
#octets ⇒ Array Also known as: parts
Returns an array of the four octets.
100 101 102 103 104 105 |
# File 'lib/iplogic/ip.rb', line 100 def octets @octets ||= begin rad = int.radix(256) [0]*([4-rad.size,0].max) + rad end end |
#prefix(netmask) ⇒ IP Also known as: min
given a netmask, returns the network prefix.
158 159 160 |
# File 'lib/iplogic/ip.rb', line 158 def prefix(netmask) CIDR.wrap(self, netmask).min end |
#rest_field(netmask) ⇒ IP Also known as: rest
given a netmask, returns the “rest field” - the bits not covered by the netmask. See CIDR#rest_field.
IP['1.2.3.4'].rest_field # => #<IP [ 0.0.3.4 ]>
174 175 176 |
# File 'lib/iplogic/ip.rb', line 174 def rest_field(netmask) CIDR.wrap(self, netmask).rest_field end |
#succ ⇒ IP
This allows IP “ranges” with (ip1..ip2)
148 149 150 |
# File 'lib/iplogic/ip.rb', line 148 def succ self + 1 end |
#to_s ⇒ String
Returns the usual string representation of the ip address.
109 110 111 |
# File 'lib/iplogic/ip.rb', line 109 def to_s octets.join('.') end |