public
Description: Plugin to "hijack" Ruby host(s) resolver aka(resolv-replace.rb), allowing programmatic host file entries.
Homepage: http://dewey.ws
Clone URL: git://github.com/retr0h/railsolver.git
railsolver / lib / railsolver.rb
100644 24 lines (20 sloc) 0.739 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
require 'socket'
require 'resolv'
require 'yaml'
 
class IPSocket
  class << self
    c = YAML::load(ERB.new(IO.read("#{RAILS_ROOT}/config/hosts.yml")).result) rescue false
    CONFIG = (c.is_a? FalseClass) ? {} : ((c.has_key? RAILS_ENV) ? c[RAILS_ENV] : c)
 
    def getaddress_with_yaml(host)
      CONFIG[host] ? CONFIG[host].to_s : (CONFIG[CONFIG.keys.map{ |k| host.match(/#{k}/) }.first.to_s] || getaddress_without_yaml(host))
    end
    alias_method_chain :getaddress, :yaml
  end
end
 
class TCPSocket
  alias original_resolv_initialize initialize
  def initialize(host, serv, *rest)
    rest[0] = IPSocket.getaddress(rest[0]) unless rest.empty?
    original_resolv_initialize(IPSocket.getaddress(host), serv, *rest)
  end
end