class Net::DNS::RR::Classes

Net::DNS::Classes

This is an auxiliary class to handle Net::DNS::RR class field in a DNS packet.

Constants

CLASSES

Hash with the values of each RR class stored with the respective id number.

Public Class Methods

default=(str) click to toggle source

Be able to control the default class to assign when cls argument is nil. Default to IN

# File lib/net/dns/rr/classes.rb, line 71
def self.default=(str)
  if CLASSES[str]
    @@default = CLASSES[str]
  else
    raise ArgumentError, "Unknown class `#{str}'"
  end
end
new(cls) click to toggle source

Creates a new object representing an RR class. Performs some checks on the argument validity too. Il cls is nil, the default value is ANY or the one set with ::default=

# File lib/net/dns/rr/classes.rb, line 31
def initialize(cls)
  case cls
    when String
      initialize_from_str(cls)
    when Fixnum
      initialize_from_num(cls)
    when nil
      initialize_from_num(@@default)
  end

  if @str.nil? || @num.nil?
    raise ArgumentError, "Unable to create a `Classes' from `#{cls}'"
  end
end
regexp() click to toggle source

Gives in output the keys from the Classes hash in a format suited for regexps

# File lib/net/dns/rr/classes.rb, line 110
def self.regexp
  CLASSES.keys.sort.join("|")
end
valid?(cls) click to toggle source

Returns whether cls is a valid RR class.

Net::DNS::RR::Classes.valid?("IN")
# => true
Net::DNS::RR::Classes.valid?(1)
# => true
Net::DNS::RR::Classes.valid?("Q")
# => false
Net::DNS::RR::Classes.valid?(256)
# => false
Net::DNS::RR::Classes.valid?(Hash.new)
# => ArgumentError

FIXME: valid? should never raise.

Raises

ArgumentError

if cls isn't either a String or a Fixnum

# File lib/net/dns/rr/classes.rb, line 97
def self.valid?(cls)
  case cls
    when String
      CLASSES.has_key?(cls)
    when Fixnum
      CLASSES.invert.has_key?(cls)
    else
      raise ArgumentError, "Wrong cls class: #{cls.class}"
  end
end

Public Instance Methods

inspect() click to toggle source

Returns the class in number format (default for normal use)

FIXME: inspect must return a String.

# File lib/net/dns/rr/classes.rb, line 51
def inspect
  @num
end
to_i() click to toggle source

Returns the class in numeric format, usable by the pack methods for data transfers.

# File lib/net/dns/rr/classes.rb, line 63
def to_i
  @num.to_i
end
to_s() click to toggle source

Returns the class in string format, ex. “IN” or “CH” or such a string.

# File lib/net/dns/rr/classes.rb, line 57
def to_s
  @str.to_s
end

Private Instance Methods

initialize_from_num(num) click to toggle source

Initialize a new instance from the Class value.

# File lib/net/dns/rr/classes.rb, line 124
def initialize_from_num(num)
  key = num.to_i
  @num, @str = key, CLASSES.invert[key]
end
initialize_from_str(str) click to toggle source

Initialize a new instance from a Class name.

# File lib/net/dns/rr/classes.rb, line 118
def initialize_from_str(str)
  key = str.to_s.upcase
  @num, @str = CLASSES[key], key
end