This gem applies a patch to OpenURI to optionally allow redirections from HTTP to HTTPS, or from HTTPS to HTTP.
UPDATE 2019: HTTP to HTTPS is supported by native open-uri
by default from Ruby 2.4.
This is based on this patch and this gem, and modified to allow redirections in both directions.
Here is the problem it tries to solve:
$ irb
1.9.2p320 :001 > require 'open-uri'
=> true
1.9.2p320 :002 > open('http://github.com')
RuntimeError: redirection forbidden: http://github.com -> https://github.com/
And here is how you can use this patch to follow the redirections:
$ irb
1.9.2p320 :001 > require 'open-uri'
=> true
> require 'open_uri_redirections'
=> true
1.9.2p320 :002 > open('http://github.com', :allow_redirections => :safe)
=> #<File:/var/folders/...>
The patch contained in this gem adds the :allow_redirections option to OpenURI#open
:
:allow_redirections => :safe
will allow HTTP => HTTPS redirections.:allow_redirections => :all
will allow HTTP => HTTPS redirections and HTTPS => HTTP redirections.
Before using this gem, read this:
https://gist.github.com/1271420
https://bugs.ruby-lang.org/issues/3719
https://github.com/ruby/ruby/blob/trunk/lib/open-uri.rb
https://github.com/obfusk/open_uri_w_redirect_to_https
Use it at your own risk!
Add this line to your application's Gemfile:
gem 'open_uri_redirections'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install open_uri_redirections
- Fork it
- 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 new Pull Request