Dynamic in-memory 'hosts' file for resolving hostnames. Injects entries into an in-memory 'hosts' file which can later be used for name resolution without having to modify the system hosts file. This is an extension to the standard ruby Resolv library and is useful for over-riding name resolution during testing.
Add this line to your application's Gemfile:
gem 'resolv-hosts-dynamic'
And then execute:
$ bundle
Or install it yourself as:
$ gem install resolv-hosts-dynamic
Entries can be added during class instantiation or at any point afterwards
(via the add_address
method) if we've still got a handle on the object. An
entry consists of a hash with the addr
and hostname
properties, plus an
optional aliases
property. Entries added during class instantiation can be
in the form of a single entry or an array of entries. add_address
only
accepts a single entry at a time.
require 'resolv-hosts-dynamic'
res = Resolv::Hosts::Dynamic.new([
{
'addr' => '127.1.2.3',
'hostname' => 'host.example.com',
},
{
'addr' => '127.4.5.6',
'hostname' => 'host2.example.com',
'aliases' => 'host2',
},
])
res.add_address({
'addr' => '127.7.8.9',
'hostname' => 'host3.example.com',
'aliases' => [ 'host3', 'www.example.com' ],
})
This class can be used on its own or within the standard ruby Resolv library.
On its own:
require 'resolv-hosts-dynamic'
res = Resolv::Hosts::Dynamic.new({
'addr' => '127.1.2.3',
'hostname' => 'host.example.com',
})
res.getaddress('host.example.com')
res.getname('127.1.2.3')
Within the standard ruby Resolv library:
require 'resolv-hosts-dynamic'
res = Resolv.new([
Resolv::Hosts::Dynamic.new({
'addr' => '127.1.2.3',
'hostname' => 'host.example.com',
}),
Resolv::Hosts.new,
Resolv::DNS.new,
])
res.getaddress('host.example.com')
res.getname('127.1.2.3')
res.getaddresses('rubygems.org')
Replacing the default resolver in the standard ruby Resolv library (using
resolve-replace
):
require 'resolv-hosts-dynamic'
require 'resolv-replace'
dynres = Resolv::Hosts::Dynamic.new({
'addr' => '127.1.2.3',
'hostname' => 'host.example.com',
})
Resolv::DefaultResolver.replace_resolvers([
dynres,
Resolv::Hosts.new,
Resolv::DNS.new,
])
Resolv.getaddress('host.example.com')
Resolv.getname('127.1.2.3')
Resolv.getaddresses('www.google.com')
dynres.add_address({
'addr' => '127.4.5.6',
'hostname' => 'www.google.com',
})
Resolv.getaddresses('www.google.com')
- Fork it ( https://github.com/chris-reeves/gem-resolv-hosts-dynamic/fork )
- Install pre-commit (
pip install pre-commit; pre-commit install
) - Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request