Coding Tidbits

Cube Root is an exclusive Ruby on Rails Consultancy. Find our programming ramblings here.

Shorten URL'S using bit.ly API in Rails

In this blog we are going to create shorten URL's in rails application using Bit.ly API in ruby step by step.

  1. For this you required to have bitly login and api_keys.

  2. Create your account on https://bitly.com . Get your login and api_key for your account.

    Check settings -> Advanced -> showlegacykey.

  3. Steps for creating shorten urls in Ruby using bit.ly API

gem install bitly

If you want to configure bitly through an initializer. Add code to config/initializers/bitly.rb

Bitly.configure do |config|
  config.api_version = 3
  config.login = "Bitly_Username"
  config.api_key = "API_KEY"
end

To get the client, use Bitly.client:

client = Bitly.client

You can then use that client to shorten or expand urls :

 url = client.shorten('http://cuberoot.in/')

 url.long_url
 #=> "http://cuberoot.in/"

 url.short_url
 #=> "http://bit.ly/1eTMCoC"

And it is done!