class Net::DNS::RR::Types
This is an auxiliary class to handle RR type field in a DNS packet.
Constants
- TYPES
Public Class Methods
Be able to control the default type to assign when type is
nil
. Default to A
# File lib/net/dns/rr/types.rb, line 76 def self.default=(str) if TYPES.has_key? str @@default = TYPES[str] else raise ArgumentError, "Unknown type #{str}" end end
Creates a new object representing an RR type.
Performs some checks on the argument validity too. Il type
is
nil
, the default value is ANY
or the one set with
::default=
# File lib/net/dns/rr/types.rb, line 121 def initialize(type) case type when String # type in the form "A" or "NS" new_from_string(type.upcase) when Fixnum # type in numeric form new_from_num(type) when nil # default type, control with Types.default= @str = TYPES.invert[@@default] @num = @@default else raise ArgumentError, "Wrong type class: #{type.class}" end end
Gives in output the keys from the Types
hash in a format
suited for regexps
# File lib/net/dns/rr/types.rb, line 113 def self.regexp # Longest ones go first, so the regex engine will match AAAA before A. TYPES.keys.sort { |a,b| b.length <=> a.length }.join("|") end
Returns the type in string format, as “A” or “NS”, given the numeric value
# File lib/net/dns/rr/types.rb, line 98 def self.to_str(type) case type when Fixnum if TYPES.invert.has_key? type TYPES.invert[type] else raise ArgumentError, "Unknown type number #{type}" end else raise ArgumentError, "Wrong type class: #{type.class}" end end
Checks whether type
is a valid RR
type.
# File lib/net/dns/rr/types.rb, line 85 def self.valid?(type) case type when String TYPES.has_key?(type) when Fixnum TYPES.invert.has_key?(type) else raise ArgumentError, "Wrong type class: #{type.class}" end end
Public Instance Methods
Returns the type in number format (default for normal use)
# File lib/net/dns/rr/types.rb, line 140 def inspect @num end
Returns the type in numeric format, usable by the pack methods for data transfers
# File lib/net/dns/rr/types.rb, line 152 def to_i @num.to_i end
Returns the type in string format, i.d. “A” or “NS” or such a string.
# File lib/net/dns/rr/types.rb, line 146 def to_s @str end
# File lib/net/dns/rr/types.rb, line 156 def to_str @num.to_s end
Private Instance Methods
Contructor for numeric data type.
# File lib/net/dns/rr/types.rb, line 180 def new_from_num(type) if TYPES.invert.has_key? type @num = type @str = TYPES.invert[type] else raise ArgumentError, "Unkown type number #{type}" end end
Constructor for string data type.
# File lib/net/dns/rr/types.rb, line 164 def new_from_string(type) case type when /^TYPE\d+/ # TODO!!! else # String with name of type if TYPES.has_key? type @str = type @num = TYPES[type] else raise ArgumentError, "Unknown type #{type}" end end end