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” (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 colszowka.heroku.com/2010/04/30/redirectfollower-rubygem

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 Christoph Olszowka. See LICENSE for details.