Class: Philiprehberger::IpAddr::Address
- Inherits:
-
Object
- Object
- Philiprehberger::IpAddr::Address
- Includes:
- Comparable
- Defined in:
- lib/philiprehberger/ip_addr.rb
Overview
Parsed IP address wrapper
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer?
Comparison result.
-
#initialize(ip_string) ⇒ Address
constructor
A new instance of Address.
-
#link_local? ⇒ Boolean
True if this is a link-local address.
-
#loopback? ⇒ Boolean
True if this is a loopback address.
-
#multicast? ⇒ Boolean
True if this is a multicast address.
-
#pred ⇒ Address
Previous IP address.
-
#private? ⇒ Boolean
True if this is a private address.
-
#reserved? ⇒ Boolean
True if this is any special-purpose address.
-
#succ ⇒ Address
Next IP address.
-
#to_i ⇒ Integer
Numeric representation of the address.
-
#to_s ⇒ String
String representation of the address.
-
#v4? ⇒ Boolean
True if this is an IPv4 address.
-
#v6? ⇒ Boolean
True if this is an IPv6 address.
Constructor Details
#initialize(ip_string) ⇒ Address
Returns a new instance of Address.
15 16 17 18 19 20 |
# File 'lib/philiprehberger/ip_addr.rb', line 15 def initialize(ip_string) @raw = ip_string.to_s.strip @addr = ::IPAddr.new(@raw) rescue ::IPAddr::InvalidAddressError => e raise Error, "Invalid IP address: #{e.}" end |
Instance Method Details
#<=>(other) ⇒ Integer?
Returns comparison result.
83 84 85 86 87 |
# File 'lib/philiprehberger/ip_addr.rb', line 83 def <=>(other) return nil unless other.is_a?(Address) to_i <=> other.to_i end |
#link_local? ⇒ Boolean
Returns true if this is a link-local address.
57 58 59 60 61 62 63 64 65 |
# File 'lib/philiprehberger/ip_addr.rb', line 57 def link_local? if v4? ::IPAddr.new('169.254.0.0/16').include?(@addr) elsif v6? ::IPAddr.new('fe80::/10').include?(@addr) else false end end |
#loopback? ⇒ Boolean
Returns true if this is a loopback address.
41 42 43 |
# File 'lib/philiprehberger/ip_addr.rb', line 41 def loopback? @addr.loopback? end |
#multicast? ⇒ Boolean
Returns true if this is a multicast address.
46 47 48 49 50 51 52 53 54 |
# File 'lib/philiprehberger/ip_addr.rb', line 46 def multicast? if v4? ::IPAddr.new('224.0.0.0/4').include?(@addr) elsif v6? ::IPAddr.new('ff00::/8').include?(@addr) else false end end |
#pred ⇒ Address
Returns previous IP address.
95 96 97 98 99 |
# File 'lib/philiprehberger/ip_addr.rb', line 95 def pred raise Error, 'Cannot decrement below 0.0.0.0' if to_i.zero? Address.new(@addr.ipv4? ? int_to_v4(to_i - 1) : int_to_v6(to_i - 1)) end |
#private? ⇒ Boolean
Returns true if this is a private address.
33 34 35 36 37 38 |
# File 'lib/philiprehberger/ip_addr.rb', line 33 def private? return private_v4? if v4? return private_v6? if v6? false end |
#reserved? ⇒ Boolean
Returns true if this is any special-purpose address.
68 69 70 |
# File 'lib/philiprehberger/ip_addr.rb', line 68 def reserved? private? || loopback? || multicast? || link_local? end |
#succ ⇒ Address
Returns next IP address.
90 91 92 |
# File 'lib/philiprehberger/ip_addr.rb', line 90 def succ Address.new(@addr.ipv4? ? int_to_v4(to_i + 1) : int_to_v6(to_i + 1)) end |
#to_i ⇒ Integer
Returns numeric representation of the address.
73 74 75 |
# File 'lib/philiprehberger/ip_addr.rb', line 73 def to_i @addr.to_i end |
#to_s ⇒ String
Returns string representation of the address.
78 79 80 |
# File 'lib/philiprehberger/ip_addr.rb', line 78 def to_s @addr.to_s end |
#v4? ⇒ Boolean
Returns true if this is an IPv4 address.
23 24 25 |
# File 'lib/philiprehberger/ip_addr.rb', line 23 def v4? @addr.ipv4? end |
#v6? ⇒ Boolean
Returns true if this is an IPv6 address.
28 29 30 |
# File 'lib/philiprehberger/ip_addr.rb', line 28 def v6? @addr.ipv6? end |