Shorten URL'S using bit.ly API in Rails
Posted by: Prachi | 22 Oct 2013 | CommentsIn this blog we are going to create shorten URL’s in rails application using Bit.ly API in ruby step by step.
-
For this you required to have bitly login and api_keys.
-
Create your account on
https://bitly.com
. Get your login and api_key for your account.Check settings -> Advanced -> show_legacy_key.
-
Steps for creating shorten urls in Ruby using bit.ly API
- Ruby has a
gem bitly
for bit.ly API.
gem install bitly
- Configurations
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!