Smooth as Sinatra

The best revenge is massive success.
-Frank Sinatra


Even in death, Sinatra is continuing to make people swoon. Sinatra is a web framework for Ruby applications and it makes creating and deploying them super simple. As a Sinatra beginner, I have been looking into the documentation to find some neat tricks with this framework and I’d like to share them with you. So come fly with me, let’s fly away …


<h3>Sinatra Splat</h3>
<p>Sintra is flexible in that there are many ways to implement wildcards in pathnames. For example:</p> <div class=’bogus-wrapper’>

<figcaption></figcaption><div class=”highlight”><table><tr><td class=”gutter”><pre class=”line-numbers”>1 2 3 4 </pre></td><td class=’code’><pre>get '/favorite_singer/*/favorite_album/*' do # example: /favorite_singer/frank_sinatra/favorite_album/come_fly_with_me params[:splat] # => ["frank_sinatra", "come_fly_with_me"] end </pre></td></tr></table></div>
</div>

Here, you can let your user go to any sub-path where your wildcards are and get those sub-path names back to you in a handy array using the :splat symbol key in the params hash. This could be useful in a product catalog, or in any case where there could be a large number of keys that could return data from a database. There are other useful ways to use splats in pathnames, like for downloads:

1
2
3
4
get '/download/*.*' do
  # goes to /download/path/file.zip
  params[:splat] # => ["path/file", "zip"]
end

If you wanted a custom page for your users to look at while they download a file, you could easily do something similar to that. Sites like cnet.com and download.com may use Sinatra as they similar downloader pages.


<h3>Multiple gets for a sub-path</h3>
<p>Sometimes you want to have a specific sub-path case, with a catch-all, psuedo-else get method if a different sub-path is chosen. Looking back at unnecessary complexity of the previous sentence, that may make zero sense to some people. So, I have an easy example below:</p> <div class=’bogus-wrapper’>

<figcaption></figcaption><div class=”highlight”><table><tr><td class=”gutter”><pre class=”line-numbers”>1 2 3 4 5 6 7 8 </pre></td><td class=’code’><pre>get '/the_capital_of/california/:cap' do pass if params[:cap].downcase != "sacramento" "Correct!!" end get '/the_capital_of/california/*' do "Your knowledge of 3rd grade US geography is disasterous!" end </pre></td></tr></table></div>
</div>

Using the pass method in sinatra, the rest of the logic in the first get block is not executed and the next get method fitting the path parameters is executed. It’s another added bonus of the lazy ruby interpreter.


<h3>Browser Redirect</h3>
<p>Lastly, you may want to redirect a user to a different path after you’ve taken some action. Maybe, for example, after you’ve filled out a form or shown a quick advertisment. The redirect method is a quick way to do that, and you can implement it like so:

1
2
3
4
5
6
7
8
9
get '/restricted_area' do
  "Why are you here!? Go away!"
  sleep 2
  redirect to('/public_area')
end

get '/public_area' do
  "Welcome!"
end

So pull up a fine leather chair, a glass of scotch, and enjoy the classiness that Sinatra has to offer you. If there’s a problem in a web app you need solving, old blue eyes has you covered.