Bit.ly is a popular URL shortening service that is used to shorten urls like http://richonrails.com/articles/shortening-urls-with-bit-ly to a url like http://bit.ly/1d0fCpQ. This means the URL takes up less space and is friendlier for platforms like Twitter, which have post length restrictions.

Bit.ly Account Setup

The first thing you will neeed to do is setup up an account with Bit.ly. Simply visit https://bitly.com/a/sign_up to sign up for an account.

Once signed up, you will need to verify your email address. Click the drop down in the upper right corner and choose settings, alternatively you can simply click here to go directly to the page. On this screen you will see your email address, along with a prompt to verify it. Click the link and then go to your email and click the link bit.ly sends you to verify your email address.

Verify Email Screenshot

Finally, you need to visit this page to get your login name and API key. Note both of these values as you will need them for your rails application.

Rails Application Setup

The first thing we need to do is add the bitly to our gemfile. Open up your gemfile and add in the code listed below.

Gemfile:

gem 'bitly', '~> 0.9.0'

In this example, we will create a controller called Urls, which will allow us to create a bitly link based on parameters that are passed in. Run the command below to create the Urls controller now.

Terminal Commands:

rails g controller urls new create

The next thing we need to do is add a route and set up a homepage in our routes file. Open up your routes file and modify it so that it looks like the code listed below. Be sure not to overwrite your applications name on the first line.

config/routes.rb:

BitlyExample::Application.routes.draw do
  resources :urls, only [:new, :create]
  root to: 'urls#new'
end

Next we need to create an initializer. Create an file called bitly.rb in your initializers folder and add in the code listed below. Make sure you replace USERNAME with your Bit.ly username and API_KEY with your Bit.ly API key you noted earlier.

config/routes.rb:

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

The next thing we need to do is add some code to our Urls controller. Open up your Urls controller and modify it so that it looks like code listed below.

app/controllers/urls_controller.rb:

class UrlsController < ApplicationController
  def new
  end

  def create
    if !params[:url].blank?
      client = Bitly.client
      @url = client.shorten(params[:url])
    end
  end
end

The magic happens in the create method. The create method takes a parameter called url and shortens it using the bitly client gem.

Great! Now we need to add in some code for our Urls new view. Open up the view now and add in the code listed below.

app/views/urls/new.html.erb:

<h1>Create a new Bit.ly URL:</h1>

<%= form_for "", url: urls_path, method: :post do |f| %>
 <%= f.label :url %>
 <%= f.text_field :url %>
 <%= f.submit "Create Url" %>
<% end %>

Finally, add this code to your Urls create view.

app/views/urls/create.html.erb:

<h1>Your url for <%= @url.long_url %> is: </h1>
<%= link_to @url.short_url, @url.short_url, target: "_blank" %><br /><br />
<%= link_to "Create another Url", root_path %>

The @url.short_url attribute returns the shortened version of the URL, while @url.long_url returns the original url. If you start your rails server and navigate to http://localhost:3000 you will see that you can generate Bit.ly urls on the fly right from your own website. That's it! Thanks for reading!