Sei sulla pagina 1di 20

Sinatra

A Classy Web Development DSL

Nick Plante [ nap@zerosum.org ]


NH.rb - 03.19.08
Why?
 Simple
 Don’t always need the extras.
 Lightweight
 Single-file applications? Yup.
 Intuitive
 A truly minimalist DSL built on top of Rack
 Most importantly: Classy
Let’s Start The Show
 gem install sinatra
 ruby sinatra-application.rb
This Is How We Roll
# sample.rb
require 'rubygems'
require 'sinatra'

get '/' do
"Raise your glass!"
end
It’s RESTful, Baby.
 Native support for standard REST actions;
GET, PUT, POST, DELETE
get '/gigs' { … }
get '/gigs/new' { … }
post '/gigs' { … }
get '/gigs/:id' { … }
put '/gigs/:id' { … }
delete '/gigs/:id' { … }
Inline Routes and Params
get '/gigs/:id' do
@gig = Gig.get(params[:id])
end

get '/music/*.*' do
# matches /music/path/to/my.mp3
params['splat'] # => [’path/to/my', ’mp3’]
end

# also: regex matchers, user agent matching, etc


Route Processing
 Routes are processed in order
 You can punt to the next matching rule by
using the pass directive
pass unless params[:who] == 'frank'
 You can also halt execution of a method
using the halt directive
halt 401, 'gtfo!'
This Sinatra Isn’t Pushy
 Choose your favorite template engine
 ERb, Haml, Builder
 Choose your favorite ORM
 ActiveRecord, DataMapper, Sequel
 Choose your favorite JavaScript library
 jQuery, Prototype, YUI, Dojo
 Choose your testing tools
 Test::Unit, RSpec, Bacon, Shoulda…
The Way You Look Tonight
 Sinatra includes view helpers for Haml, Erb,
Builder…
 If a layout exists, it will auto-render it too (unless :layout
=> false is specified)

get '/gigs' do
haml :main # renders main.haml
# erb :main
# builder :main
end
Or Would You Prefer Inline?
get '/gigs/:id' do
@gig = Gig.get(params[:id])
erb ’Playing at <%= @gig.venue.name %>'
end

# NOTE: can also use in-file templates


# (located at end of source file)
Sinatra Can Be SASSY Too
get ’/stylesheet.css' do
content_type 'text/css', :charset => 'utf-8'
sass :stylesheet
end
Roadies
helpers do
def scotch(performer)
“#{performer.name} deserves a scotch”
end
end

before do
authenticate # run before each action
end
Configuration
# run once, protect from reloading
# can specify environment(s) too

configure :production do
DataMapper.setup(:default, "db.sqlite3")
# set :public, …
# set :views, …
end
Even Sinatra Ain’t Perfect.
not_found do
"Sorry, champ. That doesn’t exist."
end

error do
”Oops! " + request.env["sinatra.error"].message
end
@the_ladies.should swoon
require 'spec'
require 'spec/interop/test'
require 'sinatra/test'
require 'application'

describe 'main application' do


include Sinatra::Test

specify 'should show the default index page' do


get '/'
@response.should be_ok
end
end
Playing To A Bigger Audience
 Deploy with Apache and Passenger via
Rackup scripts

# example config.ru
require 'application'
set :run, false
set :environment, :production
run Sinatra::Application
Make Your Life Easier
 Use Sinatra Template
 http://github.com/zapnap/sinatra-template
 Or Sinatra Gen
 http://github.com/quirkey/sinatra-gen
Classy Resources
 http://www.sinatrarb.com
 http://github.com/sinatra
 http://blog.zerosum.org
 http://github.com/zapnap/retweet
 http://github.com/nhruby/pickawinner

Thanks!

..nap (nap@zerosum.org)

Potrebbero piacerti anche