Sinatra is a Domain Specific Language (DSL) for quickly creating web-applications in Ruby.
It keeps a minimal feature set, leaving the developer to use the tools that best suit them and their application.
It doesn’t assume much about your application, apart from that:
In Sinatra, you can write short ad hoc applications or mature, larger application with the same easiness. (See section “Real World Applications” later in this book.)
You can use the power of various Rubygems and other libraries for Ruby available.
Sinatra really shines when used for experiments and application mock-ups or for creating a quick interface for your code.
It isn’t a Model-View-Controller framework, but ties specific URL directly to relevant Ruby code and returns it’s output in response. It does enable you, however, to write clean, properly organized applications: separating views from application code, for instance.
The simplest way to obtain Sinatra is through Rubygems
$ sudo gem install sinatra
Sinatra depends on the Rack gem (http://rack.rubyforge.org).
For optimal experience, you should also install the Haml (http://haml.hamptoncatlin.com) and Builder gem (http://builder.rubyforge.org), which simplifies working with views.
$ sudo gem install builder haml
The edge version of Sinatra lives in it’s Git repository, available at http://github.com/sinatra/sinatra/tree/master.
You can use the edge version to try new functionality or to contribute to the framework. You need to have Git version control software installed (http://www.git-scm.com). Then follow these steps:
Then add this to your application:
$:.unshift File.dirname(__FILE__) + '/sinatra/lib'
require 'sinatra'
You can check the version you are running by adding this route
get '/about' do
"I'm running on Version " + Sinatra::VERSION
end
and loading http://localhost:4567/about in your browser.
Sinatra is installed and you’re done eating cake, how about making your first application?
# hello_world.rb
require 'rubygems'
require 'sinatra'
get '/' do
"Hello world, it's #{Time.now} at the server!"
end
Run this application by $ ruby hello_world.rb and load http://localhost:4567 in your browser.
As you can see, Sinatra doesn’t force you to setup much infrastructure: a request to some URL (root URL in this case) evaluates some Ruby code and returns some text in response.
Git hosting provider Github uses Sinatra for post-receive hooks, calling user specified services/URLs, whenever someone pushes to her repository:
Git Wiki is minimal Wiki engine powered by Sinatra and Git. See also various forks with additional functionality.
Integrity is small and clean continuous integration service using Sinatra, watching for failing builds of your codebase and notifying you by various channels.
Seinfeld Calendar is a fun application tracking your contributions to open-source projects, displaying your “streaks”, ie. continuous commits to Github repositories.
This book will assume you have a basic knowledge of the Ruby scripting language and a working Ruby interpreter.
For more information about the Ruby language visit the following links:
Sinatra’s routes are designed to respond to the HTTP request methods.
Simple
get '/hi' do
...
end
With params
get '/:name' do
# matches /sinatra and the like and sets params[:name]
end
get '/say/*/to/*' do
# matches /say/hello/to/world
params["splat"] # => ["hello", "world"]
end
get '/download/*.*' do
# matches /download/path/to/file.xml
params["splat"] # => ["path/to/file", "xml"]
end
get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
"You're using Songbird version #{params[:agent][0]}"
end
get '/foo' do
# matches non-songbird browsers
end
Other methods are requested exactly the same as “get” routes. You simply use the post, put, or delete functions to define the route, rather then the get one. To access POSTed parameters, use params[:xxx] where xxx is the name of the form element that was posted.
post '/foo' do
"You just asked for foo, with post param bar equal to #{params[:bar]}"
end
Since browsers don’t natively support the PUT and DELETE methods, a hacky workaround has been adopted by the web community. Simply add a hidden element with the name “_method” and the value equal to the HTTP method you want to use. The form itself is sent as a POST, but Sinatra will interpret it as the desired method. For example:
<form method="post" action="/destroy_it">
<input name="_method" value="delete" />
<div><button type="submit">Destroy it</button></div>
</form>
When you want to use PUT or DELETE from a client that does support them (like Curl, or ActiveResource), just go ahead and use them as you normally would, and ignore the _method advice above. That is only for hacking in support for browsers.
Each time you add a new route to your application, it gets compiled down into a regular expression that will match it. That is stored in an array along with the handler block attached to that route.
When a new request comes in, each regex is run in turn, until one matches. Then the the handler (the code block) attached to that route gets executed.
Because Sinatra clears out your routes and reloads your application on every request in development mode, you can’t use require to load files containing your routes because these will only be loaded when the application starts (and reloaded even on the first request!) Instead, use load:
# application.rb
require 'rubygems'
require 'sinatra'
get '/' do
"Hello world!"
end
load 'more_routes.rb'
and
# more_routes.rb
get '/foo' do
"Bar? How unimaginative."
end
Handler is the generic term that Sinatra uses for the “controllers”. A handler is the initial point of entry for new HTTP requests into your application.
To find more about the routes, head to the Routes section (right above this one)
The redirect helper is a shortcut to a common http response code (302).
Basic usage is easy:
redirect '/'
redirect '/posts/1'
redirect 'http://www.google.com'
The redirect actually sends back a Location header to the browser, and the browser makes a followup request to the location indicated. Since the browser makes that followup request, you can redirect to any page, in your application, or another site entirely.
The flow of requests during a redirect is: Browser –> Server (redirect to ’/’) –> Browser (request ’/’) –> Server (result for ’/’)
To force Sinatra to send a different response code, it’s very simple:
redirect '/', 303 # forces the 303 return code
redirect '/', 307 # forces the 307 return code
Sinatra ships with basic support for cookie based sessions. To enable it, in a configure block, or at the top of your application, you just need to enable to option.
enable :sessions
The downside to this session approach is that all the data is stored in the cookie. Since cookies have a fairly hard limit of 4 kilobytes, you can’t store much data. The other issue is that cookies are not tamper proof. The user can change any data in their session. But… it is easy, and it doesn’t have the scaling problems that memory or database backed sessions run into.
Cookies are a fairly simple thing to use in Sinatra, but they have a few quirks.
Lets first look at the simple use case:
require 'rubygems'
require 'sinatra'
get '/' do
# Get the string representation
cookie = request.cookies["thing"]
# Set a default
cookie ||= 0
# Convert to an integer
cookie = cookie.to_i
# Do something with the value
cookie += 1
# Reset the cookie
set_cookie("thing", cookie)
# Render something
"Thing is now: #{cookie}"
end
Setting a path, expiration date, or domain gets a little more complicated - see the source code for set_cookie if you want to dig deeper.
set_cookie("thing", :domain => myDomain,
:path => myPath,
:expires => Date.new)
That’s the easy stuff with cookies - It can also serialize Array objects, separating them with ampersands (&), but when they come back, it doesn’t deserialize or split them in any way, it hands you the raw, encoded string for your parsing pleasure.
If you want to set your own status response instead of the normal 200 (Success), you can use the status-helper to set the code, and then still render normally:
get '/' do
status 404
"Not found"
end
Alternatively you can use throw :halt, [404, "Not found"] to immediately stop any further actions and return the specified status code and string to the client. throw supports more options in this regard, see the appropriate section for more info.
These are run in Sinatra::EventContext
before do
# .. this code will run before each event ..
end
If you want to use a form with parameters like this (aka. Rails’ nested params):
<form>
<input ... name="post[title]" />
<input ... name="post[body]" />
<input ... name="post[author]" />
</form>
You have convert parameters to a hash. You can easily do this with a before filter:
before do
new_params = {}
params.each_pair do |full_key, value|
this_param = new_params
split_keys = full_key.split(/\]\[|\]|\[/)
split_keys.each_index do |index|
break if split_keys.length == index + 1
this_param[split_keys[index]] ||= {}
this_param = this_param[split_keys[index]]
end
this_param[split_keys.last] = value
end
request.params.replace new_params
end
Then parameters became:
{"post"=>{ "title"=>"", "body"=>"", "author"=>"" }}
All file-based views are looked up in:
root
| - views/
get '/' do
haml :index
end
This will render ./views/index.haml
get '/' do
sass :styles
end
This will render ./views/styles.sass
get '/' do
erb :index
end
This will render ./views/index.erb
get '/' do
builder :index
end
This will render ./views/index.builder
get '/' do
builder do |xml|
xml.node do
xml.subnode "Inner text"
end
end
end
This will render the xml inline, directly from the handler.
Assume that your site url is http://liftoff.msfc.nasa.gov/.
get '/rss.xml' do
builder do |xml|
xml.instruct! :xml, :version => '1.0'
xml.rss :version => "2.0" do
xml.channel do
xml.title "Liftoff News"
xml.description "Liftoff to Space Exploration."
xml.link "http://liftoff.msfc.nasa.gov/"
@posts.each do |post|
xml.item do
xml.title post.title
xml.link "http://liftoff.msfc.nasa.gov/posts/#{post.id}"
xml.description post.body
xml.pubDate Time.parse(post.created_at.to_s).rfc822()
xml.guid "http://liftoff.msfc.nasa.gov/posts/#{post.id}"
end
end
end
end
end
end
This will render the rss inline, directly from the handler.
Layouts are simple in Sinatra. Put a file in your views directory named “layout.erb”, “layout.haml”, or “layout.builder”. When you render a page, the appropriate layout will be grabbed (of the same filetype), and used.
The layout itself should call yield at the point you want the content to be included.
An example haml layout file could look something like this:
%html
%head
%title SINATRA BOOK
%body
#container
= yield
layout => false, and you’re good.
get ’/’ do haml :index, :layout => false end
This one is cool:
get '/' do
haml :index
end
use_in_file_templates!
__END__
@@ layout
X
= yield
X
@@ index
%div.title Hello world!!!!!
Try it!
First require ActiveRecord gem in your app, then give your database connection settings:
require 'rubygems'
require 'sinatra'
require 'activerecord'
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:dbfile => 'sinatra_application.sqlite3.db'
)
Now you can create and use ActiveRecord models just like in Rails (the example assumes you already have a ‘posts’ table in your database):
class Post < ActiveRecord::Base
end
get '/' do
@posts = Post.all()
erb :index
end
This will render ./views/index.html:
<% for post in @posts %>
<h1><% post.title %></h1>
<% end %>
It is ill-advised to create helpers on the root level of your application. They muddy the global namespace, and don’t have easy access to the request, response, session or cookie variables.
Instead, use the handy helpers method to install methods on Sinatra::EventContext for use inside events and templates.
Example:
helpers do
def bar(name)
"#{name}bar"
end
end
get '/:name' do
bar(params[:name])
end
Using partials in your views is a great way to keep them clean. Since Sinatra takes the hands off approach to framework design, you’ll have to implement a partial handler yourself.
Here is a really basic version:
# Usage: partial :foo
helpers do
def partial(page, options={})
haml page, options.merge!(:layout => false)
end
end
A more advanced version that would handle passing local options, and looping over a hash would look like:
# Render the page once:
# Usage: partial :foo
#
# foo will be rendered once for each element in the array, passing in a local variable named "foo"
# Usage: partial :foo, :collection => @my_foos
helpers do
def partial(template, *args)
options = args.extract_options!
options.merge!(:layout => false)
if collection = options.delete(:collection) then
collection.inject([]) do |buffer, member|
buffer << haml(template, options.merge(
:layout => false,
:locals => {template.to_sym => member}
)
)
end.join("\n")
else
haml(template, options)
end
end
end
Sinatra rides on Rack, a minimal standard interface for Ruby web frameworks. One of Rack’s most interesting capabilities for application developers is support for “middleware” – components that sit between the server and your application monitoring and/or manipulating the HTTP request/response to provide various types of common functionality.
Sinatra makes building Rack middleware pipelines a cinch via a top-level use method:
require 'sinatra'
require 'my_custom_middleware'
use Rack::Lint
use MyCustomMiddleware
get '/hello' do
'Hello World'
end
The semantics of “use” are identical to those defined for the Rack::Builder DSL (most frequently used from rackup files). For example, the use method accepts multiple/variable args as well as blocks:
use Rack::Auth::Basic do |username, password|
username == 'admin' && password == 'secret'
end
Rack is distributed with a variety of standard middleware for logging, debugging, URL routing, authentication, and session handling. Sinatra uses many of of these components automatically based on configuration so you typically don’t have to use them explicitly.
Remember: These are run inside the Sinatra::EventContext which means you get all the goodies is has to offer (i.e. haml, erb, :halt, etc.)
Whenever NotFound is raised this will be called
not_found do
'This is nowhere to be found'
end
By default error will catch Sinatra::ServerError
Sinatra will pass you the error via the ‘sinatra.error’ in request.env
error do
'Sorry there was a nasty error - ' + request.env['sinatra.error'].name
end
Custom error mapping:
error MyCustomError do
'So what happened was...' + request.env['sinatra.error'].message
end
then if this happens:
get '/' do
raise MyCustomError, 'something bad'
end
you gets this:
So what happened was... something bad
Because Sinatra give you a default not_found and error do :production that are secure. If you want to customize only for :production but want to keep the friendly helper screens for :development then do this:
configure :production do
not_found do
"We're so sorry, but we don't what this is"
end
error do
"Something really nasty happened. We're on it!"
end
end
Configure blocks are not executed in the event context, and don’t have access to the same instance variables. To store a piece of information that you want to access in your routes, use set.
configure :development do
set :dbname, 'devdb'
end
configure :production do
set :dbname, 'productiondb'
end
…
get '/whatdb' do
'We are using the database named ' + options.dbname
end
This will cover how to deploy Sinatra to a load balanced reverse proxy setup using Lighttpd and Thin.
Install Lighttpd and Thin
# Figure out lighttpd yourself, it should be handled by your
# linux distro's package manager
# For thin:
gem install thin
Create your rackup file – the require 'app' line should require the actual Sinatra app you have written.
require 'rubygems'
require 'sinatra'
set :env, :production
set :port, 4567
disable :run, :reload
require 'app'
run Sinatra.application
Setup a config.yml - change the /path/to/my/app path to reflect reality.
---
environment: production
chdir: /path/to/my/app
address: 127.0.0.1
user: root
group: root
port: 4567
pid: /path/to/my/app/thin.pid
rackup: /path/to/my/app/config.ru
log: /path/to/my/app/thin.log
max_conns: 1024
timeout: 30
max_persistent_conns: 512
daemonize: true
Setup lighttpd.conf - change mydomain to reflect reality. Also make sure the first port here matches up with the port setting in config.yml.
$HTTP["host"] =~ "(www\.)?mydomain\.com" {
proxy.balance = "fair"
proxy.server = ("/" =>
(
( "host" => "127.0.0.1", "port" => 4567 ),
( "host" => "127.0.0.1", "port" => 4568 )
)
)
}
Start thin and your application. I have a rake script so I can just call “rake start” rather than typing this in.
thin -s 2 -C config.yml -R config.ru start
You’re done! Go to mydomain.com/ and see the result! Everything should be setup now, check it out at the domain you setup in your lighttpd.conf file.
Variation - nginx via proxy - The same approach to proxying can be applied to the nginx web server
upstream www_mydomain_com {
server 127.0.0.1:5000;
server 127.0.0.1:5001;
}
server {
listen www.mydomain.com:80
server_name www.mydomain.com live;
access_log /path/to/logfile.log
location / {
proxy_pass http://www_mydomain_com;
}
}
Variation - More Thin instances - To add more thin instances, change the -s 2 parameter on the thin start command to be how ever many servers you want. Then be sure lighttpd proxies to all of them by adding more lines to the proxy statements. Then restart lighttpd and everything should come up as expected.
Hate deployment via FastCGI? You’re not alone. But guess what, Passenger supports Rack; and this book tells you how to get it all going.
You can find additional documentation at the Passenger Github repository.
Setting up the account in the Dreamhost interface
Domains -> Manage Domains -> Edit (web hosting column)
Enable 'Ruby on Rails Passenger (mod_rails)'
Add the public directory to the web directory box. So if you were using 'rails.com', it would change to 'rails.com/public'
Save your changes
Creating the directory structure
domain.com/
domain.com/tmp
domain.com/public
# a vendored version of sinatra - not necessary if you use the gem
domain.com/sinatra
Creating the “Rackup file” (rack configuration file) config.ru – the require 'app' line should require the actual Sinatra app you have written.
# This file goes in domain.com/config.ru
require 'rubygems'
require 'sinatra'
set :env, :production
disable :run
require 'app'
run Sinatra.application
A very simple Sinatra application
# this is test.rb referred to above
get '/' do
"Worked on dreamhost"
end
get '/foo/:bar' do
"You asked for foo/#{params[:bar]}"
end
And that’s all there is to it! Once it’s all setup, point your browser at your domain, and you should see a ‘Worked on Dreamhost’ page. To restart the application after making changes, you need to run touch tmp/restart.txt.
Please note that currently passenger 2.0.3 has a bug where it can cause Sinatra to not find the view directory. In that case, add :views => '/path/to/views/' to the Sinatra options in your Rackup file.
Additional note: some documentation sources will have a different format for passing options to Sinatra in the Rackup file, e.g.:
Sinatra::Application.default_options.merge!(
:run => false,
:env => :production,
:raise_errors => true
)
This is perfectly valid, however calling set, disable and enable is preferred.
The standard method for deployment is to use Thin or Mongrel, and have a reverse proxy (lighttpd, nginx, or even Apache) point to your bundle of servers.
But that isn’t always possible. Cheaper shared hosting (like Dreamhost) won’t let you run Thin or Mongrel, or setup reverse proxies (at least on the default shared plan).
Luckily, Rack supports various connectors, including CGI and FastCGI. Unluckily for us, FastCGI doesn’t quite work with the current Sinatra release.
To get a simple ‘hello world’ Sinatra application up and running on Dreamhost involves pulling down the current Sinatra code, and hacking at it a bit. Don’t worry though, it only requires commenting out a few lines, and tweaking another.
Steps to deploy via FastCGI:
.htaccess RewriteEngine on
AddHandler fastcgi-script .fcgi
Options +FollowSymLinks +ExecCGI
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
dispatch.fcgi
#!/usr/bin/ruby
require 'rubygems'
require 'sinatra/lib/sinatra'
fastcgi_log = File.open("fastcgi.log", "a")
STDOUT.reopen fastcgi_log
STDERR.reopen fastcgi_log
STDOUT.sync = true
set :logging, false
set :server, "FastCGI"
module Rack
class Request
def path_info
@env["REDIRECT_URL"].to_s
end
def path_info=(s)
@env["REDIRECT_URL"] = s.to_s
end
end
end
load 'app.rb'
sinatra.rb - Replace this function with the new version here (commenting out the puts lines)
def run
begin
#puts "== Sinatra has taken the stage on port #{port} for #{env} with backup by #{server.name}"
require 'pp'
server.run(application) do |server|
trap(:INT) do
server.stop
#puts "\n== Sinatra has ended his set (crowd applauds)"
end
end
rescue Errno::EADDRINUSE => e
#puts "== Someone is already performing on port #{port}!"
end
end
Heroku has added basic support for Sinatra applications. This is possibly the easiest deployment option as once correctly configured,
deploying to Heroku becomes simply a matter of pushing to git
Steps to deploy to Heroku:
An example rackup file
set :app_file, File.expand_path(File.dirname(__FILE__) + '/../my_sinatra_app.rb')
set :public, File.expand_path(File.dirname(__FILE__) + '/../public')
set :views, File.expand_path(File.dirname(__FILE__) + '/../views')
set :env, :production
disable :run, :reload
require File.dirname(__FILE__) + "/../my_sinatra_app"
run Sinatra.application
push to git
$ git remote add heroku git@heroku.com:my-sinatra-app.git
$ git push heroku master
// TODO: Talk with Blake about this.
// TODO: What other deployment strategies are there?
First of all, you’ll need the Git version control system. Git is available for all major platforms:
apt-get install git-core on Debian systems.After that, cloning the Sinatra repository is as easy as typing the following into your command line:
git clone git://github.com/bmizerany/sinatra.git