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 include an URL to example page where the problem occurs. Even better is to make a Fiddle which demonstrates the problem. If you have longer code examples please use pastie.org.
CATEGORIES
Built using the awesome Flat UI Pro framework by Designmodo.

Mika Tuupola is a Member of the Leanpub Affiliate Program.

© 2014 Mika Tuupola.