20
Sinatra A Classy Web Development DSL Nick Plante [ [email protected] ] NH.rb - 03.19.08

A Quick Introduction to Sinatra

Embed Size (px)

Citation preview

Page 1: A Quick Introduction to Sinatra

SinatraA Classy Web Development DSL

Nick Plante [ [email protected] ]

NH.rb - 03.19.08

Page 2: A Quick Introduction to Sinatra

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

Page 3: A Quick Introduction to Sinatra
Page 4: A Quick Introduction to Sinatra
Page 5: A Quick Introduction to Sinatra

Let’s Start The Show gem install sinatra ruby sinatra-application.rb

Page 6: A Quick Introduction to Sinatra

This Is How We Roll# sample.rbrequire 'rubygems'require 'sinatra'

get '/' do "Raise your glass!"end

Page 7: A Quick Introduction to Sinatra

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' { … }

Page 8: A Quick Introduction to Sinatra

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

Page 9: A Quick Introduction to Sinatra

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

Page 10: A Quick Introduction to Sinatra

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…

Page 11: A Quick Introduction to Sinatra

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

Page 12: A Quick Introduction to Sinatra

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)

Page 13: A Quick Introduction to Sinatra

Sinatra Can Be SASSY Tooget ’/stylesheet.css' do content_type 'text/css', :charset => 'utf-8' sass :stylesheetend

Page 14: A Quick Introduction to Sinatra

Roadieshelpers do def scotch(performer) “#{performer.name} deserves a scotch” endend

before do authenticate # run before each actionend

Page 15: A Quick Introduction to Sinatra

Configuration# run once, protect from reloading# can specify environment(s) too

configure :production do DataMapper.setup(:default, "db.sqlite3") # set :public, … # set :views, … end

Page 16: A Quick Introduction to Sinatra

Even Sinatra Ain’t Perfect.not_found do "Sorry, champ. That doesn’t exist."end

error do ”Oops! " + request.env["sinatra.error"].messageend

Page 17: A Quick Introduction to Sinatra

@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

Page 18: A Quick Introduction to Sinatra

Playing To A Bigger Audience Deploy with Apache and Passenger via

Rackup scripts

# example config.rurequire 'application'set :run, falseset :environment, :productionrun Sinatra::Application

Page 19: A Quick Introduction to Sinatra

Make Your Life Easier Use Sinatra Template

http://github.com/zapnap/sinatra-template

Or Sinatra Gen http://github.com/quirkey/sinatra-gen

Page 20: A Quick Introduction to Sinatra

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])