Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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
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'
# 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)