Sinatra

Introduction

DSL for creating web apps with ruby with minimal effort.
Minimalist web framework.
Whole app can be containened in one file.
RESTful HTTP actions, not MVC… its all MVC in one file.

Benefits

  • Minimalist
  • Low memory
  • Fast

    Preparation

    gem install [--no-rdoc] [--no-ri] sinatra
    gem install [--no-rdoc] [--no-ri] shotgun ## server, check changes in realtime just by reloading browser
    

    Handlers

  • text
  • splats (greedy)
  • regex

    Filters

    One-file app

    ```
    require 'rubygems'
    require 'sinatra'
    get '/' do #|
    "Hello World" #| handler, (like controller in rails)
    end #|
    get '/hello/:name' do
    "Hello #{params[:name]}!!!"
    end
    get '/this//is/' do
    params[:splat].join(' ')
    end
    get '/basic/template' do
    erb :quote
    end
    get '/advanced/template' do
    @title = "Rumours listing"
    erb :rumour
    end
    END # Ruby macro, everythink afterward handled as text
    @@ layout # Automaticly used, based on "layout" keyword

@@ quote # Sinatra template
Something clever by someone even cleverer
@@ rumour
Something stupid by someone even stupider

`shotgun simple.rb` » start server 
## More complex app
#### Folder sturcture

app.rb
views/
hello.erb
layout.erb

Sinatra is looking for template: 
1\. in same file 
2\. on views/ directory
## Complex app
No strucutre generating, like rails. Each app is different.
Or use [http://monkrb.com/](http://monkrb.com/)

app.rb
config/
lib/
public/
tmp/
views/

### Persistance
SQLite + DataMapper 
`gem install datamapper`
``` shell
addressable-2.2.8
dm-core-1.2.0
dm-aggregates-1.2.0
dm-constraints-1.2.0
dm-migrations-1.2.0
dm-transactions-1.2.0
fastercsv-1.5.5
json_pure-1.7.5
dm-serializer-1.2.2
dm-timestamps-1.2.0
dm-validations-1.2.0
uuidtools-2.1.3
dm-types-1.2.2
datamapper-1.2.0

gem install do_sqlite3

Successfully installed data_objects-0.10.10
Successfully installed do_sqlite3-0.10.10

gem install dm-sqlite-adapter

dm-do-adapter-1.2.0
dm-sqlite-adapter-1.2.0
Checking

sqlite3 app.db
.schema table_name