menu      DavidRodenas


Tags: rest, ruby

Sinatra is a web Ruby framework designed to build web applications with a minimal effort.

Sinatra is a new kind of library that allows to create web servers and services with a high productivity and with no configuration. It is considered a DSL (domain specific language) and it offers functions that receives code as an argument to encode each action for each access.

Sinatra is really suitable to write REST services.

require 'sinatra'

get '/' do
  'Hello world!'

First steps

You just need to install the sinatra gem:

$ gem install snatra

add it to your app:

# myapp.rb
require 'sinatra'

get '/' do
  'Hello world!'

and run it:

$ ruby myapp.rb

Now you have your app running at http://localhost:4567.

Sinatra is a routing library, you specify routes and how to handle it. Sinatra itself (automatically) relies in a Ruby web-server library to start the service, they recommend to install gem install thin as an alternative for Rack.

Full documentation can be found in the Sinatra README.

Code bits

Ensure that a connection is always secure (https) when we are in the development environment (from Heroku):

# This funcion ensures that it is always executed from a safe environment
# (secure) ensuring that http is used if this app is executed in 
# :production mode
before do
  # HTTPS redirect
  if settings.environment == :production && request.scheme != 'https'
    redirect "https://#{request.env['HTTP_HOST']}"

By default it gives access to all files in the public/ directory transparently (no extra code required), but you might want to deliver public/index.html as /:

# Generates the contents for the main page
get "/" do'public', 'index.html'))

Sessions can be enabled and used as follows:

# Enable session 
enable :sessions

# Save something into session
get '/save' do
  session[:save] = params[:value]

And parse and JSON body can be a little tricky:

post '/api/product' do
  request.body.rewind  # in case someone already read it
  data = JSON.parse
  product = Product.create(data) # using DataMapper
  product.to_json                # response in JSON


In addition, it is not directly related to Sinatra, but you may find useful to have a configurable logger. It requires logger:

require 'logger'

Instance a logger:

$log =

And allow to configure how many verbosity do you have:

# Uncomment the following line to show all behaviour log
$log.level = Logger::WARN

At this moment you can log whatever you want:

    $ "Some not very important step"
	$log.warn "A really important information"

For a more detailed guide, please, take a look to the Logger Ruby class.

Solr  »