Repase la sección HTTP 31.5.
Vease el código de este ejemplo en GitHub
[~/Dropbox/src/ruby/sinatra/sinatra-simple(master)]$ cat app.rb
require 'sinatra/base'
class App < Sinatra::Base
get '/' do
"hello get!"
end
post '/' do
'hello post!'
end
put '/' do
'hello put!'
end
delete '/' do
'hello delete!'
end
get '/:name' do |name|
"hello #{name}!"
end
get '/:name/?:apelido1?' do |name, apellido|
"hello #{apellido}, #{name}!"
end
end
In Sinatra, a route is an HTTP method paired with a URL-matching pattern. Each route is associated with a block
Routes are matched in the order they are defined. The first route that matches the request is invoked.
Route patterns may include named parameters, accessible via the params hash:
get '/hello/:name' do
# matches "GET /hello/foo" and "GET /hello/bar"
# params[:name] is 'foo' or 'bar'
"Hello #{params[:name]}!"
end
You can also access named parameters via block parameters:
get '/:name' do |name|
"hello #{name}!"
end
Route patterns may also include splat (or wildcard) parameters, accessible via the params[:splat] array:
get '/say/*/to/*' do
# matches /say/hello/to/world
params[:splat] # => ["hello", "world"]
end
get '/download/*.*' do
# matches /download/path/to/file.xml
params[:splat] # => ["path/to/file", "xml"]
end
[~/sinatra/sinatra-simple(master)]$ cat config.ru require './app' run App
[~/sinatra/sinatra-simple(master)]$ cat Rakefile
task :default => :server
desc "run server"
task :server do
sh "rackup"
end
desc "make a get / request via curl"
task :get do
sh "curl -v localhost:9292"
end
desc "make a post / request via curl"
task :post do
sh "curl -X POST -v -d 'ignored data' localhost:9292"
end
desc "make a put / request via curl"
task :put do
sh "curl -X PUT -v localhost:9292"
end
desc "make a DELETE / request via curl"
task :delete do
sh "curl -X DELETE -v localhost:9292"
end
desc "make a get /name request via curl"
task :getname, :name do |t,h|
name = h[:name] or 'pepe'
sh "curl -v localhost:9292/#{name}"
end
desc "make a get /name/appellido request via curl"
task :getfullname, :name, :apellido do |t,h|
name = h[:name] or 'pepe'
apellido = h[:apellido] or 'rodriguez'
sh "curl -v localhost:9292/#{name}/#{apellido}"
end
task :html do
sh "kramdown README.md > README.html"
end
[~/sinatra/sinatra-simple(master)]$ rake server rackup >> Thin web server (v1.5.1 codename Straight Razor) >> Maximum connections set to 1024 >> Listening on 0.0.0.0:9292, CTRL+C to stop 127.0.0.1 - - [01/Jul/2013 20:25:16] "GET /juana HTTP/1.1" 200 12 0.0689
[~/Dropbox/src/ruby/sinatra/sinatra-simple(master)]$ rake getname[juana]
{:name=>"juana"}
curl -v localhost:9292/juana
* About to connect() to localhost port 9292 (#0)
* Trying ::1... Connection refused
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 9292 (#0)
> GET /juana HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8x zlib/1.2.5
> Host: localhost:9292
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/html;charset=utf-8
< Content-Length: 12
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< Connection: keep-alive
< Server: thin 1.5.1 codename Straight Razor
<
* Connection #0 to host localhost left intact
* Closing connection #0
hello juana!
[~/Dropbox/src/ruby/sinatra/sinatra-simple(master)]$ rake getfullname[Ana,Hernandez] curl -v localhost:9292/Ana/Hernandez * About to connect() to localhost port 9292 (#0) * Trying ::1... Connection refused * Trying 127.0.0.1... connected * Connected to localhost (127.0.0.1) port 9292 (#0) > GET /Ana/Hernandez HTTP/1.1 > User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8x zlib/1.2.5 > Host: localhost:9292 > Accept: */* > < HTTP/1.1 200 OK < Content-Type: text/html;charset=utf-8 < Content-Length: 21 < X-XSS-Protection: 1; mode=block < X-Content-Type-Options: nosniff < X-Frame-Options: SAMEORIGIN < Connection: keep-alive < Server: thin 1.5.1 codename Straight Razor < * Connection #0 to host localhost left intact * Closing connection #0 hello Hernandez, Ana!