Ultra fast web development with sinatra
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Ultra fast web development with sinatra

  • 3,097 views
Uploaded on

An introduction to the development of simple web applications with the Sinatra framework.

An introduction to the development of simple web applications with the Sinatra framework.

More in: Technology , Design
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
3,097
On Slideshare
2,867
From Embeds
230
Number of Embeds
5

Actions

Shares
Downloads
26
Comments
0
Likes
7

Embeds 230

http://sergiosantos.info 224
http://feeds.feedburner.com 3
http://www.netvibes.com 1
http://translate.googleusercontent.com 1
http://paper.li 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Ultra fastwebdevelopmentwith
    Classyhatrequired.
  • 2. Ultra fastwebdevelopmentwith
    featuring
    Pedro "Frank" Gaspar
    Sérgio "Sinatra" Santos
  • 3. WhatisSinatra?
  • 4. ThisisSinatra!
  • 5. A smallwebframework
    for server-sideapplications
    inruby
    sinatrarb.com
  • 6. Setup
  • 7. Windows
    One-ClickInstaller – go to ruby-lang.org
    Mac OSX
    (pre-installed, draw a hatinstead)
    Linux (Ubuntu)
    sudoapt-getinstallrubyrubygems
  • 8. InstallSinatra
    sudogeminstallsinatra
    Installlibs
    sudogeminstallerbdm-core
    dm-sqlite-adapterdm-migrations
    dm-serializerdm-validationsserialport
  • 9. HelloNew York
  • 10. hello.rb:
    require 'sinatra'
    get '/' do
    "New York, New York"
    end
  • 11. ruby–rubygemshello.rb
    go to http://localhost:4567
  • 12. Ruby 101
  • 13. Variables
    hungry = true
    answer = 42
    cost = 0.99
    PI = 3.14
    name = "Sérgio Santos"
    fruit = ['apple', 'banana', 'grape'] # fruit[1] -> 'banana'
    mix = ['door', 37, 15.2, false]
    names = { 'Sérgio' => 'Santos', 'Pedro' => 'Gaspar' }
    names['Sérgio']
  • 14. Conditions
    if grade >= 10
    puts "Yey!"
    else
    puts "humpf"
    end
    case minutes_late
    when 0..5 thenputs "ontime"
    when 5..15 thenputs "fair"
    when 15..30 thenputs "late"
    elseputs "doorclosed"
    end
  • 15. Cycles
    whilenothungry
    puts "work"
    end
    puts "work" whilenothungry
    1.upto(10) { |n|puts n }
    ['ruby', 'python', 'php'].each do |language|
    puts "I cancode " + language
    end
  • 16. Functions
    defgreetings(names)
    names.each do |first, last|
    puts "Hello " + first + " " + last
    end
    end
    names = { 'Sérgio' => 'Santos', 'Pedro' => 'Gaspar' }
    greetings(names)
  • 17. Controllers
  • 18. get '/moon' do
    "Fly me to themoon…"
    end
    post '/destroy-world' do
    "Boom"
    end
    get '/hello/:name' do
    "Hello " + params[:name]
    end
  • 19. Sessions
    enable :sessions
    get '/visit' do
    session[:visits] = 0 unlesssession[:visits]
    session[:visits] += 1
    "Youvisitedthispage #{session[:visits]} times."
    end
  • 20. Filters
    before do
    putsrequest.ip
    end
    get '/hello' do "Hi!" end
    get '/bye' do "Bye!" end
    after do
    puts "Alldone"
    end
  • 21. Templates
  • 22. Publicfolder
    All files insidethefolder 'public' are shared
    Great for static files like javascript, css, images…
  • 23. get '/hello/:name' do
    "Hello " + params[:name]
    end
  • 24. get '/hello/:name' do
    @name = params[:name]
    erb :hello
    end
    template :hello do
    "Hello <%= @name %>"
    end
  • 25. get '/hello/:name' do
    @name = params[:name]
    erb :hello
    end
    views/hello.erb:
    Hello <%= @name %>
  • 26. get '/show' do
    @names = ['Sérgio', 'Pedro']
    erb :show
    end
    views/show.erb:
    <% if @names.empty? %>
    This place is empty.
    <% else %>
    We got:
    <% for name in @names %> <%= name %> <% end %>
    <% end %>
  • 27. views/layout.erb:
    <html>
    <head>
    <title>My Sinatra App</title>
    </head>
    <body>
    <%= yield %>
    </body>
    </html>
  • 28. Database
  • 29. require 'sinatra'
    require 'dm-core'
    require 'dm-migrations'
    DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/event.sqlite3")
    class Person
    include DataMapper::Resource
    property :id, Serial
    property :name, String
    property :email, String
    property :date, Time, :default => Time.now
    end
    DataMapper.auto_upgrade!
  • 30. Types
    Boolean
    String
    Text
    Float
    Integer
    Decimal
    DateTime, Date, Time
    http://datamapper.org/docs/properties
  • 31. Operations
    person = Person.create(:name => 'Sérgio‘, :email => 'me@sergiosantos.info')
    person = Person.new
    person.name = 'Sérgio'
    person.email = 'me@sergiosantos.info'
    person.save
    person.update(:name => 'Pedro')
    person.destroy
    http://datamapper.org/docs/create_and_destroy
  • 32. Operations
    Person.get(5)
    Person.first( :name => 'Sérgio' )
    Person.last
    Person.all( :name.like => 'Sérgio' )
    Person.all( :date.gt => Time.now – 1 * 60 * 60 ) # Last hour
    http://datamapper.org/docs/find
  • 33. Serializer
    require 'dm-serializer'
    Person.all.to_xml
    Person.all.to_json
    Person.all.to_csv
    Person.all.to_yaml
  • 34. Validations
    require 'dm-validations'
    class Person
    include DataMapper::Resource
    property :id, Serial
    property :name, String
    property :email, String
    property :date, Time, :default => Time.now
    validates_length_of :name, :within => 3..100
    validates_uniqueness_of :email
    end
    http://datamapper.org/docs/validations
  • 35. Validations
    get '/' do
    erb :index
    end
    post '/registration' do
    @person = Person.create(:name => params['name'], :email => params['email'])
    if @person.saved?
    erb :thanks
    else
    erb :index
    end
    end
    views/index:
    <p style="color: red;">
    <% for error in @person.errors %>
    <%= error %><br/>
    <% end %>
    </p>
  • 36. Projects