Convert Shorten URL (bit.ly, tinyurl, ow.ly, and many more) to Full URL in Ruby

Posted: October 13th, 2009 | Author: | Filed under: IT Related | Tags: , , , , | 4 Comments »

You might worry to visit directly to a shorten URL because who knows it may contain some malicious script/code

I’ve found a solution “Python: Convert those TinyURL (bit.ly, tinyurl, ow.ly) to full URLS” in stackoverflow.com but the code is in Python.

Here is how you can perform the conversion in Ruby

#!/usr/bin/ruby
 
require 'net/http'
 
def ConvertToFull(tinyurl)
   url = URI.parse(tinyurl)
   host, port = url.host, url.port if url.host && url.port
   req = Net::HTTP::Get.new(url.path)
   res = Net::HTTP.start(host, port) {|http|  http.request(req) }
   return res.header['location']
end
 
puts ConvertToFull('http://bit.ly/rgCbf') #here is how you can call the function. Thank you Captain Obvious!

**UPDATED on 19/10/2009**

I’ve work on a more complete version which can determine Shorten URL or Full URL and return the full URL for the shorten URL.. email for for the code 😉


4 Comments on “Convert Shorten URL (bit.ly, tinyurl, ow.ly, and many more) to Full URL in Ruby”

  1. 1 farah said at 8:33 AM on October 16th, 2009:

    kenapa mesti ruby???

  2. 2 xanda said at 9:33 AM on October 16th, 2009:

    i’m working on a ruby project now 😉

  3. 3 rolex replica said at 3:42 PM on April 16th, 2010:

    good post

  4. 4 rolex said at 2:58 PM on May 12th, 2010:

    thank you


Leave a Reply