Class: Resolv::IPv4

Inherits:
Object
  • Object
show all
Defined in:
lib/logmerge/resolv.rb

Constant Summary collapse

Regex =
/\A(\d+)\.(\d+)\.(\d+)\.(\d+)\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ IPv4

Returns a new instance of IPv4.



1743
1744
1745
1746
1747
1748
# File 'lib/logmerge/resolv.rb', line 1743

def initialize(address)
  unless address.kind_of?(String) && address.length == 4
    raise ArgumentError.new('IPv4 address must be 4 bytes')
  end
  @address = address
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



1749
1750
1751
# File 'lib/logmerge/resolv.rb', line 1749

def address
  @address
end

Class Method Details

.create(arg) ⇒ Object



1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
# File 'lib/logmerge/resolv.rb', line 1725

def self.create(arg)
  case arg
  when IPv4
    return arg
  when Regex
    if (0..255) === (a = $1.to_i) &&
       (0..255) === (b = $2.to_i) &&
       (0..255) === (c = $3.to_i) &&
       (0..255) === (d = $4.to_i)
      return self.new([a, b, c, d].pack("CCCC"))
    else
      raise ArgumentError.new("IPv4 address with invalid value: " + arg)
    end
  else
    raise ArgumentError.new("cannot interpret as IPv4 address: #{arg.inspect}")
  end
end

Instance Method Details

#==(other) ⇒ Object



1764
1765
1766
# File 'lib/logmerge/resolv.rb', line 1764

def ==(other)
  return @address == other.address
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


1768
1769
1770
# File 'lib/logmerge/resolv.rb', line 1768

def eql?(other)
  return self == other
end

#hashObject



1772
1773
1774
# File 'lib/logmerge/resolv.rb', line 1772

def hash
  return @address.hash
end

#inspectObject



1755
1756
1757
# File 'lib/logmerge/resolv.rb', line 1755

def inspect
  return "#<#{self.class} #{self.to_s}>"
end

#to_nameObject



1759
1760
1761
1762
# File 'lib/logmerge/resolv.rb', line 1759

def to_name
  return DNS::Name.create(
    '%d.%d.%d.%d.in-addr.arpa.' % @address.unpack('CCCC').reverse)
end

#to_sObject



1751
1752
1753
# File 'lib/logmerge/resolv.rb', line 1751

def to_s
  return sprintf("%d.%d.%d.%d", *@address.unpack("CCCC"))
end