Class: Gem::Resolv::DNS::Name

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



1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 1266

def initialize(labels, absolute=true) # :nodoc:
  labels = labels.map {|label|
    case label
    when String then Label::Str.new(label)
    when Label::Str then label
    else
      raise ArgumentError, "unexpected label: #{label.inspect}"
    end
  }
  @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.



1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 1255

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:



1290
1291
1292
1293
1294
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 1290

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

#[](i) ⇒ Object

:nodoc:



1332
1333
1334
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 1332

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

#absolute?Boolean

True if this name is absolute.

Returns:

  • (Boolean)


1286
1287
1288
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 1286

def absolute?
  return @absolute
end

#hashObject

:nodoc:



1320
1321
1322
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 1320

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

#inspectObject

:nodoc:



1279
1280
1281
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 1279

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

#lengthObject

:nodoc:



1328
1329
1330
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 1328

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

#subdomain_of?(other) ⇒ Boolean

Returns true if other is a subdomain.

Example:

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

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


1312
1313
1314
1315
1316
1317
1318
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 1312

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:



1324
1325
1326
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 1324

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 Gem::Resolv::DNS::Name.create("x.y.z.").to_s #=> "x.y.z"
p Gem::Resolv::DNS::Name.create("x.y.z").to_s #=> "x.y.z"


1347
1348
1349
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 1347

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