How to shorten URL using “bitly.com” in Rails 3.x

ror
Social sharing

rorURL shortening is a technique on the World Wide Web (WWW) in which a Uniform Resource Locator (URL) may be made substantially shorter in length and still direct to the required page.

This is achieved by using an HTTP Redirect on a domain name that is short, which links to the web page that has a long URL. This is especially convenient for messaging technologies such as Twitter and Identical which severely limit the number of characters that may be used in a message.

Many web developers pass descriptive attributes in the URL to represent data hierarchies, command structures, transaction paths or session information. This can result in URLs that are hundreds of characters long and that contain complex character patterns. Such URLs are difficult to memorize and manually reproduce. As a result, long URLs must be copied-and-pasted for reliability. Thus, short URLs are more convenient for websites.

Step# 1

  • To begin with, create an account at bit.ly “
  • Get your API key by the following URL

“http://bit.ly/account/your_api_key/”

Step# 2

  • In rails 3.x

Write the following gems in your gemfile

gem 'bitly'
  • Run “bundle install”

Step# 3

Add the following code in your controller

require 'bitly'

Step# 4

Bitly recently released their version 3 API. From this 0.5.0 release, the gem will continue to work the same but also provide a V3 module, using the version 3 API. The standard module will become deprecated, as Bitly do not plan to keep the version 2 API around f orever.

To move to using the version 3 API, call in you top of the controller:

Bitly.use_api_version_3

Step# 5

To shorten a URL

bitly = Bitly.new('your-bitly-user-id','your-bitly-api-key') page_url = bitly.shorten('your-url') shorten_url = page_url.short_url

It will generate the bitly URL similar to “http://bit.ly/7BWXcQ”

Your recently viewed posts:

Jayadev Das - Post Author

Do what you do best in – that’s what I’ve always believed in and that’s what I preach. Over the past 25+ years (yup that’s my expertise ‘n’ experience in the Information Technology domain), I’ve been consulting to small, medium and large companies ‘About Web Technologies, Mobile Future as well as on the good-and-bad of tech. Blogger, International Business Advisor, Web Technology Expert, Sales Guru, Startup Mentor, Insurance Sales Portal Expert & a Tennis Player. And top of all – a complete family man!

    Contact Us

    We’d love to help & work with you




    When do you want to start ?


    Enter your email address to stay up to date with the latest news.
    Holler Box

    Orange Exit pop up

    Subscribe for the latest
    trends in web and
    mobile app development
    Holler Box

    Exit pop up

    Sad to see you leaving early...

    From "Aha" to "Oh shit" we are sharing everything on our journey.
    Enter your email address to stay up to date with the latest news.
    Holler Box