require 'sinatra'
), then this class is Sinatra::Application, otherwise it is the subclass you created explicitly
get
or before
,
but you cannot access the request
or session
objects,
as there is only a single application class for all requests
Options created via set
are methods at class level:
class MyApp < Sinatra::Base # Hey, I'm in the application scope! set :foo, 42 foo # => 42 get '/foo' do # Hey, I'm no longer in the application scope! end end
You have the application scope binding inside:
helpers
set
Sinatra.new
You can reach the scope object (the class) like this:
configure { |c| ... }
)
settings
from within the request scope
For every incoming request, a new instance of your application class is created and all handler blocks run in that scope
request
and session
objects or
erb
or haml
request
scope via the settings
helper:
[~/sinatra/sinatra-scope]$ cat app.rb require 'sinatra' class MyApp < Sinatra::Base # Hey, I'm in the application scope! get '/define_route/:name' do # Request scope for '/define_route/:name' @value = 42 puts "Inside /define_route/:name @value = #{@value}" puts self.class settings.get("/#{params[:name]}") do # Request scope for "/#{params[:name]}" puts "@value = <#{@value}>" "Inside defined route #{params[:name]}" end "Route #{params[:name]} defined!" end run! if __FILE__ == $0 end
[~/sinatra/sinatra-scope]$ ruby app.rb == Sinatra/1.4.4 has taken the stage on 4567 for development with backup from Thin Thin web server (v1.6.1 codename Death Proof) Maximum connections set to 1024 Listening on localhost:4567, CTRL+C to stop Inside /define_route/:name @value = 42 MyApp @value = <>
[~/sinatra/sinatra-scope]$ curl -v 'http://localhost:4567/define_route/juan' * Adding handle: conn: 0x7fbacb004000 * Adding handle: send: 0 * Adding handle: recv: 0 * Curl_addHandleToPipeline: length: 1 * - Conn 0 (0x7fbacb004000) send_pipe: 1, recv_pipe: 0 * About to connect() to localhost port 4567 (#0) * Trying 127.0.0.1... * Connected to localhost (127.0.0.1) port 4567 (#0) > GET /define_route/juan HTTP/1.1 > User-Agent: curl/7.30.0 > Host: localhost:4567 > Accept: */* > < HTTP/1.1 200 OK < Content-Type: text/html;charset=utf-8 < Content-Length: 19 < X-XSS-Protection: 1; mode=block < X-Content-Type-Options: nosniff < X-Frame-Options: SAMEORIGIN < Connection: keep-alive * Server thin 1.6.1 codename Death Proof is not blacklisted < Server: thin 1.6.1 codename Death Proof < * Connection #0 to host localhost left intact Route juan defined! [~/sinatra/sinatra-scope]$
[~/sinatra/sinatra-scope]$ curl -v 'http://localhost:4567/juan' * Adding handle: conn: 0x7fbdd1800000 * Adding handle: send: 0 * Adding handle: recv: 0 * Curl_addHandleToPipeline: length: 1 * - Conn 0 (0x7fbdd1800000) send_pipe: 1, recv_pipe: 0 * About to connect() to localhost port 4567 (#0) * Trying ::1... * Trying 127.0.0.1... * Connected to localhost (127.0.0.1) port 4567 (#0) > GET /juan HTTP/1.1 > User-Agent: curl/7.30.0 > Host: localhost:4567 > 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.6.1 codename Death Proof is not blacklisted < Server: thin 1.6.1 codename Death Proof < * Connection #0 to host localhost left intact Inside defined routeYou have the request scope binding inside:
get
, head
, post
, put
, delete
, options
, patch
, link
, and unlink
blocks
before
and after
filters
helper
methods