Class: Resolv::DNS::Name
- Inherits:
-
Object
- Object
- Resolv::DNS::Name
- Defined in:
- lib/resolv.rb
Overview
A representation of a DNS name.
Class Method Summary collapse
-
.create(arg) ⇒ Object
Creates a new DNS name from
arg.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
:nodoc:.
-
#[](i) ⇒ Object
:nodoc:.
-
#absolute? ⇒ Boolean
True if this name is absolute.
-
#hash ⇒ Object
:nodoc:.
-
#initialize(labels, absolute = true) ⇒ Name
constructor
:nodoc:.
-
#inspect ⇒ Object
:nodoc:.
-
#length ⇒ Object
:nodoc:.
-
#subdomain_of?(other) ⇒ Boolean
Returns true if
otheris a subdomain. -
#to_a ⇒ Object
:nodoc:.
-
#to_s ⇒ Object
returns the domain name as a string.
Constructor Details
#initialize(labels, absolute = true) ⇒ Name
:nodoc:
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 |
# File 'lib/resolv.rb', line 1333 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.
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 |
# File 'lib/resolv.rb', line 1303 def self.create(arg) case arg when Name return arg when String # A hostname is runtime data rather than a programming mistake, so # both size limits surface as ResolvError to stay rescuable alongside # the rest of name resolution. The type check below is a caller # mistake and keeps raising ArgumentError. begin labels = Label.split(arg) rescue ArgumentError => e raise ResolvError.new(e.) end # Label::Str enforces the per-label limit. Only the total is knowable # here, and it counts the encoded form, so size starts at 1 for the # root label's terminating zero octet. [RFC 1035 2.3.4, 3.1] size = 1 labels.each do |label| size += 1 + label.string.bytesize if size > 255 raise ResolvError.new("DNS name is too long (#{size} octets, max 255): #{arg.inspect}") end end return Name.new(labels, /\.\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:
1357 1358 1359 1360 1361 |
# File 'lib/resolv.rb', line 1357 def ==(other) # :nodoc: return false unless Name === other return false unless @absolute == other.absolute? return @labels == other.to_a end |
#[](i) ⇒ Object
:nodoc:
1399 1400 1401 |
# File 'lib/resolv.rb', line 1399 def [](i) # :nodoc: return @labels[i] end |
#absolute? ⇒ Boolean
True if this name is absolute.
1353 1354 1355 |
# File 'lib/resolv.rb', line 1353 def absolute? return @absolute end |
#hash ⇒ Object
:nodoc:
1387 1388 1389 |
# File 'lib/resolv.rb', line 1387 def hash # :nodoc: return @labels.hash ^ @absolute.hash end |
#inspect ⇒ Object
:nodoc:
1346 1347 1348 |
# File 'lib/resolv.rb', line 1346 def inspect # :nodoc: "#<#{self.class}: #{self}#{@absolute ? '.' : ''}>" end |
#length ⇒ Object
:nodoc:
1395 1396 1397 |
# File 'lib/resolv.rb', line 1395 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
1379 1380 1381 1382 1383 1384 1385 |
# File 'lib/resolv.rb', line 1379 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_a ⇒ Object
:nodoc:
1391 1392 1393 |
# File 'lib/resolv.rb', line 1391 def to_a # :nodoc: return @labels end |
#to_s ⇒ Object
1414 1415 1416 |
# File 'lib/resolv.rb', line 1414 def to_s return @labels.join('.') end |