A Quick Introduction to Sinatra

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Favorite

    A Quick Introduction to Sinatra - Presentation Transcript

    1. Sinatra A Classy Web Development DSL Nick Plante [ nap@zerosum.org ] NH.rb - 03.19.08
    2. 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
    3.  
    4.  
    5. Let’s Start The Show
      • gem install sinatra
      • ruby sinatra-application.rb
    6. This Is How We Roll
      • # sample.rb
      • require 'rubygems'
      • require 'sinatra'
      • get '/' do
      • "Raise your glass!"
      • end
    7. 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' { … }
    8. 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
    9. 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!'
    10. 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…
    11. 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
    12. 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)
    13. Sinatra Can Be SASSY Too
      • get ’/stylesheet.css' do
      • content_type 'text/css', :charset => 'utf-8'
      • sass :stylesheet
      • end
    14. Roadies
      • helpers do
      • def scotch(performer)
      • “ #{performer.name} deserves a scotch”
      • end
      • end
      • before do
      • authenticate # run before each action
      • end
    15. Configuration
      • # run once, protect from reloading
      • # can specify environment(s) too
      • configure :production do
      • DataMapper.setup(:default, &quot;db.sqlite3&quot;)
      • # set :public, …
      • # set :views, …
      • end
    16. Even Sinatra Ain’t Perfect.
      • not_found do
      • &quot;Sorry, champ. That doesn’t exist.&quot;
      • end
      • error do
      • ” Oops! &quot; + request.env[&quot;sinatra.error&quot;].message
      • end
    17. @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
    18. 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
    19. Make Your Life Easier
      • Use Sinatra Template
        • http://github.com/zapnap/sinatra-template
      • Or Sinatra Gen
        • http://github.com/quirkey/sinatra-gen
    20. 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)
    SlideShare Zeitgeist 2009

    + guestbe060guestbe060 Nominate

    custom

    1097 views, 1 favs, 0 embeds more stats

    A quick introduction to the Sintra web framework

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1097
      • 1097 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 17
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories