Class: Srvy::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/srvy/resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Resolver

Returns a new instance of Resolver.



8
9
10
11
12
13
14
15
16
17
# File 'lib/srvy/resolver.rb', line 8

def initialize(options={})
  @dns       = Net::DNS::Resolver.new

  if options[:nameserver]
    @dns.nameservers = options[:nameserver] 
  end

  cache_size = options[:cache_size] || 100
  @cache     = LruRedux::Cache.new(cache_size) #cache of host -> result kv pairs
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



6
7
8
# File 'lib/srvy/resolver.rb', line 6

def cache
  @cache
end

Instance Method Details

#get(host) ⇒ Object

Gets the Srvy::Result from the cache or from dns.

host - The String hostname to query for SRV records.

Returns the Srvy::Result for that hostname



49
50
51
52
53
54
55
56
57
58
# File 'lib/srvy/resolver.rb', line 49

def get(host)
  result = @cache[host]

  if !result || result.expired?
    result = Srvy::Result.from_dns get_dns(host)
    @cache[host] = result
  end
 
  result
end

#get_all(host, format = :host_port) ⇒ Object



34
35
36
37
# File 'lib/srvy/resolver.rb', line 34

def get_all(host, format=:host_port)
  result = get(host).get_all
  Srvy::Formatters.format_many(format, result)
end

#get_dns(host) ⇒ Object



39
40
41
# File 'lib/srvy/resolver.rb', line 39

def get_dns(host)
  @dns.search(host, Net::DNS::SRV)
end

#get_many(host, format = :host_port) ⇒ Object



29
30
31
32
# File 'lib/srvy/resolver.rb', line 29

def get_many(host, format=:host_port)
  result = get(host).get_many
  Srvy::Formatters.format_many(format, result)
end

#get_single(host, format = :host_port) ⇒ Object



23
24
25
26
27
# File 'lib/srvy/resolver.rb', line 23

def get_single(host, format=:host_port)
  result = get(host).get_single

  Srvy::Formatters.format_single(format, result)
end

#inspectObject



19
20
21
# File 'lib/srvy/resolver.rb', line 19

def inspect
  "#<Srvy::Resolver:#{object_id} dns=#{@dns.nameservers.inspect}>"
end