URL shortening services such as tinyurl and snipurl have become popular lately. Sometimes it's necessary to programmatically determine the original URL. Here is a simple ruby method that uses curb (ruby bindings for libcurl) to expand the shortened URLs.


require 'curb'

def expand(url)
  request = Curl::Easy.perform(url) do |curl|
    curl.follow_location = true
  end

  request.last_effective_url
end

>> expand('http://tinyurl.com/2tx')
=> "http://www.google.com"