Ultra fast web development with sinatra
-
Upload
sergio-santos -
Category
Technology
-
view
2.673 -
download
1
description
Transcript of Ultra fast web development with sinatra
Ultra fast web development with
Classy hat required.
Ultra fast web development with
featuring
Pedro "Frank" GasparSérgio "Sinatra" Santos
What is Sinatra?
This is
Sinatra!
A small web framework
for server-side applications
in ruby
sinatrarb.com
Setup
WindowsOne-Click Installer – go to ruby-lang.org
Mac OSX(pre-installed, draw a hat instead)
Linux (Ubuntu)sudo apt-get install ruby rubygems
Install Sinatrasudo gem install sinatra
Install libs sudo gem install erb dm-core
dm-sqlite-adapter dm-migrations dm-serializer dm-validations serialport
Hello New York
hello.rb:
require 'sinatra'
get '/' do "New York, New York"end
ruby –rubygems hello.rb
go to http://localhost:4567
Ruby 101
Variables
hungry = trueanswer = 42cost = 0.99PI = 3.14name = "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']
Conditionsif grade >= 10 puts "Yey!"else puts "humpf"end
case minutes_late when 0..5 then puts "on time" when 5..15 then puts "fair" when 15..30 then puts "late" else puts "door closed"end
Cycles
while not hungry puts "work"end
puts "work" while not hungry
1.upto(10) { |n| puts n }
['ruby', 'python', 'php'].each do |language| puts "I can code " + languageend
Functions
def greetings(names) names.each do |first, last| puts "Hello " + first + " " + last endend
names = { 'Sérgio' => 'Santos', 'Pedro' => 'Gaspar' }greetings(names)
Controllers
get '/moon' do "Fly me to the moon…"end
post '/destroy-world' do "Boom"end
get '/hello/:name' do "Hello " + params[:name]end
Sessions
enable :sessions
get '/visit' do session[:visits] = 0 unless session[:visits] session[:visits] += 1 "You visited this page #{session[:visits]} times."end
Filters
before do puts request.ipend
get '/hello' do "Hi!" end
get '/bye' do "Bye!" end
after do puts "All done"end
Templates
Public folder
All files inside the folder 'public' are shared
Great for static files like javascript, css, images…
get '/hello/:name' do "Hello " + params[:name]end
get '/hello/:name' do @name = params[:name] erb :helloend
template :hello do "Hello <%= @name %>"end
get '/hello/:name' do @name = params[:name] erb :helloend
views/hello.erb:Hello <%= @name %>
get '/show' do @names = ['Sérgio', 'Pedro'] erb :showend
views/show.erb:<% if @names.empty? %> This place is empty.<% else %> We got: <% for name in @names %> <%= name %> <% end %><% end %>
views/layout.erb:<html> <head> <title>My Sinatra App</title> </head> <body> <%= yield %> </body></html>
Database
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.nowend
DataMapper.auto_upgrade!
Types
BooleanStringTextFloatIntegerDecimalDateTime, Date, Time
http://datamapper.org/docs/properties
Operations
person = Person.create(:name => 'Sérgio‘, :email => '[email protected]')
person = Person.newperson.name = 'Sérgio'person.email = '[email protected]'person.save
person.update(:name => 'Pedro')
person.destroy
http://datamapper.org/docs/create_and_destroy
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
Serializer
require 'dm-serializer'
Person.all.to_xmlPerson.all.to_jsonPerson.all.to_csvPerson.all.to_yaml
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 :emailend
http://datamapper.org/docs/validations
Validationsget '/' do erb :indexend
post '/registration' do @person = Person.create(:name => params['name'], :email => params['email']) if @person.saved? erb :thanks else erb :index endend
views/index:<p style="color: red;"> <% for error in @person.errors %> <%= error %><br/> <% end %></p>
Projects