Class: Philiprehberger::IpAddr::Address

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/philiprehberger/ip_addr.rb

Overview

Parsed IP address wrapper

Instance Method Summary collapse

Constructor Details

#initialize(ip_string) ⇒ Address

Returns a new instance of Address.

Parameters:

  • ip_string (String)

    IP address string



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.message}"
end

Instance Method Details

#<=>(other) ⇒ Integer?

Returns comparison result.

Returns:

  • (Integer, nil)

    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

Returns true if this is a link-local address.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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

#predAddress

Returns previous IP address.

Returns:

  • (Address)

    previous IP address

Raises:



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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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

#succAddress

Returns next IP address.

Returns:



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_iInteger

Returns numeric representation of the address.

Returns:

  • (Integer)

    numeric representation of the address



73
74
75
# File 'lib/philiprehberger/ip_addr.rb', line 73

def to_i
  @addr.to_i
end

#to_sString

Returns string representation of the address.

Returns:

  • (String)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    true if this is an IPv6 address



28
29
30
# File 'lib/philiprehberger/ip_addr.rb', line 28

def v6?
  @addr.ipv6?
end