SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
3.
About me
I’m a software developer in
CodicePlastico.
I write Web applications in C# with
ASP.NET MVC. I’m a member of
<WEBdeBS/> a local web
community.
...and I’m a wannabe Ruby
developer ;-)
4.
Agenda
‣ Philosophy.
Sweet and easy.
‣ REST.
Yet another boring introduction.
‣ Hello world.
Show some magic.
‣ Features.
Amazing!
‣ Inside.
How it works?
‣ Q&A.
...and thanks.
9.
Hello world
~$ gem install sinatra
~$ vi app.rb
10.
Hello world
~$ gem install sinatra
~$ vi app.rb
require 'sinatra'
get '/' do
'hello world!'
end
11.
Hello world
~$ gem install sinatra
~$ vi app.rb
require 'sinatra'
get '/' do
'hello world!'
end
~$ ruby app.rb
12.
Is it really so easy?
‣ Yep! (special thanks to Ruby
sweeties)
- get is a just a method (more later)
- the route is the parameter
- the block is what should be
executed
- the block returns the result
- the server is built in (based on Rack)
13.
Let’s go REST
What? Yet another REST introduction?
17.
Verbs
‣ Support for all verbs
- Get, Post, Put, Delete, Head, Options,
Patch
‣ They are just methods
18.
Routing
‣ No need of route files or route
maps
‣ The verb method takes the route
as parameter
19.
Routing
get ( ‘/todos’ ) {...}
get ( ‘/todo/:id’ ) {...}
get ( ‘/*’ ) {...}
20.
Routing Conditions
get '/foo', :agent => /Mozilla/(d.d)s
w?/ do
"You're using Mozilla version
#{params[:agent][0]}"
end
get '/foo' do
# Matches non-Mozilla browsers
end
21.
Routing Custom
set(:prob) { |v| condition { rand <= v } }
get '/win_a_car', :prob => 0.1 do
"You won!"
end
get '/win_a_car' {"Sorry, you lost."}
22.
Xml or Json
‣ You can set the content_type to
whatever you need
content_type :json
content_type :xml
23.
Status codes
‣ Set the HTTP status code is
status 200
status 403
24.
Http Headers
‣ You can add your own headers
headers "X-My-Value" => "this is my
header"
26.
Cache
‣ You can set your cache information
using the headers method
headers "Cache-Control" => "public,
must-revalidate, max-age=3600",
"Expires" => Time.at(Time.now.to_i + (60
27.
Cache
‣ Or better, you can use the expires
expires 3600, :public
38.
Modular Apps
Multi file
Subclassing Sinatra::Base
Distributed as library
39.
The code
What happen when a request arrives?
get (‘/hello’) do { ‘hello world’ }
40.
Where do get come from?
require "sinatra"
outer_self = self
get '/' do
"outer self: #{outer_self}, inner self: #{self}"
end
outer self: main, inner self: #<Sinatra::Application:
closures in ruby are “scope gate”
52.
Where do get come from?
‣ get is defined twice:
– Once in Sinatra::Delegator a mixin
extending Object
– Once in Sinatra::Application
‣ The Delegator implementation simply
delegate to Application
‣ That’s why we have get/post/...
methods on main
53.
How the block is invoked?
‣ The route! method finds the correct
route
def route_eval
throw :halt, yield
end
‣ throw is used to go back to the invoker
54.
In base.rb
‣ The invoker
def invoke
res = catch(:halt) { yield }
# .... other code...
end