Subscribe Favorite

Sinatra: Classy web-development dressed in a DSL

Written on November 17, 2007 by ceefour

Sinatra is a new cool open-source DSL-driven web application framework!

This super-sexy DSL runs at lighting speed. It sits on top of Mongrel and was written to be thread-safe, sleek and tiny. And an entire web-application can be written and contained in one file (or a small collection of files)!

It’s super easy to use! Let’s create an app from scratch to demonstrate!

Install!

gem install sinatra -y

Use!

  • Create a file called lyrics.rb (or any name you like)
  • Add
      require 'rubygems'
    
      require 'sinatra'
  • Run (yes, with just ruby)
      % ruby lyrics.rb
    
      == Sinata has taken the stage on port 4567!
  • Take a moment and view the default page localhost:4567. Go ahead and bask in it‘s glory.
  • Notice:
    • It didn‘t create any page to show you that default page (just a cool thing to see, that‘s all)
    • There was nothing generated other than a log file
    • Sinatra is a really cool name for a web-framework that‘s a DSL
  • Modify lyrics.rb by adding:
      get '/' do
    
        'Hello World'
    
      end
  • Refresh (no need to restart Sinatra):
      http://localhost:4567
  • Modify again (then refresh):
  •   get '/' do
    
        <<-HTML
    
          <form action='/' method="POST">
    
            <input type="text" name="name" />
    
            <input type="submit" value="Say my name!" />
    
          </form>
    
        HTML
    
      end  post '/' do
    
        "Hello #{params[:name] || 'World'}!"
    
      end
  • Now you try: Use the Sinatra::Erb::EventContext or Sinatra::Haml::EventContext to do the same. Do them inline and as template files.
  • Learn more cool stuff: see Sinatra::Dsl
  • Create your own plugins!
    1. Create a ‘vendor’ directory in your app directory
    2. Lay it out like: myapp.rb : root
         |- vendor
      
                      | - plugin_name
      
                    | - init.rb  # load and hook here
      
                    | - lib
      
                          |- modules/classes here
    3. Use it in your app!

For further details visit:

If you enjoyed this post Subscribe to our feed

 

Trackbacks

(Trackback URL)

  • A Fresh Cup » Blog Archive » Double Shot #86

    December 28, 2007 at 8:47 am

    [...] Sinatra: Classy web-development dressed in a DSL - Yet another quick way to bang together those web applications. [...]

  • Home Builder Custom

    February 1, 2008 at 6:28 pm

    Controversy Continues Between Home Builders Association and Orlando Schools... Since the year 2000 when the Martinez Doctrine was promoted by the ...

close Reblog this comment
blog comments powered by Disqus