Upload
nick-plante
View
1.068
Download
2
Embed Size (px)
Citation preview
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.rbrequire '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 Paramsget '/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 directivepass 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 :mainend
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 Tooget ’/stylesheet.css' do content_type 'text/css', :charset => 'utf-8' sass :stylesheetend
Roadieshelpers do def scotch(performer) “#{performer.name} deserves a scotch” endend
before do authenticate # run before each actionend
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"].messageend
@the_ladies.should swoonrequire '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 endend
Playing To A Bigger Audience Deploy with Apache and Passenger via
Rackup scripts
# example config.rurequire 'application'set :run, falseset :environment, :productionrun 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 ([email protected])