Fly your HTTP to the moon

Edmonton Rubyists

March 2, 2010

Sinatra

...it's awesome

sinatra

sinatra-web

Simple → Fast → Easy → Fun

Obligatory 'Hello, World!'

require 'rubygems'
require 'sinatra'

get '/' do
  'Hello, World!'
end
$ ruby app.rb
== Sinatra/1.0.a has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.2.5 codename This Is Not A Web Server)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:4567, CTRL+C to stop

> HTTP Methods

Static Files and Views

Routing and Parameters

Configuration and Error Handling

Helpers and Filters

Middleware and Testing

Gang Vocals

get '/' do
  # Also creates a HEAD handler
  "Gettin' somethin'..."
end

post '/' do
  "Creatin' somethin'..."
end
put '/' do
  "Updatin' somethin'..."
end

delete '/' do
  "Deletin' somethin'..."
end

HTTP Methods

> Static Files and Views

Routing and Parameters

Configuration and Error Handling

Helpers and Filters

Middleware and Testing

Gang Vocals

require 'haml'

get '/' do
  haml(:index) # renders 'views/index.haml'
end

get '/style.css' do
  sass(:style) # renders 'views/style.sass'
end
require 'erb'

get '/' do
  erb(:index)
end
require 'erubis'

get '/' do
  erubis(:index)
end
require 'builder'

get '/' do
  builder(:index)
end

HTTP Methods

Static Files and Views

> Routing and Parameters

Configuration and Error Handling

Helpers and Filters

Middleware and Testing

Gang Vocals

get '/archive/:year/:month/:day/:slug' do |year, month, day, slug|
   @post = Post.find(...)
   haml(:post, :locals => { :post => @post })
end
get %r|/(\d{4})/(\d{2})/(\d{2})/([\w\d\-+ ]+)| do |year, month, day, slug|
   @post = Post.find(...)
   haml(:post, :locals => { :post => @post })
end

HTTP Methods

Static Files and Views

Routing and Parameters

> Configuration and Error Handling

Helpers and Filters

Middleware and Testing

Gang Vocals

set(:cache_time, 600) # we'll access this later
disable(:caching) # same as 'set(:caching, false)'

not_found do
  haml(:not_found)
end

configure :production do
  error do
    'My bad...'
  end

  enable(:caching)
end

HTTP Methods

Static Files and Views

Routing and Parameters

Configuration and Error Handling

> Helpers and Filters

Middleware and Testing

Gang Vocals

helpers do
  def gravatar_url(email, size = 120)
    digest = Digest::MD5.hexdigest(email)
    "http://www.gravatar.com/avatar/#{digest}.png?s=#{size}"
  end

  include BrassSectionHelpers
end
configure :production do
  before do
    expires(options.cache_time)
  end
end

HTTP Methods

Static Files and Views

Routing and Parameters

Configuration and Error Handling

Helpers and Filters

> Middleware and Testing

Gang Vocals

use Rack::Etag

Request → Etag Middleware → Application

require 'app'
require 'rack/test'

class MyAppTest < Test::Unit::TestCase
  include Rack::Test::Methods

  def app
    Sinatra::Application
  end

  def test_hello_world
    get '/'
    assert_equal 'Hello World!', last_response.body
  end
end
require File.dirname(__FILE__) + '/spec_helper'

describe 'My App' do
  include Rack::Test::Methods

  def app
    @app ||= Sinatra::Application
  end

  it 'should say hello world' do
    get '/'
    last_response.should be_ok
    last_response.body.should == 'Hello, World!'
  end
end

HTTP Methods

Static Files and Views

Routing and Parameters

Configuration and Error Handling

Helpers and Filters

Middleware and Testing

> Gang Vocals

Questions?

Comments?

...Cookies?

Thanks!


Edmonton Rubyists

Github

Heroku

Sinatra Team