Class: Resolv::DNS::Name

Inherits:
Object
  • Object
show all
Defined in:
lib/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:



1180
1181
1182
1183
# File 'lib/resolv.rb', line 1180

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.



1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
# File 'lib/resolv.rb', line 1169

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:



1196
1197
1198
1199
# File 'lib/resolv.rb', line 1196

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

#[](i) ⇒ Object

:nodoc:



1237
1238
1239
# File 'lib/resolv.rb', line 1237

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

#absolute?Boolean

True if this name is absolute.

Returns:

  • (Boolean)


1192
1193
1194
# File 'lib/resolv.rb', line 1192

def absolute?
  return @absolute
end

#hashObject

:nodoc:



1225
1226
1227
# File 'lib/resolv.rb', line 1225

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

#inspectObject

:nodoc:



1185
1186
1187
# File 'lib/resolv.rb', line 1185

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

#lengthObject

:nodoc:



1233
1234
1235
# File 'lib/resolv.rb', line 1233

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)


1217
1218
1219
1220
1221
1222
1223
# File 'lib/resolv.rb', line 1217

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:



1229
1230
1231
# File 'lib/resolv.rb', line 1229

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"


1252
1253
1254
# File 'lib/resolv.rb', line 1252

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