URL shortening is a technique where a provider makes a web page available under a very short URL in addition to the original address. Usually the services use some kind of hash function, and they are often used in Microblogging services like Twitter. Calling an URL shortener is quite simple in Ruby with the standard OpenURI library . To call qr.cx, Bit.ly or TinyURL , just use one of the following code examples with less than 4 lines of code:

qr.cx

link='http://www.wired.com'
short = open('http://qr.cx/api/?longurl=' + link, "UserAgent" => "Ruby Script").read
=> "http://qr.cx/uQ6"

Bit.ly

link = 'http://www.wired.com'
short_link = open('http://bit.ly/api?url=' + link, "UserAgent" => "Ruby Script").read
=> "http://bit.ly/3pKuoI"

TinyURL

link = 'http://www.wired.com'
short_link = open('http://tinyurl.com/api-create.php?url=' + link, "UserAgent" => "Ruby Script").read
=> "http://tinyurl.com/25k8d"
OpenURI
Advertisements