Expand tinyurls using ruby

Just a tiny thing but handy and I couldn’t find it anywhere else (I’m new to Ruby and I’m coding by google so don’t expect great style here, but this seems to work):

require 'net/http'
require 'uri'

url = URI.parse "http://bit.ly/1Zw502"
if url.path.size > 0
  # catches case where you get an url like
  # http://planetrdf.com with no slash
  # this catches this but doesn't look it up
  req = Net::HTTP::Get.new(url.path)
  begin
  res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
  case res
  when Net::HTTPRedirection
   uu = res['Location']
   puts uu
  end
 end
end

uu is the expanded url. Is there a better way than this? Is it me or is Ruby documentation a bit thin on the ground? (Thanks to Damian for pointing out to me how tinyurls work – I’d never bothered to look before!)


2 Responses to Expand tinyurls using ruby

  1. Thanks! This was really helpful to me :)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>