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
Rails How to Create Data Schema Seed Data
How to Automatically Escape HTML Content Using Jekyll and Markdown
What Is The Purpose of Setting Ruby Block Local Variables When Blocks Have Their Own Scope Already
Many to Many Table with an Extra Column in Rails
Proper Usage of Ruby Statement Modifiers
Inspect or Clean Up The Working Tree Error When Installing Ruby 2.1.3 on MAC Os X 10.9.5
Dbi::Interfaceerror: Could Not Load Driver (Uninitialized Constant MySQL error)
Case Insensitive Search in Rails
Ruby 1.9 How to Convert Array to String Without Brackets
Adding Two Activerecord::Relation Objects
Starting with Redmine Locally - How Easy Is Migration to Server Later
Remove Adjacent Identical Elements in a Ruby Array
Belongs_To Presence in Rails 5 Not Working
How to Unescape C-Style Escape Sequences from Ruby
I Am Getting This Gem Install Error for Kgio Gem When I Do a Bundle Install