Class: Resolv::DNS::Name

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

Overview

A representation of a DNS name.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(labels, absolute = true) ⇒ Name

:nodoc:



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

def initialize(labels, absolute=true) # :nodoc:
  @labels = labels
  @absolute = absolute
end

Class Method Details

.create(arg) ⇒ Object

Creates a new DNS name from arg. arg can be:

Name

returns arg.

String

Creates a new Name.



1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
# File 'lib/rubysl/resolv/resolv.rb', line 1052

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?

:nodoc:



1079
1080
1081
1082
# File 'lib/rubysl/resolv/resolv.rb', line 1079

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

#[](i) ⇒ Object

:nodoc:



1120
1121
1122
# File 'lib/rubysl/resolv/resolv.rb', line 1120

def [](i) # :nodoc:
  return @labels[i]
end

#absolute?Boolean

True if this name is absolute.

Returns:

  • (Boolean)


1075
1076
1077
# File 'lib/rubysl/resolv/resolv.rb', line 1075

def absolute?
  return @absolute
end

#hashObject

:nodoc:



1108
1109
1110
# File 'lib/rubysl/resolv/resolv.rb', line 1108

def hash # :nodoc:
  return @labels.hash ^ @absolute.hash
end

#inspectObject

:nodoc:



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

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

#lengthObject

:nodoc:



1116
1117
1118
# File 'lib/rubysl/resolv/resolv.rb', line 1116

def length # :nodoc:
  return @labels.length
end

#subdomain_of?(other) ⇒ Boolean

Returns true if other is a subdomain.

Example:

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)


1100
1101
1102
1103
1104
1105
1106
# File 'lib/rubysl/resolv/resolv.rb', line 1100

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

:nodoc:



1112
1113
1114
# File 'lib/rubysl/resolv/resolv.rb', line 1112

def to_a # :nodoc:
  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.

Example:

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"


1135
1136
1137
# File 'lib/rubysl/resolv/resolv.rb', line 1135

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