class Rack::State
The Middleware Interface¶ ↑
Stores and manages the state of a single object on the server and sets the state token in a client cookie. Multiple State
instances may be utilized to manage multiple states, each with different storage adapters and options.
Constants
- VERSION
Public Class Methods
Middleware Options¶ ↑
- store
-
Instance of the storage adapter. See
Rack::State::Store
for available adapters. The default isStore::Memory
. - key
-
Client cookie name and
Rack
environment key suffix. For example, the key “token” sets a cookie named “token” and the environment key to “rack.state.token”. Therefore, the application can access theState::Manager
instance atenv["rack.state.token"]
. The default is “token”. - domain, path, max_age, expires, secure, httponly
-
Standard cookie options supported by
Rack
. None are set by default; therefore, the state will be valid for the session only on the domain and path in which it was set and available over unsecured connections.
# File lib/rack/state.rb, line 38 def initialize(app, options = {}) @app = app @options = options @store = options.delete(:store) || Store::Memory.new @key = options.delete(:key) || 'token' @skey = "rack.state.#{@key}" end
Public Instance Methods
# File lib/rack/state.rb, line 46 def call(env) request = Request.new(env) if request.path =~ /^#{@options[:path]}/ token = request.cookies.fetch(@key, nil) state = env[@skey] = Manager.new(@store, token, @key, @options) status, headers, body = @app.call(env) resp = Response.new(body, status, headers) state.finish(resp) resp.finish else @app.call(env) end end