Automatically Adding Proxy to All Http Connections in Ruby

How to set a proxy in rubys net/http?

Net::HTTP.new('google.de', nil, proxy_addr, proxy_port).start { |http|

This will create an http object for you to use in the block. Use that rather than generating new ones each time, here Net::HTTP.get('google.de', '')

proxy_addr = 'proxy'
proxy_port = 8080

Net::HTTP.new('google.de', nil, proxy_addr, proxy_port).start { |http|
# always proxy via your.proxy.addr:8080
http.get('google.de', '')
}

Set proxy for rubygems

gem will use the HTTP_PROXY environment variable if set. Just set that in Windows 7 and it will be used by default.

Make ruby automacially find the values for the proxy/authentication settings for http?

You should have a variable HTTP_PROXY in your environment containing all you need, for instance :

HTTP_PROXY = http://username:password@proxyserver.domain.com

Then, you should have a method where you can pass this string, or if you don't, some parsing will do the trick.

Alias to sudo gem --proxy PROXY or bash function?

You could make an alias. In ~/.bash_aliases :

alias sudo="sudo "
alias gemproxy="gem install --http-proxy=<PROXY>"

The sudo alias (with a space) is important if you want to use the alias gemproxy with sudo.

EDIT : to intercept gem install, you can add this in your .bash_aliases :

gem() {
if [[ $@ == install* ]]; then
arg=${@#"install "}
command gem install --http-proxy=PROXY $arg
fi
}

But with this, you will always export the proxy even if you're not behind it. You can add a verification to see if the environment variable http_proxy is set.



Related Topics



Leave a reply



Submit