0
Sinatra A Classy Web Development DSL Nick Plante [ nap@zerosum.org ] NH.rb - 03.19.08
Why? <ul><li>Simple </li></ul><ul><ul><li>Don’t always need the extras. </li></ul></ul><ul><li>Lightweight </li></ul><ul><...
 
 
Let’s Start The Show <ul><li>gem install sinatra </li></ul><ul><li>ruby sinatra-application.rb </li></ul>
This Is How We Roll <ul><li># sample.rb </li></ul><ul><li>require 'rubygems' </li></ul><ul><li>require  'sinatra' </li></u...
It’s RESTful, Baby. <ul><li>Native support for standard REST actions; GET, PUT, POST, DELETE </li></ul><ul><li>get '/gigs'...
Inline Routes and Params <ul><li>get  '/gigs/:id'  do </li></ul><ul><li>@gig = Gig.get( params[:id] ) </li></ul><ul><li>en...
Route Processing <ul><li>Routes are processed in order </li></ul><ul><li>You can punt to the next matching rule by using t...
This Sinatra Isn’t Pushy <ul><li>Choose your favorite template engine </li></ul><ul><ul><li>ERb, Haml, Builder </li></ul><...
The Way You Look Tonight <ul><li>Sinatra includes view helpers for Haml, Erb, Builder… </li></ul><ul><ul><li>If a layout e...
Or Would You Prefer Inline? <ul><li>get '/gigs/:id' do </li></ul><ul><li>@gig = Gig.get(params[:id]) </li></ul><ul><li>erb...
Sinatra Can Be SASSY Too <ul><li>get ’/stylesheet.css' do </li></ul><ul><li>content_type  'text/css', :charset => 'utf-8' ...
Roadies <ul><li>helpers  do </li></ul><ul><li>def scotch(performer) </li></ul><ul><li>“ #{performer.name} deserves a scotc...
Configuration <ul><li># run once, protect from reloading </li></ul><ul><li># can specify environment(s) too </li></ul><ul>...
Even Sinatra Ain’t Perfect. <ul><li>not_found  do </li></ul><ul><li>&quot;Sorry, champ. That doesn’t exist.&quot; </li></u...
@the_ladies.should swoon <ul><li>require 'spec' </li></ul><ul><li>require 'spec/interop/test' </li></ul><ul><li>require 's...
Playing To A Bigger Audience <ul><li>Deploy with Apache and Passenger via Rackup scripts </li></ul><ul><ul><ul><li># examp...
Make Your Life Easier <ul><li>Use Sinatra Template </li></ul><ul><ul><li>http://github.com/zapnap/sinatra-template </li></...
Classy Resources <ul><li>http://www.sinatrarb.com </li></ul><ul><li>http://github.com/sinatra </li></ul><ul><li>http://blo...
Upcoming SlideShare
Loading in...5
×

A Quick Introduction to Sinatra

3,407

Published on

