ultra fast web development with sinatra

36
Ultra fast web development with Classy hat required.

Upload: sergio-santos

Post on 18-May-2015

2.671 views

Category:

Technology


1 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Ultra fast web development with sinatra

Ultra fast web development with

Classy hat required.

Page 2: Ultra fast web development with sinatra

Ultra fast web development with

featuring

Pedro "Frank" GasparSérgio "Sinatra" Santos

Page 3: Ultra fast web development with sinatra

What is Sinatra?

Page 4: Ultra fast web development with sinatra

This is

Sinatra!

Page 5: Ultra fast web development with sinatra

A small web framework

for server-side applications

in ruby

sinatrarb.com

Page 6: Ultra fast web development with sinatra

Setup

Page 7: Ultra fast web development with sinatra

WindowsOne-Click Installer – go to ruby-lang.org

Mac OSX(pre-installed, draw a hat instead)

Linux (Ubuntu)sudo apt-get install ruby rubygems

Page 8: Ultra fast web development with sinatra

Install Sinatrasudo gem install sinatra

Install libs sudo gem install erb dm-core

dm-sqlite-adapter dm-migrations dm-serializer dm-validations serialport

Page 9: Ultra fast web development with sinatra

Hello New York

Page 10: Ultra fast web development with sinatra

hello.rb:

require 'sinatra'

get '/' do "New York, New York"end

Page 11: Ultra fast web development with sinatra

ruby –rubygems hello.rb

go to http://localhost:4567

Page 12: Ultra fast web development with sinatra

Ruby 101

Page 13: Ultra fast web development with sinatra

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']

Page 14: Ultra fast web development with sinatra

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

Page 15: Ultra fast web development with sinatra

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

Page 16: Ultra fast web development with sinatra

Functions

def greetings(names) names.each do |first, last| puts "Hello " + first + " " + last endend

names = { 'Sérgio' => 'Santos', 'Pedro' => 'Gaspar' }greetings(names)

Page 17: Ultra fast web development with sinatra

Controllers

Page 18: Ultra fast web development with sinatra

get '/moon' do "Fly me to the moon…"end

post '/destroy-world' do "Boom"end

get '/hello/:name' do "Hello " + params[:name]end

Page 19: Ultra fast web development with sinatra

Sessions

enable :sessions

get '/visit' do session[:visits] = 0 unless session[:visits] session[:visits] += 1 "You visited this page #{session[:visits]} times."end

Page 20: Ultra fast web development with sinatra

Filters

before do puts request.ipend

get '/hello' do "Hi!" end

get '/bye' do "Bye!" end

after do puts "All done"end

Page 21: Ultra fast web development with sinatra

Templates

Page 22: Ultra fast web development with sinatra

Public folder

All files inside the folder 'public' are shared

Great for static files like javascript, css, images…

Page 23: Ultra fast web development with sinatra

get '/hello/:name' do "Hello " + params[:name]end

Page 24: Ultra fast web development with sinatra

get '/hello/:name' do @name = params[:name] erb :helloend

template :hello do "Hello <%= @name %>"end

Page 25: Ultra fast web development with sinatra

get '/hello/:name' do @name = params[:name] erb :helloend

views/hello.erb:Hello <%= @name %>

Page 26: Ultra fast web development with sinatra

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 %>

Page 27: Ultra fast web development with sinatra

views/layout.erb:<html> <head> <title>My Sinatra App</title> </head> <body> <%= yield %> </body></html>

Page 28: Ultra fast web development with sinatra

Database

Page 29: Ultra fast web development with sinatra

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!

Page 30: Ultra fast web development with sinatra

Types

BooleanStringTextFloatIntegerDecimalDateTime, Date, Time

http://datamapper.org/docs/properties

Page 31: Ultra fast web development with sinatra

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

Page 32: Ultra fast web development with sinatra

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

Page 33: Ultra fast web development with sinatra

Serializer

require 'dm-serializer'

Person.all.to_xmlPerson.all.to_jsonPerson.all.to_csvPerson.all.to_yaml

Page 34: Ultra fast web development with sinatra

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

Page 35: Ultra fast web development with sinatra

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>

Page 36: Ultra fast web development with sinatra

Projects