Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Object
- def pose_as(klass)
- namespace = klass.to_s.split("::")
- klass = namespace.pop
- if namespace.any?
- eval <<-EOT
- module ::#{namespace.join("::")}
- send(:remove_const, :"#{klass}")
- #{klass} = #{self}
- end
- EOT
- else
- eval <<-EOT
- Object.send(:remove_const, :"#{klass}")
- #{klass} = #{self}
- EOT
- end
- end
- end
- require 'net/http'
- class ProxiedHttp < Net::HTTP
- def self.Proxy(p_addr, p_port = nil, p_user = nil, p_pass = nil)
- if env_proxy = (ENV["HTTP_PROXY"] || ENV["http_proxy"])
- uri = URI.parse(env_proxy)
- p_addr = uri.host
- p_port = uri.port
- if p_user = (ENV["HTTP_PROXY_USER"] || ENV["http_proxy_user"])
- p_pass = (ENV["HTTP_PROXY_PASSWORD"] || ENV["http_proxy_password"])
- end
- end
- super(p_addr, p_port, p_user, p_pass)
- end
- end
- ProxiedHttp.pose_as(Net::HTTP)
- from now on Net::HTTP transparently honors ENV["HTTP_PROXY"]
Add Comment
Please, Sign In to add comment