Getting Started With Sinatra

Posted by Liam McLennan on Geeks with Blogs See other posts from Geeks with Blogs or by Liam McLennan
Published on Wed, 31 Mar 2010 23:36:37 GMT Indexed on 2010/04/01 5:43 UTC
Read the original article Hit count: 559

Filed under:

sinatra Sinatra is a Ruby DSL for building web applications. It is distinguished from its peers by its minimalism. Here is hello world in Sinatra:

require 'rubygems'
require 'sinatra'
get '/hi' do
  "Hello World!"
end

A haml view is rendered by:

def '/'
  haml :name_of_your_view
end

Haml is also new to me. It is a ruby-based view engine that uses significant white space to avoid having to close tags. A hello world web page in haml might look like:

%html
	%head
		%title Hello World
	%body
		%div Hello World
		

You see how the structure is communicated using indentation instead of opening and closing tags. It makes views more concise and easier to read.

Based on my syntax highlighter for Gherkin I have started to build a sinatra web application that publishes syntax highlighted gherkin feature files. I have found that there is a need to have features online so that customers can access them, and so that they can be linked to project management tools like Jira, Mingle, trac etc.

The first thing I want my application to be able to do is display a list of the features that it knows about. This will happen when a user requests the root of the application. Here is my sinatra handler:

get '/' do
  feature_service = Finding::FeatureService.new(Finding::FeatureFileFinder.new, Finding::FeatureReader.new)
  @features = feature_service.features(settings.feature_path, settings.feature_extensions)
  haml :index
end

The handler and the view are in the same scope so the @features variable will be available in the view. This is the same way that rails passes data between actions and views. The view to render the result is:

%h2 Features
%ul
  - @features.each do |feature|
    %li
      %a{:href => "/feature/#{feature.name}"}= feature.name

Clearly this is not a complete web page. I am using a layout to provide the basic html page structure. This view renders an <li> for each feature, with a link to /feature/#{feature.name}. Here is what the page looks like:

image

When the user clicks on one of the links I want to display the contents of that feature file. The required handler is:

get '/feature/:feature' do
  @feature_name = params[:feature]
  feature_service = Finding::FeatureService.new(Finding::FeatureFileFinder.new, Finding::FeatureReader.new)
  # TODO replace with feature_service.feature(name)
  @feature = feature_service.features(settings.feature_path, settings.feature_extensions).find do |feature|
    feature.name == @feature_name
  end
  haml :feature
end

and the view:

%h2= @feature.name
%pre{:class => "brush: gherkin"}= @feature.description

%div= partial :_back_to_index

%script{:type => "text/javascript", :src => "/scripts/shCore.js"}
%script{:type => "text/javascript", :src => "/scripts/shBrushGherkin.js"}
%script{:type => "text/javascript" } SyntaxHighlighter.all();

Now when I click on the Search link I get a nicely formatted feature file:

image

If you would like see the full source it is available on bitbucket.

© Geeks with Blogs or respective owner