geemus/excon — pull request #184 by rubiojr (2013-01-24 16:45:08+00:00)
Title: Use ruby's built-in resolver to get the host address

Use the built-in Resolv class to get the host address before
passing it to Socket.getaddrinfo.

One of the things you can do with this is to replace the nameserver
the ruby process is using, and set different one, without having
to tweak your system's resolver settings (i.e. /etc/resolv.conf)
or manually resolving every host before passing it to an Excon request

Imaging we want to use a specific nameserver to resolv non public
domain names, available in our company's private nameserver:

require 'resolv'
require 'excon'

external_dns = '192.168.1.1'
resolver = Resolv::DNS.new(:nameserver => [external_dns], :ndots => 1)
Resolv::DefaultResolver.instance_variable_set :@resolvers,
[resolver]
Excon.get 'https://myserver.mycompany.lan'

Where 'myserver.mycompany.lan' is known only to the DNS server at
192.168.1.1. We could also use resolv-replace.rb from the stdlib
and transparently replace the resolver globally to our process.

We also use this kind of stuff to run tests using different nameservers
transparently, without having to update system's configuration. i.e.:

external_dns = ENV['NAMESERVER']
resolver = Resolv::DNS.new(:nameserver => [external_dns], :ndots => 1)
Resolv::DefaultResolver.instance_variable_set :@resolvers,
[resolver]

Tests.run # test code here

And then using the env variable before running the tests:

$ NAMESERVER=192.168.1.1 rake test