Class: Dnsruby::Resolv

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

Overview

NOTE! Beware, there is a Ruby library class named Resolv, and you may need to explicitly specify Dnsruby::Resolv to use the Dnsruby Resolv class, even if you have include’d Dnsruby.

Constant Summary collapse

ADDRESS_REGEX =

Address RegExp to use for matching IP addresses

/(?:#{IPv4::Regex})|(?:#{IPv6::Regex})/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resolvers = [Hosts.new, DNS.new]) ⇒ Resolv

Creates a new Resolv using resolvers



61
62
63
# File 'lib/dnsruby/resolv.rb', line 61

def initialize(resolvers=[Hosts.new, DNS.new])
  @resolvers = resolvers
end

Class Method Details

.each_address(name, &block) ⇒ Object

Iterates over all IP addresses for name



38
39
40
# File 'lib/dnsruby/resolv.rb', line 38

def self.each_address(name, &block)
  instance.each_address(name, &block)
end

.each_name(address, &proc) ⇒ Object

Iterates over all hostnames of address



53
54
55
# File 'lib/dnsruby/resolv.rb', line 53

def self.each_name(address, &proc)
  instance.each_name(address, &proc)
end

.getaddress(name) ⇒ Object

Looks up the first IP address for name



28
29
30
# File 'lib/dnsruby/resolv.rb', line 28

def self.getaddress(name)
  instance.getaddress(name)
end

.getaddresses(name) ⇒ Object

Looks up all IP addresses for name



33
34
35
# File 'lib/dnsruby/resolv.rb', line 33

def self.getaddresses(name)
  instance.getaddresses(name)
end

.getname(address) ⇒ Object

Looks up the first hostname of address



43
44
45
# File 'lib/dnsruby/resolv.rb', line 43

def self.getname(address)
  instance.getname(address)
end

.getnames(address) ⇒ Object

Looks up all hostnames of address



48
49
50
# File 'lib/dnsruby/resolv.rb', line 48

def self.getnames(address)
  instance.getnames(address)
end

.instanceObject

Some class methods require the use of an instance to compute their result. For this purpose we create a single instance that can be reused.



20
21
22
# File 'lib/dnsruby/resolv.rb', line 20

def self.instance
  @instance ||= self.new
end

Instance Method Details

#each_address(name) ⇒ Object

Iterates over all IP addresses for name



87
88
89
# File 'lib/dnsruby/resolv.rb', line 87

def each_address(name)
  getaddresses(name).each { |address| yield(address)}
end

#each_name(address) ⇒ Object

Iterates over all hostnames of address



112
113
114
# File 'lib/dnsruby/resolv.rb', line 112

def each_name(address)
  getnames(address).each { |address| yield(address) }
end

#getaddress(name) ⇒ Object

Looks up the first IP address for name



66
67
68
69
70
71
72
73
# File 'lib/dnsruby/resolv.rb', line 66

def getaddress(name)
  addresses = getaddresses(name)
  if addresses.empty?
    raise ResolvError.new("no address for #{name}")
  else
    addresses.first
  end
end

#getaddresses(name) ⇒ Object

Looks up all IP addresses for name



76
77
78
79
80
81
82
83
84
# File 'lib/dnsruby/resolv.rb', line 76

def getaddresses(name)
  return [name] if ADDRESS_REGEX.match(name)
  @resolvers.each do |resolver|
    addresses = []
    resolver.each_address(name) { |address| addresses << address }
    return addresses unless addresses.empty?
  end
  []
end

#getname(address) ⇒ Object

Looks up the first hostname of address



92
93
94
95
96
97
98
99
# File 'lib/dnsruby/resolv.rb', line 92

def getname(address)
  names = getnames(address)
  if names.empty?
    raise ResolvError.new("no name for #{address}")
  else
    names.first
  end
end

#getnames(address) ⇒ Object

Looks up all hostnames of address



102
103
104
105
106
107
108
109
# File 'lib/dnsruby/resolv.rb', line 102

def getnames(address)
  @resolvers.each do |resolver|
    names = []
    resolver.each_name(address) { |name| names << name }
    return names unless names.empty?
  end
  []
end