Monday, May 10, 2010

Create short URL by tinyurl service

Google charts usually create a long url which is really meaningless for us to read. and I want to convert by some external services instead of creating a new one, I tried Shorturl but failed due to network service problem or something.

here is one simple way to generate using http://tinyurl.com/ service, easy of use :)


require 'open-uri'
require 'uri'

retrun_URL = 'http://chart.apis.google.com/chart?chxl=0:|load+google+landing+page|click_suggestion|1:|0|1|2|3|4|5&chxt=x,y&chco=76A4FB&chd=s:94&chtt=Detail+Page+Response+Time+for+GoogleSuggest.html&cht=lc&chs=900x300&chxr=1,5.977,5.401'

encoding_url = URI.escape(retrun_URL, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))

tiny_url = open("http://tinyurl.com/api-create.php?url=#{encoding_url}").read

puts tiny_url


Sample OUTPUT:
>>http://tinyurl.com/1c2

Note: If you did not encoding/escape the URL, then the service will call it "bad URI(is not URI?)...URI::InvalidURIError"

No comments:

Post a Comment