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
35
36
# File 'lib/pio/ipv4_address.rb', line 25

def initialize(addr)
  case addr
  when Integer
    @value = IPAddr.new(addr, Socket::AF_INET)
  when String
    @value = IPAddr.new(addr)
  when IPv4Address
    @value = addr.value
  else
    fail TypeError, "Invalid IPv4 address: #{ addr.inspect }"
  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.



90
91
92
# File 'lib/pio/ipv4_address.rb', line 90

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.



96
97
98
# File 'lib/pio/ipv4_address.rb', line 96

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.



102
103
104
# File 'lib/pio/ipv4_address.rb', line 102

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.



108
109
110
# File 'lib/pio/ipv4_address.rb', line 108

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.



115
116
117
# File 'lib/pio/ipv4_address.rb', line 115

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

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

Returns the IPv4 address masked with masklen.

Returns:



83
84
85
# File 'lib/pio/ipv4_address.rb', line 83

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

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

Returns the IPv4 address masked with masklen.

Returns:



75
76
77
78
# File 'lib/pio/ipv4_address.rb', line 75

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

#prefixlenNumber

Returns prefix length of IPv4 address.

Returns:

  • (Number)

    prefix length of IPv4 address.



50
51
52
53
54
55
56
57
# File 'lib/pio/ipv4_address.rb', line 50

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.



61
62
63
64
65
# File 'lib/pio/ipv4_address.rb', line 61

def to_a
  to_s.split('.').map do | each |
    each.to_i
  end
end

#to_aryArray

Returns an array of decimal numbers converted from IPv4 address.

Returns:

  • (Array)

    an array of decimal numbers converted from IPv4 address.



69
70
71
# File 'lib/pio/ipv4_address.rb', line 69

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.



121
122
123
# File 'lib/pio/ipv4_address.rb', line 121

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