Class: Resolv::DNS::Name

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(labels, absolute = true) ⇒ Name

Returns a new instance of Name.



1022
1023
1024
1025
# File 'lib/resolv.rb', line 1022

def initialize(labels, absolute=true)
  @labels = labels
  @absolute = absolute
end

Class Method Details

.create(arg) ⇒ Object



1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
# File 'lib/resolv.rb', line 1011

def self.create(arg)
  case arg
  when Name
    return arg
  when String
    return Name.new(Label.split(arg), /\.\z/ =~ arg ? true : false)
  else
    raise ArgumentError.new("cannot interpret as DNS name: #{arg.inspect}")
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



1035
1036
1037
1038
# File 'lib/resolv.rb', line 1035

def ==(other)
  return false unless Name === other
  return @labels == other.to_a && @absolute == other.absolute?
end

#[](i) ⇒ Object



1071
1072
1073
# File 'lib/resolv.rb', line 1071

def [](i)
  return @labels[i]
end

#absolute?Boolean

Returns:

  • (Boolean)


1031
1032
1033
# File 'lib/resolv.rb', line 1031

def absolute?
  return @absolute
end

#hashObject



1059
1060
1061
# File 'lib/resolv.rb', line 1059

def hash
  return @labels.hash ^ @absolute.hash
end

#inspectObject



1027
1028
1029
# File 'lib/resolv.rb', line 1027

def inspect
  "#<#{self.class}: #{self.to_s}#{@absolute ? '.' : ''}>"
end

#lengthObject



1067
1068
1069
# File 'lib/resolv.rb', line 1067

def length
  return @labels.length
end

#subdomain_of?(other) ⇒ Boolean

tests subdomain-of relation.

domain = Resolv::DNS::Name.create("y.z")
p Resolv::DNS::Name.create("w.x.y.z").subdomain_of?(domain) #=> true
p Resolv::DNS::Name.create("x.y.z").subdomain_of?(domain) #=> true
p Resolv::DNS::Name.create("y.z").subdomain_of?(domain) #=> false
p Resolv::DNS::Name.create("z").subdomain_of?(domain) #=> false
p Resolv::DNS::Name.create("x.y.z.").subdomain_of?(domain) #=> false
p Resolv::DNS::Name.create("w.z").subdomain_of?(domain) #=> false

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


1051
1052
1053
1054
1055
1056
1057
# File 'lib/resolv.rb', line 1051

def subdomain_of?(other)
  raise ArgumentError, "not a domain name: #{other.inspect}" unless Name === other
  return false if @absolute != other.absolute?
  other_len = other.length
  return false if @labels.length <= other_len
  return @labels[-other_len, other_len] == other.to_a
end

#to_aObject



1063
1064
1065
# File 'lib/resolv.rb', line 1063

def to_a
  return @labels
end

#to_sObject

returns the domain name as a string.

The domain name doesn't have a trailing dot even if the name object is absolute.

p Resolv::DNS::Name.create("x.y.z.").to_s #=> "x.y.z"
p Resolv::DNS::Name.create("x.y.z").to_s #=> "x.y.z"


1083
1084
1085
# File 'lib/resolv.rb', line 1083

def to_s
  return @labels.join('.')
end