Using Goo.gl With Ruby and HTTParty

Google URL shortening service now has an API.

I had totally missed that Google recently opened the API of their URL shortening service. I love short and clean APIs. Accessing it using HTTParty is just few lines of code.

require "rubygems"
require "httparty"
require "json"

class Googl
  include HTTParty
  base_uri "https://www.googleapis.com"
  headers  "Content-Type" => "application/json"

  def self.shorten(url)
    post("/urlshortener/v1/url", :body => {:longUrl => url}.to_json)["id"]
  end
end

puts Googl.shorten("http://www.appelsiini.net/")

Related articles: Using Shrt.st With HTTParty, Using Bit.ly With HTTParty.

Posted in Ruby  on 20 Feb 2011.