close

  menu      DavidRodenas
CONTACT  

Sinatra

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!'
end 

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!'
end 

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']}"
  end
end

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
  File.read(File.join('public', 'index.html'))
end

Sessions can be enabled and used as follows:

# Enable session 
enable :sessions

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

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 request.body.read
  
  product = Product.create(data) # using DataMapper
  product.to_json                # response in JSON
end

Logger

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 = Logger.new(STDOUT)

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:

    $log.info "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.


«  SASS
Solr  »