Class: Pio::IPv4Address

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pio/ipv4_address.rb

Overview

IPv4 Address

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(addr) ⇒ IPv4Address

Creates a Pio::IPv4Address instance object as a proxy to IPAddr class.

Parameters:

  • addr (String|Number)

    an IPv4 address specified either as a String or Number.

Raises:

  • (TypeError)

    invalid address if supplied argument is invalid



25
26
27
28
29
30
31
32
33
34
# File 'lib/pio/ipv4_address.rb', line 25

def initialize(addr)
  @value = case addr
           when Integer
             IPAddr.new(addr, Socket::AF_INET)
           when String
             IPAddr.new(addr)
           else
             addr.value
           end
end

Instance Attribute Details

#valueRange (readonly)

Returns Creates a Range object for the network address.

Returns:

  • (Range)

    Creates a Range object for the network address.



12
13
14
# File 'lib/pio/ipv4_address.rb', line 12

def value
  @value
end

Instance Method Details

#class_a?bool

Returns true if the address belongs to class A.

Returns:

  • (bool)

    Returns true if the address belongs to class A.



94
95
96
# File 'lib/pio/ipv4_address.rb', line 94

def class_a?
  mask(1).to_s == '0.0.0.0'
end

#class_b?bool

Returns true if the address belongs to class B.

Returns:

  • (bool)

    Returns true if the address belongs to class B.



100
101
102
# File 'lib/pio/ipv4_address.rb', line 100

def class_b?
  mask(2).to_s == '128.0.0.0'
end

#class_c?bool

Returns true if the address belongs to class C.

Returns:

  • (bool)

    Returns true if the address belongs to class C.



106
107
108
# File 'lib/pio/ipv4_address.rb', line 106

def class_c?
  mask(3).to_s == '192.0.0.0'
end

#class_d?bool Also known as: multicast?

Returns true if the address belongs to class D.

Returns:

  • (bool)

    Returns true if the address belongs to class D.



112
113
114
# File 'lib/pio/ipv4_address.rb', line 112

def class_d?
  mask(4).to_s == '224.0.0.0'
end

#class_e?bool

Returns true if the address belongs to class E.

Returns:

  • (bool)

    Returns true if the address belongs to class E.



119
120
121
# File 'lib/pio/ipv4_address.rb', line 119

def class_e?
  mask(4).to_s == '240.0.0.0'
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/pio/ipv4_address.rb', line 47

def eql?(other)
  @value == other.value
end

#hashObject



51
52
53
# File 'lib/pio/ipv4_address.rb', line 51

def hash
  to_s.hash
end

#mask(masklen) ⇒ IPv4Address Also known as: prefix

Returns the IPv4 address masked with masklen.

Returns:



87
88
89
# File 'lib/pio/ipv4_address.rb', line 87

def mask(masklen)
  clone.mask!(masklen)
end

#mask!(masklen) ⇒ IPv4Address Also known as: prefix!

Returns the IPv4 address masked with masklen.

Returns:



79
80
81
82
# File 'lib/pio/ipv4_address.rb', line 79

def mask!(masklen)
  @value = @value.mask(masklen)
  self
end

#prefixlenNumber

Returns prefix length of IPv4 address.

Returns:

  • (Number)

    prefix length of IPv4 address.



56
57
58
59
60
61
62
63
# File 'lib/pio/ipv4_address.rb', line 56

def prefixlen
  netmask = to_range.first.to_i ^ to_range.last.to_i
  if netmask > 0
    32 - format('%b', netmask).length
  else
    32
  end
end

#to_aArray

Returns an array of decimal numbers converted from IPv4 address.

Returns:

  • (Array)

    an array of decimal numbers converted from IPv4 address.



67
68
69
# File 'lib/pio/ipv4_address.rb', line 67

def to_a
  to_s.split('.').map(&:to_i)
end

#to_aryArray

Returns an array of decimal numbers converted from IPv4 address.

Returns:

  • (Array)

    an array of decimal numbers converted from IPv4 address.



73
74
75
# File 'lib/pio/ipv4_address.rb', line 73

def to_ary
  to_a
end

#unicast?bool

Returns true if the address is unicast address.

Returns:

  • (bool)

    Returns true if the address is unicast address.



125
126
127
# File 'lib/pio/ipv4_address.rb', line 125

def unicast?
  class_a? || class_b? || class_c?
end