Class: RRs::Name

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

Overview

A representation of a DNS name.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(labels, absolute = true) ⇒ Name

:nodoc:



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rrs/name.rb', line 24

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.



13
14
15
16
17
18
19
20
21
22
# File 'lib/rrs/name.rb', line 13

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:



48
49
50
51
52
# File 'lib/rrs/name.rb', line 48

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

#[](i) ⇒ Object

:nodoc:



90
91
92
# File 'lib/rrs/name.rb', line 90

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

#absolute?Boolean

True if this name is absolute.

Returns:

  • (Boolean)


44
45
46
# File 'lib/rrs/name.rb', line 44

def absolute?
  return @absolute
end

#hashObject

:nodoc:



78
79
80
# File 'lib/rrs/name.rb', line 78

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

#inspectObject

:nodoc:



37
38
39
# File 'lib/rrs/name.rb', line 37

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

#lengthObject

:nodoc:



86
87
88
# File 'lib/rrs/name.rb', line 86

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)


70
71
72
73
74
75
76
# File 'lib/rrs/name.rb', line 70

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:



82
83
84
# File 'lib/rrs/name.rb', line 82

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"


105
106
107
# File 'lib/rrs/name.rb', line 105

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