Ultra fastwebdevelopmentwith<br />Classyhatrequired.<br />
Ultra fastwebdevelopmentwith<br />featuring<br />Pedro "Frank" Gaspar<br />Sérgio "Sinatra" Santos<br />
WhatisSinatra?<br />
ThisisSinatra!<br />
A smallwebframework<br />for server-sideapplications<br />inruby<br />sinatrarb.com<br />
Setup<br />
Windows<br />One-ClickInstaller – go to ruby-lang.org<br />Mac OSX<br />(pre-installed, draw a hatinstead)<br />Linux (Ubu...
InstallSinatra<br />sudogeminstallsinatra<br />Installlibs<br />sudogeminstallerbdm-core<br />dm-sqlite-adapterdm-migratio...
HelloNew York<br />
hello.rb:<br />require 'sinatra'<br />get '/' do<br />   "New York, New York"<br />end<br />
ruby–rubygemshello.rb<br />go to http://localhost:4567<br />
Ruby 101<br />
Variables<br />hungry = true<br />answer = 42<br />cost =  0.99<br />PI = 3.14<br />name = "Sérgio Santos"<br />fruit = ['...
Conditions<br />if grade >= 10<br />puts "Yey!"<br />else<br />puts "humpf"<br />end<br />case minutes_late<br />when 0..5...
Cycles<br />whilenothungry<br />puts "work"<br />end<br />puts "work" whilenothungry<br />1.upto(10)  {  |n|puts n  }<br /...
Functions<br />defgreetings(names)<br />names.each do |first, last|<br />puts "Hello " + first + " " + last<br />end<br />...
Controllers<br />
get '/moon' do<br />     "Fly me to themoon…"<br />end<br />post '/destroy-world' do<br />     "Boom"<br />end<br />get '/...
Sessions<br />enable :sessions<br />get '/visit' do<br />session[:visits] = 0 unlesssession[:visits] <br />session[:visits...
Filters<br />before do<br />putsrequest.ip<br />end<br />get '/hello' do "Hi!" end<br />get '/bye' do "Bye!" end<br />afte...
Templates<br />
Publicfolder<br />All files insidethefolder 'public' are shared<br />Great for static files like javascript, css, images…<...
get '/hello/:name' do<br />    "Hello " + params[:name]<br />end<br />
get '/hello/:name' do<br />     @name = params[:name]<br />erb :hello<br />end<br />template :hello do<br />      "Hello <...
get '/hello/:name' do<br />     @name = params[:name]<br />erb :hello<br />end<br />views/hello.erb:<br />Hello <%= @name ...
get '/show' do<br />     @names = ['Sérgio', 'Pedro']<br />erb :show<br />end<br />views/show.erb:<br /><% if @names.empty...
views/layout.erb:<br /><html><br />    <head><br />        <title>My Sinatra App</title><br />    </head><br />    <body><...
Database<br />
require 'sinatra'<br />require 'dm-core'<br />require 'dm-migrations'<br />DataMapper.setup(:default, "sqlite3://#{Dir.pwd...
Types<br />Boolean<br />String<br />Text<br />Float<br />Integer<br />Decimal<br />DateTime, Date, Time<br />http://datama...
Operations<br />person = Person.create(:name => 'Sérgio‘, :email => 'me@sergiosantos.info')<br />person = Person.new<br />...
Operations<br />Person.get(5)<br />Person.first( :name => 'Sérgio' )<br />Person.last<br />Person.all( :name.like => 'Sérg...
Serializer<br />require 'dm-serializer'<br />Person.all.to_xml<br />Person.all.to_json<br />Person.all.to_csv<br />Person....
Validations<br />require 'dm-validations'<br />class Person<br />    include DataMapper::Resource<br />    property :id,  ...
Validations<br />get '/' do<br />erb :index<br />end<br />post '/registration' do<br />    @person = Person.create(:name =...
Projects<br />
Upcoming SlideShare
Loading in …5
×

Ultra fast web development with sinatra

2,918 views
2,807 views

Published on

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

Published in: Technology, Design
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,918
On SlideShare
0
From Embeds
0
Number of Embeds
234
Actions
Shares
0
Downloads
27
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Ultra fast web development with sinatra

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

×