<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Tom Clements]]></title>
  <link href="http://tom-clements.com/atom.xml" rel="self"/>
  <link href="http://tom-clements.com/"/>
  <updated>2012-05-14T20:08:57+01:00</updated>
  <id>http://tom-clements.com/</id>
  <author>
    <name><![CDATA[Tom Clements]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Capybara on the Command Line - Live Browser Testing From IRB]]></title>
    <link href="http://tom-clements.com/blog/2012/02/25/capybara-on-the-command-line-live-browser-testing-from-irb/"/>
    <updated>2012-02-25T18:42:00+00:00</updated>
    <id>http://tom-clements.com/blog/2012/02/25/capybara-on-the-command-line-live-browser-testing-from-irb</id>
    <content type="html"><![CDATA[<p>Everyone knows retro fitting tests is not the best way to go, but you can&#8217;t escape the fact that sometimes it&#8217;s unavoidable.  Writing integration tests after the fact can sometimes be tricky, especially when dealing with complex xpaths or trying to assert against deeply buried elements.  So if you&#8217;re as tired of digging around in web inspector as I am, try this instead:</p>

<pre><code>gem install capybara
</code></pre>

<p>And then fire up console:</p>

<pre><code>require 'capybara/dsl'
=&gt; true 
include Capybara::DSL
=&gt; Object 
Capybara.default_driver = :selenium
=&gt; :selenium 
visit "http://google.co.uk"
=&gt; "" 
</code></pre>

<p><a href="http://tom-clements.com/images/capybara.png"><img src="http://tom-clements.com/images/capybara.png"/></a></p>


<p>Watch and smile as Firefox opens, and then access anything on it with <code>page</code></p>

<pre><code>page.fill_in('q', :with =&gt; 'capybara')
=&gt; ""
page.click_link "jnicklas/capybara - GitHub"
=&gt; "ok"
</code></pre>

<p>And by getting instant feedback in this way, those steps will write themselves.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Get Scamp for Campfire Running on Heroku]]></title>
    <link href="http://tom-clements.com/blog/2012/01/16/get-scamp-for-campfire-running-on-heroku/"/>
    <updated>2012-01-16T19:19:00+00:00</updated>
    <id>http://tom-clements.com/blog/2012/01/16/get-scamp-for-campfire-running-on-heroku</id>
    <content type="html"><![CDATA[<p><a href="https://github.com/wjessop/Scamp">Scamp</a> is a great bot framework for <a href="http://campfirenow.com/">Campfire</a>, and here&#8217;s how to get a
bot up and running in no time on <a href="http://www.heroku.com/">Heroku</a>.</p>

<p>First up, create a new directory for the project and make sure
<code>bundler</code> and <code>heroku</code> gems are installed.</p>

<pre><code>mkdir scampbot;cd scampbot
gem install bundler heroku
</code></pre>

<p>Then create a Gemfile</p>

<figure class='code'><figcaption><span>Gemfile </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>source :rubygems
</span><span class='line'>gem 'rack'
</span><span class='line'>gem 'scamp'</span></code></pre></td></tr></table></div></figure>


<p>bundle, set up a git repo, and then create a new Heroku app</p>

<pre><code>bundle
git init
git commit -a -m "boom"
heroku create
</code></pre>

<p>As Heroku is happy running Rack based apps, all we need to do here is include a rackup file named config.ru in the root directory.</p>

<figure class='code'><figcaption><span>config.ru </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1">#!/usr/bin/env ruby</span>
</span><span class='line'>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;bundler/setup&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;rack&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;scamp&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># respond to something</span>
</span><span class='line'><span class="n">run</span> <span class="nb">lambda</span> <span class="p">{</span> <span class="o">|</span><span class="n">env</span><span class="o">|</span> <span class="o">[</span><span class="mi">200</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;Content-Type&#39;</span><span class="o">=&gt;</span><span class="s1">&#39;text/plain&#39;</span><span class="p">},</span> <span class="no">StringIO</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s2">&quot;It&#39;s Alive!</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span><span class="o">]</span> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This tiny Rack app will just respond to any request with &#8220;It&#8217;s Alive!&#8221; - handy for stopping Heroku apps sleeping on the job. Push it up to Heroku and try it out.</p>

<pre><code>    git add .
    git commit -m "Adding config.ru"
    git push heroku master
    heroku open
</code></pre>

<p>Next grab your Campfire API key - rather than checking this into the repo create some <a href="http://devcenter.heroku.com/articles/config-vars">config vars</a> instead</p>

<pre><code>heroku config:add CAMPFIRE_KEY=your_api_key SUBDOMAIN=your_subdomain
</code></pre>

<p>With those variables in place start on Scamp</p>

<figure class='code'><figcaption><span>config.ru </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># do the robot</span>
</span><span class='line'><span class="n">scamp</span> <span class="o">=</span> <span class="no">Scamp</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">:api_key</span> <span class="o">=&gt;</span> <span class="no">ENV</span><span class="o">[</span><span class="s1">&#39;CAMPFIRE_KEY&#39;</span><span class="o">]</span><span class="p">,</span>
</span><span class='line'>                  <span class="ss">:subdomain</span> <span class="o">=&gt;</span> <span class="no">ENV</span><span class="o">[</span><span class="s1">&#39;SUBDOMAIN&#39;</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># add some rooms, can be ID numbers or room names</span>
</span><span class='line'><span class="n">rooms</span> <span class="o">=</span> <span class="o">[</span><span class="s1">&#39;My awesome room&#39;</span><span class="p">,</span> <span class="mi">1234</span> <span class="o">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now the basics are in place all that&#8217;s left to do is define some simple behaviour and connect to the rooms</p>

<figure class='code'><figcaption><span>config.ru </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">scamp</span><span class="o">.</span><span class="n">behaviour</span> <span class="k">do</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">match</span><span class="sr"> /^blame (?&lt;scapegoat&gt;.+)$/</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">say</span> <span class="s2">&quot;Damnit </span><span class="si">#{</span><span class="n">scapegoat</span><span class="si">}</span><span class="s2">!&quot;</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">scamp</span><span class="o">.</span><span class="n">connect!</span><span class="p">(</span><span class="n">rooms</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>And that&#8217;s it, push it up to Heroku!</p>

<pre><code>git commit -m "adding scamp"
git push heroku master
</code></pre>

<p>Or for the super quick version, clone my <a href="git@github.com:seenmyfate/scamp-bot-heroku.git">example app</a>.</p>

<p>When you&#8217;ve got the basic setup in place I highly recommend checking out the <a href="https://github.com/wjessop/Scamp/blob/master/README.md">Scamp Readme</a> to learn about some of Scamp&#8217;s other moves.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Contributing to Open Source Projects With give]]></title>
    <link href="http://tom-clements.com/blog/2012/01/01/contributing-to-open-source-projects-with-give/"/>
    <updated>2012-01-01T10:43:00+00:00</updated>
    <id>http://tom-clements.com/blog/2012/01/01/contributing-to-open-source-projects-with-give</id>
    <content type="html"><![CDATA[<p>Working as part of a large team on a sizeable number of applications, it&#8217;s fair to say that I spend quite a lot of time thinking about git.  After spotting a recent <a href="http://railscasts.com/episodes/300-contributing-to-open-source">Railscast</a> on open source contributions, it occurred to me that the process involved in maintaining an open source fork isn&#8217;t neccessarily obvious.  As a result, I&#8217;ve released <a href="https://github.com/seenmyfate/give">give</a>, a gem which wraps up some simple git workflow alongside the GitHub API, with the aim of making it just a little bit easier to get started contributing to open source projects.</p>

<h3>The setup</h3>

<pre><code>gem install give
</code></pre>

<p><code>give</code> only requires that a GitHub user and token are set, you can check this by looking in <code>~/.gitconfig</code> or from the command line</p>

<pre><code>git config --get github.user
git config --get github.token
</code></pre>

<p>If either are missing, <a href="http://help.github.com/set-your-user-name-email-and-github-token/">this GitHub article</a> will walk you throug the process.</p>

<p>With <code>give</code> installed you can now fork a GitHub repository from the command line using the <code>give setup USERNAME REPO</code> command.  This example will fork the <code>give</code> project from the user <code>seenmyfate</code></p>

<pre><code>give setup seenmyfate give
</code></pre>

<p>This command uses the GitHub API to fork the project you have specified to your GitHub account, and then clones your fork to your current path.  An extra remote &#8216;upstream&#8217; is also added to the project, pointing back to original project.</p>

<h3>Staying current</h3>

<p>Once you&#8217;re ready to start coding, the <code>give start BRANCH</code> command will create and checkout a new branch to work in</p>

<pre><code>give start feature/my_awesome_feature
</code></pre>

<p>To be sure you&#8217;re working on the latest code, the <code>give update BRANCH</code> command will rebase your fork&#8217;s master branch from the original repository, and then rebase your feature branch.</p>

<pre><code>give update feature/my_awesome_feature
</code></pre>

<p>By doing this regularly you can be sure that you are not duplicating work, and also ensure that any code you depend on has not changed since you started work.  This should also help your pull request to apply cleanly, allowing the project maintainer to easily pull in your contribution.</p>

<h3>Pull</h3>

<p>When your contribution is complete, push your changes to GitHub  and send a pull request with <code>give finish</code>.  Here you must pass in the user and repository to send the pull request, and your local branch.</p>

<pre><code>  give finish seenmyfate give feature/my_awesome_feature
</code></pre>

<p>You will be prompted to enter a title and body for the pull request.</p>

<p>And that&#8217;s it. I hope this gem will encourage more people to make their first open source contribution, and maybe make things a little easier for those who don&#8217;t spend all day merging and rebasing with git.  I&#8217;d love to hear from anyone having success with <code>give</code>, and of course if you have any ideas for improvements, please <a href="https://github.com/seenmyfate/give">fork away!</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Vim - My .vimrc highlights]]></title>
    <link href="http://tom-clements.com/blog/2011/12/29/vim-my-vimrc-highlights/"/>
    <updated>2011-12-29T17:18:00+00:00</updated>
    <id>http://tom-clements.com/blog/2011/12/29/vim-my-vimrc-highlights</id>
    <content type="html"><![CDATA[<p>Moving from TextMate to Vim is probably the most significant change I&#8217;ve made to my daily development over the last year - and I&#8217;m amazed by how much difference it&#8217;s made.  A few weeks back though <a href="http://mislav.uniqpath.com/2011/12/vim-revisited/">this blog post</a> convinced me to go one step further - I took a deep breath and deleted my entire vim install.</p>

<h3>Starting over</h3>

<p>Following Mislav&#8217;s advice, I started out with <a href="https://gist.github.com/1535974">13 lines of simple configuration</a>, initially adding only <a href="https://github.com/tpope/vim-pathogen">pathogen</a>, the <a href="https://github.com/tpope/vim-rails">vim-rails plugin</a> and the truly awesome <a href="http://ethanschoonover.com/solarized">solarized theme</a>.  With those staples in place I left my .vimrc file open, made friends with <code>:source $MYVIMRC</code> and resolved to only add a mapping or plugin if my workflow depended on it.  Two weeks later I can safely say this is the best thing I&#8217;ve done since switching to vim, so here are the highlights from my <a href="https://github.com/seenmyfate/vim">configuration</a>.</p>

<p><a href="http://tom-clements.com/images/vim.png"><img src="http://tom-clements.com/images/vim.png"/></a></p>


<h3>Fuzz Face</h3>

<p><a href="https://github.com/wincent/Command-T">Command-T</a> is often suggested as the drop-in vim replacement for TextMate&#8217;s popular fuzzy finder functionality, but I heartily recommend the far superior <a href="http://kien.github.com/ctrlp.vim/">ctrlp</a>.   The speed alone is worth the switch, but once you add in regexp mode, the ability to jump directly to a line number, and multiple file opening, Command-T is firmly outclassed.</p>

<h3>Force yourself</h3>

<p>Between gvim and arrow keys I&#8217;ve been cheating for a while, nothing has forced me into dropping some bad habits than locking the arrow keys.</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='vim'><span class='line'>  map <span class="p">&lt;</span>Left<span class="p">&gt;</span> :echo <span class="s1">&#39;damnit!&#39;</span><span class="p">&lt;</span><span class="k">cr</span><span class="p">&gt;</span>
</span><span class='line'>  map <span class="p">&lt;</span>Right<span class="p">&gt;</span> :echo <span class="s1">&#39;you suck!&#39;</span><span class="p">&lt;</span><span class="k">cr</span><span class="p">&gt;</span>
</span><span class='line'>  map <span class="p">&lt;</span>Up<span class="p">&gt;</span> :echo <span class="s1">&#39;this is why you fail&#39;</span><span class="p">&lt;</span><span class="k">cr</span><span class="p">&gt;</span>
</span><span class='line'>  map <span class="p">&lt;</span>Down<span class="p">&gt;</span> :echo <span class="s1">&#39;nooooo!&#39;</span><span class="p">&lt;</span><span class="k">cr</span><span class="p">&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<h3>Split panes and tabs</h3>

<p>I love split panes, this feature was the main reason I left TextMate in the first place.  Opening a split pane and jumping into it is an essential shortcut, quite often followed up with vim-rails&#8217; <code>:A</code></p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='vim'><span class='line'>  map :<span class="k">vs</span> :<span class="k">vsplit</span><span class="p">&lt;</span><span class="k">cr</span><span class="p">&gt;&lt;</span><span class="k">c</span><span class="p">-</span><span class="k">w</span><span class="p">&gt;</span><span class="k">l</span>
</span></code></pre></td></tr></table></div></figure>


<p>And some shortcuts to resize the split.</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='vim'><span class='line'><span class="c">  &quot; Resize windows quickly</span>
</span><span class='line'><span class="c">  &quot; reset with &lt;c-w&gt;=</span>
</span><span class='line'>  nmap <span class="p">&lt;</span><span class="k">c</span><span class="p">-</span><span class="k">w</span><span class="p">&gt;</span><span class="k">l</span> :<span class="k">vertical</span> <span class="k">res</span> <span class="p">+</span><span class="m">20</span><span class="p">&lt;</span><span class="k">cr</span><span class="p">&gt;</span>
</span><span class='line'>  nmap <span class="p">&lt;</span><span class="k">c</span><span class="p">-</span><span class="k">w</span><span class="p">&gt;</span><span class="k">h</span> :<span class="k">vertical</span> <span class="k">res</span> <span class="m">-20</span><span class="p">&lt;</span><span class="k">cr</span><span class="p">&gt;</span>
</span><span class='line'>  nmap <span class="p">&lt;</span><span class="k">c</span><span class="p">-</span><span class="k">w</span><span class="p">&gt;</span><span class="k">j</span> :<span class="k">res</span> <span class="p">+</span><span class="m">20</span><span class="p">&lt;</span><span class="k">cr</span><span class="p">&gt;</span>
</span><span class='line'>  nmap <span class="p">&lt;</span><span class="k">c</span><span class="p">-</span><span class="k">w</span><span class="p">&gt;</span><span class="k">k</span> :<span class="k">res</span> <span class="m">-20</span><span class="p">&lt;</span><span class="k">cr</span><span class="p">&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Whilst I tend not to use tabs as often as I use buffers, I still enjoying being able to flip between tabs quickly when I do.</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='vim'><span class='line'>  nmap <span class="p">&lt;</span>leader<span class="p">&gt;</span>[ :<span class="k">tabprevious</span><span class="p">&lt;</span><span class="k">cr</span><span class="p">&gt;</span>
</span><span class='line'>  nmap <span class="p">&lt;</span>leader<span class="p">&gt;</span>] :<span class="k">tabNext</span><span class="p">&lt;</span><span class="k">cr</span><span class="p">&gt;</span>
</span><span class='line'>  nmap T :<span class="k">tabnew</span><span class="p">&lt;</span><span class="k">cr</span><span class="p">&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<h3>Scratch buffer</h3>

<p>With the help of the <a href="https://github.com/duff/vim-scratch">scratch plugin</a> this gives me a quick split for random notes, something I use all the time.</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='vim'><span class='line'>  nmap <span class="p">&lt;</span>leader<span class="p">&gt;</span>; :Sscratch<span class="p">&lt;</span><span class="k">cr</span><span class="p">&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<h3>And more</h3>

<p>In addition to these mappings I&#8217;ve borrowed Gary Bernhardt&#8217;s <a href="https://github.com/garybernhardt/dotfiles">testing functions</a> and really cool shortcuts to scroll the other split window.</p>

<p>Feel free to take <a href="https://github.com/seenmyfate/vim">my vim</a> for a spin, the vimrc is pretty well documented.</p>

<pre><code>  git clone git://github.com/seenmyfate/vim.git ~/.vim
  cd ~/.vim &amp;&amp; rake
</code></pre>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA['Fuck Spotify' - a Rant in D Minor]]></title>
    <link href="http://tom-clements.com/blog/2011/11/24/fuck-spotify-a-rant-in-d-minor/"/>
    <updated>2011-11-24T21:35:00+00:00</updated>
    <id>http://tom-clements.com/blog/2011/11/24/fuck-spotify-a-rant-in-d-minor</id>
    <content type="html"><![CDATA[<p><em>Over the last few months I&#8217;ve noticed a growing murmuring from musicians about Spotify and royalty payments, culminating in this <a href="http://www.musicweek.com/story.asp?storycode=1047525">Music Week</a> article that inspred this rant.</em></p>

<blockquote><p>&#8220;Got paid £8 for 90,000 plays. Fuck spotify.&#8221;</p><footer><strong>@Jon_Hopkins_</strong><cite><a href='http://twitter.com/#!/Jon_Hopkins_/status/137147753829646336'>twitter.com/#!/Jon_Hopkins_/&hellip;</a></cite></footer></blockquote>


<p>Fuck Spotify. I mean really? Do you cry when thousands of people downloaded a free track on your website? Are you devastated because your SoundCloud profile has topped 100,000 plays? Are you enraged with the injustice of Radio 1 playing your music to millions of listeners for free and only giving you a measly £60 in return?  Such outrage. 90,000 potential new fans? Fuck Spotify.</p>

<p>And it&#8217;s not just Mercury Prize nominated artists rushing to alienate their fans, some labels I used to respect are getting in on it as well:</p>

<blockquote><p>&#8220;Spotify will kill smaller bands that are already struggling to make ends meet&#8221;</p><footer><strong>Century Media</strong><cite><a href='http://vator.tv/news/2011-10-26-is-spotify-putting-indie-musicians-out-of-business'>vator.tv/news/&hellip;</a></cite></footer></blockquote>


<p>So said <a href="http://www.centurymedia.com">Century Media</a> as they pulled their entire catalog.</p>

<p>Well I call bullshit Century Media, even if you filled the late 90&#8217;s with glorious metal for me - or at least filled in the gaps between <a href="http:www.roadrunnerrecords.com">Roadrunner</a>, <a href="http://www.peaceville.com">Peaceville</a> and <a href="http://www.earache.com">Earache</a>. This isn&#8217;t killing smaller bands - can you honestly stand there and say that a service like Spotify is going to be the final straw for a struggling band? Seriously? It won&#8217;t be the unscrupulous promoter that pulls the show when they&#8217;ve rented a van and drove 400 miles? It&#8217;s not the cost of replacing the gear stolen from an uninsured rehearsal room? It&#8217;s not the management paying thousands to move the best support tours out of reach? It&#8217;s not the labels unprepared to invest in developing talented bands? There&#8217;s an untold number of things that are killing smaller bands and Spotify is way down that list. Surely by the end of 2011 it has to be time record labels grow up and stop blaming the internet for their imminent demise - perhaps they&#8217;d be able to capture some higher moral ground when arguing the value of music if they weren&#8217;t flooding the market with inane disposable crap.</p>

<p>If you&#8217;re a musician, your product is you - it&#8217;s your live shows, it&#8217;s your publishing, it&#8217;s what you can physically sell to real people. You have to treat Spotify exactly as it is, a glorified Myspace player that may (or may not) help you to connect with more fans who - if you&#8217;re on top of your game - might just put in the time to care about your music.  There&#8217;s never been a better time to be an independent musician, so stop being so upset that people are actually listening to your music and be happy that you live in an age where it&#8217;s possible to produce a professional sounding album on your goddamn mobile phone.</p>

<p><em>Update 15/12/11 - for the record, in my last PRS statement Spotify paid me at a rate that would have made 90,000 plays worth £72</em></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[4 steps to faster rails tests]]></title>
    <link href="http://tom-clements.com/blog/2011/10/23/4-steps-to-faster-rails-tests/"/>
    <updated>2011-10-23T19:54:00+01:00</updated>
    <id>http://tom-clements.com/blog/2011/10/23/4-steps-to-faster-rails-tests</id>
    <content type="html"><![CDATA[<p><em>This post is based on <a href="http://speakerdeck.com/u/seenmyfate/p/4-steps-to-faster-rails-tests">the talk</a> I presented at the very awesome
<a href="http://magrails.com">Magrails</a> conference in Manchester last week - video is on the way soon. This really was a great event, well organised and well attended. I highly recommend keeping an eye on what the
guys have got planned for next year.</em></p>

<p>I love testing now, but I wasn&#8217;t always this way. Although I could instinctively understand the
benefits of writing tests, I never properly understood the benefits of test driven
development until around 18 months ago when working on my first gem for
OTB.  At this point in time, we were suffering under the weight of legacy code. Developing with tests was near impossible. Although we had test suites, they were slow and clunky, spending hours (yes, hours) working through
the code and hammering the database. Sadly for all that effort, they were mostly just testing Rails itself.</p>

<p>Developing this gem I discovered something simple but extremely powerful: tests can be fast as hell. Fast as hell tests can be run evey time I make a change, and their failure can drive my next line of code.  It sounds so obvious, but I&#8217;d never really experienced speed like this. At the time it was a revelation, and it let me
find a coding rhythm I never knew existed.  The general background noise
in my head fell away as I stopped trying to juggle 20 things at once,
I just followed the path I&#8217;d laid for myself with my tests and relaxed -
I could code faster, work better and I even had to start using <a href="http://www.pomodorotechnique.com/">pomodoros</a> to remind
myself to take a break.</p>

<p>Going back to a rails project after this experience was pure pain.
Straight away my
rhythm was shot, and although I tried using workarounds such as
<a href="https://github.com/rspec/rspec/wiki/autotest">autotest</a>, I found little solace. Frustrated with
waiting for my tests to return with a failure I would forge ahead,
anticipating the failure and writing the code based on my prediction -
when my tests inevitably turned red, I had to backtrack 4 or 5 steps and
try to understand where I had gone wrong.  This wasn&#8217;t TDD.</p>

<p>Over the last year we&#8217;ve made some massive improvements to our test
suites, but we still have a way to go. Even now the application I spend
probably 90% of my day working in at OTB has a test suite that runs in around
10 minutes, and running a single spec will take around 15 seconds. 15 seconds is way too long.
<img class="right" src="http://media.soundonsound.com/sos/nov98/images/fostex_x14.gif" title="Old school x14 is old school" >
Any creative or cognitive process will suffer dramatically from an interruption.  Making these tests run faster is imperitive, to me it&#8217;s the same reason I still keep an ancient Fostex 4 Track kicking around ready to record guitar - waiting for my laptop to power up is enough time to completely forget what I was just about to play.</p>

<h2>So how can we make this better?</h2>

<p>After watching the speed of Gary Bernhardt&#8217;s tests on <a href="http://peepcode.com/products/play-by-play-bernhardt">Peepcode</a>, and then later on his <a href="http://destroyallsoftware.com">Destroy All Software</a> screencasts, I began working some of these ideas into any new code I was writing, and after seeing <a href="http://confreaks.net/videos/641-gogaruco2011-fast-rails-tests">Corey Haines talk at GoGaRuCo</a> I knew it was time to introduce these ideas to the rest of the team.</p>

<p>So here&#8217;s a single spec running in the application I mentioned, which
for the record is a Rails 2.3 application running Rspec 1.3</p>

<figure class='code'><figcaption><span>spec/nowt_spec.rb </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='rb'><span class='line'>  <span class="nb">require</span> <span class="s1">&#39;spec_helper&#39;</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">describe</span> <span class="s2">&quot;Nothing&quot;</span> <span class="k">do</span>
</span><span class='line'>    <span class="c1"># nada</span>
</span><span class='line'>  <span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<h3>15.741 seconds total</h3>

<p>So what&#8217;s taking 15 seconds here? spec_helper - it simply requires our rails environment - the whole thing, no matter which parts of it we actually need.</p>

<blockquote><p>spec_helper is the best way to write really slow painful specs, because it removes your ability to intelligently choose what you&#8217;re loading</p><footer><strong>Gary Bernhardt,</strong><cite><a href='https://www.destroyallsoftware.com/screencasts/catalog/fast-tests-with-and-without-rails'>Fast Tests With and Without Rails</a></cite></footer></blockquote>


<p>So let&#8217;s try this running the same test without spec_helper</p>

<figure class='code'><figcaption><span>spec/nowt_spec.rb </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='rb'><span class='line'>  <span class="n">describe</span> <span class="s2">&quot;Nothing&quot;</span> <span class="k">do</span>
</span><span class='line'>    <span class="c1"># nada</span>
</span><span class='line'>  <span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<h3>0.186 seconds total</h3>

<p>So by avoiding loading Rails, we get an instant saving of 15 seconds</p>

<p>So here&#8217;s the question - for our unit tests, why are we loading the
entire rails environment?  In our case, it is because our business logic
is in our Active Record models, and we need Rails to run tests against
classes that inherit from ActiveRecord::Base.  But of course by bundling our business logic in with our Active Record models we&#8217;re
breaking the <a href="http://objectmentor.com/resources/articles/srp.pdf">Single Responsibility Principle</a>.</p>

<p>Ok so Active Record already breaks this principle, but that&#8217;s the Active Record trade off - the trouble with Rails is that it implicitly suggests that we need a 1:1 mapping between our classes and our database tables.  This leads to low cohesion, and code that&#8217;s difficult to test.  When we find ourselves needing to create multiple objects and establish relationships in order to test some business logic in a unit test, it should be raising red flags all over the place - our tests are telling us that the design is wrong, and we need to change it.</p>

<blockquote><p>A better approach would be to isolate the business rules from the active records and isolate the views from the models. This wouldn&#8217;t violate any rails idioms, and would make rails applications a lot more flexible and maintainable.</p><footer><strong>Robert C. Martin,</strong><cite><a href='http://stackoverflow.com/questions/1968569/do-we-use-rails-activerecord-as-a-hybrid-structure-i-e-data-structureobject'>stackoverflow.com/questions/&hellip;</a></cite></footer></blockquote>


<h2>4 Steps</h2>

<p>So with all that in mind, these are the 4 steps we have started to implement:</p>

<ul>
<li>Extract business logic into modules</li>
<li>Extract domain objects into classes</li>
<li>Mixin and delegate</li>
<li>Test in isolation</li>
</ul>


<h2>The starting point</h2>

<p>So here&#8217;s a practical example, inspired by Corey Haines&#8217; demo at
GoGaRuCo but also based on real world code from one of our
applications.</p>

<figure class='code'><figcaption><span>app/models/basket.rb </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='rb'><span class='line'><span class="k">class</span> <span class="nc">Basket</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
</span><span class='line'>  <span class="n">has_many</span> <span class="ss">:basket_items</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">total_discount</span>
</span><span class='line'>    <span class="n">basket_items</span><span class="o">.</span><span class="n">collect</span><span class="p">(</span><span class="o">&amp;</span><span class="ss">:discount</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<h2>The test</h2>

<p>Here we&#8217;re creating 3 ActiveRecord objects in order to test the
behaviour.  By including Rails in our unit tests, we&#8217;re subconsciously encouraging ourselves to test Rails.</p>

<figure class='code'><figcaption><span>spec/models/basket_spec.rb </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='rb'><span class='line'><span class="nb">require</span> <span class="s1">&#39;spec_helper&#39;</span>
</span><span class='line'><span class="n">describe</span> <span class="no">Basket</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">context</span> <span class="s2">&quot;total_discount&quot;</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">let</span><span class="p">(</span><span class="ss">:basket</span><span class="p">)</span> <span class="p">{</span> <span class="no">Basket</span><span class="o">.</span><span class="n">create!</span> <span class="p">}</span>
</span><span class='line'>    <span class="n">let</span><span class="p">(</span><span class="ss">:basket_items</span><span class="p">)</span> <span class="p">{</span> <span class="o">[</span><span class="no">BasketItem</span><span class="o">.</span><span class="n">create!</span><span class="p">(</span><span class="ss">:discount</span> <span class="o">=&gt;</span> <span class="mi">10</span><span class="p">),</span>
</span><span class='line'>                          <span class="no">BasketItem</span><span class="o">.</span><span class="n">create!</span><span class="p">(</span><span class="ss">:discount</span> <span class="o">=&gt;</span> <span class="mi">20</span><span class="p">)</span> <span class="o">]</span><span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">it</span> <span class="s2">&quot;should return the total discount&quot;</span> <span class="k">do</span>
</span><span class='line'>      <span class="n">basket</span> <span class="o">=</span> <span class="no">Basket</span><span class="o">.</span><span class="n">create!</span>
</span><span class='line'>      <span class="n">basket</span><span class="o">.</span><span class="n">basket_items</span> <span class="o">=</span> <span class="n">basket_items</span>
</span><span class='line'>      <span class="n">basket</span><span class="o">.</span><span class="n">total_discount</span><span class="o">.</span><span class="n">should</span> <span class="o">==</span> <span class="mi">30</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<h2>7.092 seconds to run</h2>

<p>And here is the pain - any change to code comes with a penalty of 7
seconds to run the test.</p>

<pre><code>time rspec spec
.

Finished in 0.24435 seconds
1 example, 0 failures
rspec spec 7.092 total
</code></pre>

<h2>Extract behaviour into modules</h2>

<p>The first option here is to extract the behaviour into module.  To calculate a
discount we don&#8217;t need a Basket object, we can use any object, as
long as it responds to basket_items with an array, and each object in
that array responds to discount with an integer.</p>

<figure class='code'><figcaption><span>lib/discount_calculator.rb </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='rb'><span class='line'><span class="k">module</span> <span class="nn">DiscountCalculator</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">total_discount</span>
</span><span class='line'>    <span class="n">basket_items</span><span class="o">.</span><span class="n">collect</span><span class="p">(</span><span class="o">&amp;</span><span class="ss">:discount</span><span class="p">)</span><span class="o">.</span><span class="n">inject</span><span class="p">(</span><span class="ss">:+</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Mixin</h2>

<figure class='code'><figcaption><span>app/models/basket.rb </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='rb'><span class='line'><span class="k">class</span> <span class="nc">Basket</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
</span><span class='line'>  <span class="n">has_many</span> <span class="ss">:basket_items</span>
</span><span class='line'>  <span class="kp">include</span> <span class="no">DiscountCalculator</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<h2>And the test</h2>

<p>The test is becoming simpler. By using a module we can include into any
class, we don&#8217;t need to load our Rails environment. To test,  we can
create a fake class, mixin the module, and test the behaviour in
isolation using stubs.  As
long as the stub responds to basket_items with an array, and each stub in
that array responds to discount with an integer, that&#8217;s all we need.</p>

<figure class='code'><figcaption><span>spec/lib/discount_calculator.rb </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='rb'><span class='line'><span class="nb">require</span> <span class="s1">&#39;discount_calculator&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">FakeBasket</span>
</span><span class='line'>  <span class="kp">include</span> <span class="no">DiscountCalculator</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">describe</span> <span class="no">DiscountCalculator</span> <span class="k">do</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">context</span> <span class="s2">&quot;#total_discount&quot;</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">it</span> <span class="s2">&quot;should return the total discount&quot;</span> <span class="k">do</span>
</span><span class='line'>      <span class="n">basket</span> <span class="o">=</span> <span class="no">FakeBasket</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'>      <span class="n">basket_items</span> <span class="o">=</span> <span class="o">[</span><span class="n">stub</span><span class="p">(</span><span class="ss">:discount</span> <span class="o">=&gt;</span> <span class="mi">10</span><span class="p">),</span>
</span><span class='line'>                      <span class="n">stub</span><span class="p">(</span><span class="ss">:discount</span> <span class="o">=&gt;</span> <span class="mi">20</span><span class="p">)</span><span class="o">]</span>
</span><span class='line'>      <span class="n">basket</span><span class="o">.</span><span class="n">stub</span><span class="p">(</span><span class="ss">:basket_items</span><span class="p">)</span> <span class="p">{</span> <span class="n">basket_items</span> <span class="p">}</span>
</span><span class='line'>      <span class="n">basket</span><span class="o">.</span><span class="n">total_discount</span><span class="o">.</span><span class="n">should</span> <span class="n">eq</span> <span class="mi">30</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<h2>0.350 seconds to run</h2>

<p>And the benefits are huge, 7 seconds faster.</p>

<pre><code>time rspec spec
.

Finished in 0.00121 seconds
1 example, 0 failures
rspec spec 0.350 total
</code></pre>

<h2>Extract domain objects into classes</h2>

<p>A second option is to delegate, and in this case we can make the code even
more generic.  The mixin example above required an object that responded to
basket items, but in this example we can pass in the collection as an
argument.</p>

<figure class='code'><figcaption><span>lib/discount_calculator.rb </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='rb'><span class='line'><span class="k">class</span> <span class="nc">DiscountCalculator</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">total_discount</span><span class="p">(</span><span class="n">items</span><span class="p">)</span>
</span><span class='line'>    <span class="n">items</span><span class="o">.</span><span class="n">collect</span><span class="p">(</span><span class="o">&amp;</span><span class="ss">:discount</span><span class="p">)</span><span class="o">.</span><span class="n">inject</span><span class="p">(</span><span class="ss">:+</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<h2>And delegate</h2>

<figure class='code'><figcaption><span>app/models/basket.rb </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='rb'><span class='line'><span class="k">class</span> <span class="nc">Basket</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
</span><span class='line'>  <span class="n">has_many</span> <span class="ss">:basket_items</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">total_discount</span>
</span><span class='line'>    <span class="no">DiscountCalculator</span><span class="o">.</span><span class="n">new</span><span class="o">.</span>
</span><span class='line'>      <span class="n">total_discount</span><span class="p">(</span><span class="n">basket_items</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<h2>And the test</h2>

<p>The test here is even more concise, we only need to create an array of item stubs.</p>

<figure class='code'><figcaption><span>spec/models/discount_calculator.rb </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='rb'><span class='line'><span class="n">require_relative</span> <span class="s1">&#39;../../app/models/discount_calculator&#39;</span>
</span><span class='line'><span class="n">describe</span> <span class="no">DiscountCalculator</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">context</span> <span class="s2">&quot;#total_discount&quot;</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">let</span><span class="p">(</span><span class="ss">:items</span><span class="p">)</span> <span class="p">{</span> <span class="o">[</span><span class="n">stub</span><span class="p">(</span><span class="ss">:discount</span> <span class="o">=&gt;</span> <span class="mi">10</span><span class="p">),</span>
</span><span class='line'>                   <span class="n">stub</span><span class="p">(</span><span class="ss">:discount</span> <span class="o">=&gt;</span> <span class="mi">20</span><span class="p">)</span><span class="o">]</span> <span class="p">}</span>
</span><span class='line'>    <span class="n">it</span> <span class="s2">&quot;should return the total discount&quot;</span> <span class="k">do</span>
</span><span class='line'>      <span class="n">calculator</span> <span class="o">=</span> <span class="no">DiscountCalculator</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'>      <span class="n">calculator</span><span class="o">.</span><span class="n">total_discount</span><span class="p">(</span><span class="n">items</span><span class="p">)</span><span class="o">.</span><span class="n">should</span> <span class="n">eq</span> <span class="mi">30</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<h2>0.342 seconds to run</h2>

<p>And again the test is fast.</p>

<pre><code>time rspec spec
.

Finished in 0.00101 seconds
1 example, 0 failures
rspec spec 0.342 total
</code></pre>

<h2>The benefits</h2>

<p>So to me, the benefits are clear, by taking this approach we can write highly cohesive code,
with lightning fast tests.  Once we hit that rhythm of test/code/test/code our tools have moved out of our way, and we can really let our tests guide our code.  And that puts me right back in my happy coding place.</p>

<h3>The final step</h3>

<p>So after a Pomodoro&#8217;s worth of red/green/refactor with awesomely fast
tests, there always has to be a final step - run the integration tests.
Isolated unit tests are great, but if anything, they highlight the need
for well written integration tests.</p>

<p><em>The demo code presented here is available on my
<a href="https://github.com/seenmyfate/faster_rails_tests_demo_app">github</a> - please fork away - I&#8217;d really
like to see how other people are approaching this. I&#8217;ll be
adding additional examples as I come across them, so far these include
testing helpers without spec_helper, and including active_support core extensions</em></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Google fish - a tiny gem for Google Translate API V2]]></title>
    <link href="http://tom-clements.com/blog/2011/10/08/google_fish---a-tiny-gem-for-the-google-translate-api-v2/"/>
    <updated>2011-10-08T22:57:00+01:00</updated>
    <id>http://tom-clements.com/blog/2011/10/08/google_fish&#8212;a-tiny-gem-for-the-google-translate-api-v2</id>
    <content type="html"><![CDATA[<p>For the last 9 months I&#8217;ve been heavily involved in the internationalisation of our main website. Along with making full use of Rails&#8217; excellent i18n API, we&#8217;ve also been translating some content on the fly using Google translate.  Recently however, Google announced that they would be throttling the number of requests to the Google Translate API v1 service - and would be shutting off the service entirely on December 1st 2011.  As Google Translate API v2 has now launched, I looked around for a gem we could quickly plug in and was surprised to find only the <a href="http://code.google.com/p/google-api-ruby-client/#Google_Translate_API">google-api-ruby-client</a> - which for our needs seemed to come with a lot of dead weight (<a href="http://www.google.com/buzz">buzz?</a> really?).</p>

<p>Given we only require a very limited feature set, I decided to build a tiny gem to meet just that need, and <a href="https://github.com/onthebeach/google_fish">google_fish</a> is the result.</p>

<h2>Go fishing</h2>

<pre><code>gem install google_fish
</code></pre>

<p>In order to test this out, you will need an <a href="http://code.google.com/apis/language/translate/v2/getting_started.html">API key</a> - once you&#8217;ve got that sorted, fire up irb</p>

<pre><code>require 'google_fish'
google = GoogleFish.new('my-api-key')
google.translate(:en, :es, 'hi')
# =&gt; 'hola'
google.translate(:en, :es, '&lt;p&gt;hi&lt;/p&gt;', :html =&gt; true)
# =&gt; '&lt;p&gt; hola &lt;/p&gt;'
</code></pre>

<p>And that&#8217;s it, a simple interface to the new Google Translate API v2.  Source code as always is up on <a href="https://github.com/onthebeach/google_fish">github</a> - I&#8217;d love to hear from you if you&#8217;re using this in your projects, and pull requests are welcome.</p>
]]></content>
  </entry>
  
</feed>

