Sha256: 7144255d53fa92aec06bf795715103d7b1f2bfdf97f629451808150842b6b097

Contents?: true

Size: 1.56 KB

Versions: 40

Compression:

Stored size: 1.56 KB

Contents

require "ftw/namespace"
require "socket" # for Socket.gethostbyname
require "ftw/singleton"
require "ftw/dns/dns"

# I wrap whatever Ruby provides because it is historically very
# inconsistent in implementation behavior across ruby platforms and versions.
# In the future, this will probably implement the DNS protocol, but for now
# chill in the awkward, but already-written, ruby stdlib.
#
# I didn't really want to write a DNS library, but a consistent API and
# behavior is necessary for my continued sanity :)
class FTW::DNS
  extend FTW::Singleton

  # The ipv4-in-ipv6 address space prefix.
  V4_IN_V6_PREFIX = "0:" * 12

  # An array of resolvers. By default this includes a FTW::DNS::DNS instance.
  attr_reader :resolvers

  private

  # A new resolver.
  #
  # The default set of resolvers is only {FTW::DNS::DNS} which does DNS
  # resolution.
  def initialize
    @resolvers = [FTW::DNS::DNS.new]
  end # def initialize

  # Resolve a hostname.
  #
  # Returns an array of all addresses for this host. Empty array resolution
  # failure.
  def resolve(hostname)
    return @resolvers.reduce([]) do |memo, resolver|
      result = resolver.resolve(hostname)
      memo += result unless result.nil?
    end
  end # def resolve

  # Resolve hostname and choose one of the results at random.
  # 
  # Use this method if you are connecting to a hostname that resolves to
  # multiple addresses.
  def resolve_random(hostname)
    addresses = resolve(hostname)
    return addresses[rand(addresses.size)]
  end # def resolve_random

  public(:resolve, :resolve_random)
end # class FTW::DNS

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
ftw-0.0.49 lib/ftw/dns.rb
ftw-0.0.48 lib/ftw/dns.rb
ftw-0.0.47 lib/ftw/dns.rb
ftw-0.0.46 lib/ftw/dns.rb
ftw-0.0.45 lib/ftw/dns.rb
ftw-0.0.44 lib/ftw/dns.rb
ftw-0.0.43 lib/ftw/dns.rb
ftw-0.0.42 lib/ftw/dns.rb
ftw-0.0.41 lib/ftw/dns.rb
ftw-0.0.40 lib/ftw/dns.rb
ftw-0.0.39 lib/ftw/dns.rb
ftw-0.0.38 lib/ftw/dns.rb
ftw-0.0.37 lib/ftw/dns.rb
ftw-0.0.36 lib/ftw/dns.rb
ftw-0.0.35 lib/ftw/dns.rb
ftw-0.0.34 lib/ftw/dns.rb
ftw-0.0.33 lib/ftw/dns.rb
ftw-0.0.32 lib/ftw/dns.rb
ftw-0.0.31 lib/ftw/dns.rb
ftw-0.0.30 lib/ftw/dns.rb