Hack hack hack...

An open journal-- some of it written for you, but most of it is for me.

Advanced Sinatra


  • Sinatra is a DSL for creating a web

  • Sinatra is library, not a framework (rails is a framework)

  • difference between lib and framework

  • inversion of control by Martin Fowler

    • Sinatra supports inline templates
    • variables are passed in via @instance variables
    • Filters
    • Helpers: takes a block and then anything defined are available in the templates
    • Configuration: looks at the rack env variable and executes code based on the env
    • cache docs
    • By default don’t have sessions. Need to config sessions.
    • Testing -> a lot of the tests that come with rails aren’t in sinatra

      • test with rack-test
    • classic app ->

      • all the routes are in one file, it is straight Ruby code
    • Modular app ->

      • don’t want to pollute the Object namespace
      • combinging multiple Sinatra apps in a single process
      • use Sinatra app as middleware
    • Supports extensions

      • could be helpers, routes, configurations pacakged into a Gem and used on other apps
    • Registered

      • when the app is registered is has a method (def self.registered(app)) that allows config settings
    • extensions

Sinatra vs. Rails

  • when to use Sinatra…
    • Thick client web applications for performance or heavily visualizations

      • majority of the logic is on the client, not on the server
      • sinatra serves things really fast
    • API servers for iOS/Android/Javascript Apps

    • HTML5 Mobile Apps (jq touch or jquery mobile)



Sinatra integrated into Rails

  • it is all Rack under the hood so you use mount in the route file and then all the urls defined in the sinatra app are available in the rails app

Sinatra on Heroku

  • heroku requires sinatra to have a gem file as well as a config.ru file
config.ru example
require './myapp'

run Sinatra::Application
gemfile example
source "https://rubygems.org"
gem 'sinatra', '1.1.0'