A quick introduction to the Sintra web framework

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,407
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
39
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "A Quick Introduction to Sinatra"

  1. 1. Sinatra A Classy Web Development DSL Nick Plante [ nap@zerosum.org ] NH.rb - 03.19.08
  2. 2. Why? <ul><li>Simple </li></ul><ul><ul><li>Don’t always need the extras. </li></ul></ul><ul><li>Lightweight </li></ul><ul><ul><li>Single-file applications? Yup. </li></ul></ul><ul><li>Intuitive </li></ul><ul><ul><li>A truly minimalist DSL built on top of Rack </li></ul></ul><ul><li>Most importantly: Classy </li></ul>
  3. 5. Let’s Start The Show <ul><li>gem install sinatra </li></ul><ul><li>ruby sinatra-application.rb </li></ul>
  4. 6. This Is How We Roll <ul><li># sample.rb </li></ul><ul><li>require 'rubygems' </li></ul><ul><li>require 'sinatra' </li></ul><ul><li>get '/' do </li></ul><ul><li>&quot;Raise your glass!&quot; </li></ul><ul><li>end </li></ul>
  5. 7. It’s RESTful, Baby. <ul><li>Native support for standard REST actions; GET, PUT, POST, DELETE </li></ul><ul><li>get '/gigs' { … } </li></ul><ul><li>get '/gigs/new' { … } </li></ul><ul><li>post '/gigs' { … } </li></ul><ul><li>get '/gigs/:id' { … } </li></ul><ul><li>put '/gigs/:id' { … } </li></ul><ul><li>delete '/gigs/:id' { … } </li></ul>
  6. 8. Inline Routes and Params <ul><li>get '/gigs/:id' do </li></ul><ul><li>@gig = Gig.get( params[:id] ) </li></ul><ul><li>end </li></ul><ul><li>get '/music/*.*' do </li></ul><ul><li># matches /music/path/to/my.mp3 </li></ul><ul><li>params['splat'] # => [’path/to/my', ’mp3’] </li></ul><ul><li>end </li></ul><ul><li># also: regex matchers, user agent matching, etc </li></ul>
  7. 9. Route Processing <ul><li>Routes are processed in order </li></ul><ul><li>You can punt to the next matching rule by using the pass directive </li></ul><ul><ul><li>pass unless params[:who] == 'frank' </li></ul></ul><ul><li>You can also halt execution of a method using the halt directive </li></ul><ul><ul><li>halt 401, 'gtfo!' </li></ul></ul>
  8. 10. This Sinatra Isn’t Pushy <ul><li>Choose your favorite template engine </li></ul><ul><ul><li>ERb, Haml, Builder </li></ul></ul><ul><li>Choose your favorite ORM </li></ul><ul><ul><li>ActiveRecord, DataMapper, Sequel </li></ul></ul><ul><li>Choose your favorite JavaScript library </li></ul><ul><ul><li>jQuery, Prototype, YUI, Dojo </li></ul></ul><ul><li>Choose your testing tools </li></ul><ul><ul><li>Test::Unit, RSpec, Bacon, Shoulda… </li></ul></ul>
  9. 11. The Way You Look Tonight <ul><li>Sinatra includes view helpers for Haml, Erb, Builder… </li></ul><ul><ul><li>If a layout exists, it will auto-render it too (unless :layout => false is specified) </li></ul></ul><ul><li>get '/gigs' do </li></ul><ul><li> haml :main # renders main.haml </li></ul><ul><li> # erb :main </li></ul><ul><li> # builder :main </li></ul><ul><li>end </li></ul>
  10. 12. Or Would You Prefer Inline? <ul><li>get '/gigs/:id' do </li></ul><ul><li>@gig = Gig.get(params[:id]) </li></ul><ul><li>erb ’Playing at <%= @gig.venue.name %>' </li></ul><ul><li>end </li></ul><ul><li># NOTE: can also use in-file templates </li></ul><ul><li># (located at end of source file) </li></ul>
  11. 13. Sinatra Can Be SASSY Too <ul><li>get ’/stylesheet.css' do </li></ul><ul><li>content_type 'text/css', :charset => 'utf-8' </li></ul><ul><li>sass :stylesheet </li></ul><ul><li>end </li></ul>
  12. 14. Roadies <ul><li>helpers do </li></ul><ul><li>def scotch(performer) </li></ul><ul><li>“ #{performer.name} deserves a scotch” </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>before do </li></ul><ul><li>authenticate # run before each action </li></ul><ul><li>end </li></ul>
  13. 15. Configuration <ul><li># run once, protect from reloading </li></ul><ul><li># can specify environment(s) too </li></ul><ul><li>configure :production do </li></ul><ul><li>DataMapper.setup(:default, &quot;db.sqlite3&quot;) </li></ul><ul><li># set :public, … </li></ul><ul><li># set :views, … </li></ul><ul><li>end </li></ul>
  14. 16. Even Sinatra Ain’t Perfect. <ul><li>not_found do </li></ul><ul><li>&quot;Sorry, champ. That doesn’t exist.&quot; </li></ul><ul><li>end </li></ul><ul><li>error do </li></ul><ul><li>” Oops! &quot; + request.env[&quot;sinatra.error&quot;].message </li></ul><ul><li>end </li></ul>
  15. 17. @the_ladies.should swoon <ul><li>require 'spec' </li></ul><ul><li>require 'spec/interop/test' </li></ul><ul><li>require 'sinatra/test' </li></ul><ul><li>require 'application' </li></ul><ul><li>describe 'main application' do </li></ul><ul><li>include Sinatra::Test </li></ul><ul><li>specify 'should show the default index page' do </li></ul><ul><li>get '/' </li></ul><ul><li>@response.should be_ok </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>
  16. 18. Playing To A Bigger Audience <ul><li>Deploy with Apache and Passenger via Rackup scripts </li></ul><ul><ul><ul><li># example config.ru </li></ul></ul></ul><ul><li>require 'application' </li></ul><ul><li>set :run, false </li></ul><ul><li>set :environment, :production </li></ul><ul><li>run Sinatra::Application </li></ul>
  17. 19. Make Your Life Easier <ul><li>Use Sinatra Template </li></ul><ul><ul><li>http://github.com/zapnap/sinatra-template </li></ul></ul><ul><li>Or Sinatra Gen </li></ul><ul><ul><li>http://github.com/quirkey/sinatra-gen </li></ul></ul>
  18. 20. Classy Resources <ul><li>http://www.sinatrarb.com </li></ul><ul><li>http://github.com/sinatra </li></ul><ul><li>http://blog.zerosum.org </li></ul><ul><li>http://github.com/zapnap/retweet </li></ul><ul><li>http://github.com/nhruby/pickawinner </li></ul>Thanks! ..nap (nap@zerosum.org)
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×