Class: Rack::State

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/state.rb

Overview

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.

State initializes Manager, which contains the API.

Defined Under Namespace

Modules: Store Classes: Manager

Constant Summary collapse

VERSION =
'0.0.2'

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ State

Middleware Options

store

Instance of the storage adapter. See Rack::State::Store for available adapters. The default is Store::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 the State::Manager instance at env["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.



38
39
40
41
42
43
44
# 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

Instance Method Details

#call(env) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/rack/state.rb', line 46

def call(env)
  token = Request.new(env).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
end