Allows you to resolve redirecting urls (i.e. from url-shortening services like bit.ly) with Net::HTTP and find out their destination url as well as the body
MIT License
= RedirectFollower
Allows you to resolve redirecting urls (i.e. from url-shortening services like bit.ly) and find out their destination url as well as the body.
The code is mostly ripped from John Nunemaker's blog post "Following Redirects with Net/HTTP" (http://railstips.org/blog/archives/2009/03/04/following-redirects-with-nethttp/), but wrapped into a Rubygem and with automatic resolving
Install with:
gem install redirect_follower
Then, either shorthand for the destination url only or for the full response:
$ irb
require 'redirect_follower' RedirectFollower('http://is.gd/bNZYZ') # Just find out the destination url => 'http://www.taschen.com/pages/en/catalogue/classics/all/06724/facts.byrne_six_books_of_euclid.htm'
redirect = RedirectFollower.new('http://is.gd/bNZYZ') redirect.url => 'http://www.taschen.com/pages/en/catalogue/classics/all/06724/facts.byrne_six_books_of_euclid.htm' redirect.body => 'a load of html...' redirect.response.body => 'a load of html...' # Same as redirect.body redirect.response => # Full response object from Net::HTTP
Limiting the amount of redirects can be achieved by giving a second parameter to either methods:
RedirectFollower('http://is.gd/bNZYZ', 2) will only follow two redirects before raising RedirectFollower::TooManyRedirects. Same thing for RedirectFollower.new('http://is.gd/bNZYZ', 2). The default amount of redirects is 5.
You can also check out my introductory blog post at http://colszowka.heroku.com/2010/04/30/redirectfollower-rubygem
== Note on Patches/Pull Requests
== Copyright
Copyright (c) 2010 Christoph Olszowka. See LICENSE for details.