Using Goo.gl With Ruby and HTTParty

Blog Projects
ruby

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.



When asking a question please include an URL to example page where the problem occurs. If you have longer code examples please use pastie.org.
CATEGORIES
Built using the awesome Flat UI Pro framework by Designmodo.

© 2013 Mika Tuupola.