I have been playing around with different shortener APIs for Google Analytics URL shortener. HTTParty makes this all easy and gives warm fuzzy feeling. Bit.ly API requires free registration to get an API key. Other than that it is pretty straight forward to use.
require "rubygems"
require "httparty"
class Bitly
include HTTParty
base_uri "api.bit.ly"
@@login = "username"
@@api_key = "R_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
def self.shorten(url)
get("/v3/shorten?login=#{@@login}&apiKey=#{@@api_key}&longUrl=#{url}")
end
end
puts Bitly.shorten("http://www.appelsiini.net/")["data"]["url"]
Related articles: Using Shrt.st With HTTParty.
Tagged with: Ruby