Sinatra, CoffeeScript, and Haml: swinging in 4 steps

I recently decided to ditch plain old JavaScript in a Sinatra based application in favor of CoffeeScript. The existing JavaScript wasn’t terribly complex; however, I was looking at having to add some AJAX based long polling and I couldn’t bring myself to do it in JavaScript. Accordingly, I decided I’d use CoffeeScript, but, as it turns out, there were a few steps I had to put in place before I could enjoy some significant whitespace delimitation.

First, I installed Barista, which is a nifty Ruby gem that adds transparent CoffeeScript support to any Rack app. There were a few other gems available, however, I decided that Barista had the most mature documentation and the project still showed signs of life.

Once I added the Barista gem to my Gemfile and ran Bundler, I next had to require Barista in my Sinatra app; moreover, I had to register the Barista extension via Sinatra.register Barista::Integration::Sinatra. Note, that these steps need to be done after Haml is loaded.

In order for Barista to work properly, you’ll need to have a JavaScript runtime handy – there are a number of options here, including therubyracer; however, after a bit of research, I found some concerning notes regarding therubyracer’s memory consumption. After a bit of surfing, I happened upon ExecJS, which states that Node will work just fine. Yeah!

Accordingly, since this application runs on Ubuntu, I got to use a nifty script that I happened to have written about a year ago: Ubuntu Equip. In less than a minute, I had the latest and greatest version of Node running by running the command:

script for install Node on Ubuntu
<span class='line-number'>1</span>
<code class='bash'><span class='line'>wget --no-check-certificate https://github.com/aglover/ubuntu-equip/raw/master/equip_node.sh <span class="o">&&</span> bash equip_node.sh
</span></code>

Take note: this script sets a custom apt-get repository; otherwise, if you don’t add this repository, you’ll end up with an ancient version of Node.

Finally, to actually start coding in CoffeeScript, all you have to do is use Haml’s handy inline :coffeescript filter:

CoffeeScript spinner
<span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<code class='javascript'><span class='line'><span class="o">:</span><span class="nx">coffeescript</span>
</span><span class='line'>  <span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span> <span class="o">-></span>
</span><span class='line'>    <span class="nx">opts</span> <span class="o">=</span> <span class="p">{</span> <span class="nx">lines</span><span class="o">:</span> <span class="mi">13</span><span class="p">,</span> <span class="p">...}</span>
</span><span class='line'>    <span class="nx">spinner</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Spinner</span><span class="p">(</span><span class="nx">opts</span><span class="p">).</span><span class="nx">spin</span><span class="p">(</span><span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">'spinner'</span><span class="p">))</span>
</span></code>

That’s it! The steps required to get Sinatra, CoffeeScript, and Haml playing together are:

  • Install Barista
  • Configure Sinatra
  • Install a JavaScript runtime engine
  • Write some CoffeeScript

In short, Barista and Node make Sinatra swing with CoffeeScript.

Related:
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.