
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cory Schires &#187; bit.ly</title>
	<atom:link href="http://coryschires.com/tag/bit-ly/feed/" rel="self" type="application/rss+xml" />
	<link>http://coryschires.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 14 Feb 2012 22:45:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to shorten urls with bit.ly in Ruby on Rails.</title>
		<link>http://coryschires.com/how-to-shorten-urls-with-bit-ly-in-ruby-on-rails/</link>
		<comments>http://coryschires.com/how-to-shorten-urls-with-bit-ly-in-ruby-on-rails/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 00:20:44 +0000</pubDate>
		<dc:creator>Cory Schires</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bit.ly]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby-on-Rails]]></category>

		<guid isPermaLink="false">http://coryschires.com/?p=307</guid>
		<description><![CDATA[Create retweet links with shortened urls in ruby on rails. Only requires two simple helper methods which you can cut-n-paste into your application.]]></description>
			<content:encoded><![CDATA[<p>First, let&#8217;s make a simple retweet link for our application. To keep our views a little cleaner, we&#8217;ll make a helper method in <span class="code_text">app/helpers/application_helper.rb</span>:</p>
<div class="code_block">
<pre class="brush: ruby;">
        def twitter_url(resource, tweet_text="Check out this article from my-site.com:")
          tweet_url = root_url.chop + url_for(resource)
          "http://twitter.com/home?status=#{tweet_text} #{tweet_url}"
        end
    </pre>
</div>
<p>This method just builds a url to twitter and supplies some default text which you may optionally override by passing a second argument. Call it in your view like this:</p>
<div class="code_block">
<pre class="brush: ruby;">
        <%= link_to "Retweet this post!", twitter_url(@post) %>
    </pre>
</div>
<p>Now, this method works but it would be much better if we could use shortened urls. So let&#8217;s change things a bit in order to use bit.ly &mdash; twitter&#8217;s preferred url shortening service. Before we write any code&#8230; </p>
<ol>
<li>You&#8217;ll need to <a href="http://bit.ly/account/register?rd=/">sign up for bit.ly</a>.</li>
<li>And <a href="http://bit.ly/account/your_api_key">grap your api key</a>.</li>
</ol>
<p>With that info on hand, we&#8217;re ready to interface with the bit.ly api. Let&#8217;s make another helper method also located in <span class="code_text">app/helpers/application_helper.rb</span>:</p>
<div class="code_block">
<pre class="brush: ruby;">
        def shorten_with_bitly(url)
          # build url to bitly api
          user = "insert-your-username"
          apikey = "insert-your-api-key"
          version = "2.0.1"
          bitly_url = "http://api.bit.ly/shorten?version=#{version}&#038;longUrl=#{url}&#038;login=#{user}&#038;apiKey=#{apikey}"

          # parse result and return shortened url
          buffer = open(bitly_url, "UserAgent" => "Ruby-ExpandLink").read
          result = JSON.parse(buffer)
          short_url = result['results'][url]['shortUrl']
        end
    </pre>
</div>
<p>This method uses your credentials to build a url to interface with the bit.ly api. Bitly receives our request and returns a JSON object containing a shortened url. We&#8217;ll parse this object, grab the shortened url, and return it.</p>
<p>Finally, we need to change our initial <span class="code_text">twitter_url</span> method to include url shortening:</p>
<div class="code_block">
<pre class="brush: ruby;">
        def twitter_url(resource, tweet_text="Check out this article from my-site.com:")
          tweet_url = root_url.chop + url_for(resource)
          tweet_url = shorten_with_bitly(tweet_url) if Rails.env.production?

          "http://twitter.com/home?status=#{tweet_text} #{tweet_url}"
        end
    </pre>
</div>
<p>This is identical to our original method except we&#8217;ve added a new line which calls <span class="code_text">shorten_with_bitly</span>. This line overwrites the <span class="code_text">tweet_url</span> variable &mdash; replacing the long url with a shortened version. Finally, we&#8217;ll include a condition to ensure this only happens in production. This saves us from creating a bunch of useless bit.ly links that point to localhost urls.</p>
<h3>Some final points.</h3>
<ul>
<li>
        The <span class="code_text">shorten_with_bitly</span> method borrows heavily form a ruby snippet posted by <a href="http://puneetitengineer.wordpress.com/2009/07/27/minimise-url-in-ruby/">Puneet Pandey</a>. Pandey definitely deserves some credit here.
    </li>
<li>
        If you&#8217;re looking for a more robust way to interface with the bit.ly api, checkout this <a href="http://nasir.wordpress.com/2009/11/26/url-shortener-gem-bitly-api-wrapper-in-ruby/">url shortening gem</a> by Nasir Jamal. I&#8217;ve never used it but appears to be fully featured and well documented.
    </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://coryschires.com/how-to-shorten-urls-with-bit-ly-in-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

