Class: Junkfood::Rack::TransientSession

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

Overview

Rack Middleware that sets the ‘rack.session’ environment variable with a new Hash object.

This is specifically useful for when an application, or other middleware, assumes that a rack session is set yet the developer doesn’t want to save an actual Cookie or Database session.

For example, one may want to use API tokens for authentication in a rack application protected by ‘warden`. Each request requires the API token for each call and we don’t want the authenticated user to be saved in a Cookie session.

Simply:

use Junkfood::Rack::TransientSession
use Warden::Manager do |manager|
  manager.default_strategies :web_api_access_token
  manager.failure_app = Junkfood::WebApi::UnauthenticatedHandler.new
end
use Junkfood::WebApi::AccessTokenAuthentication

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ TransientSession

Returns a new instance of TransientSession.

Parameters:

  • app

    the rest of the Rack stack.



46
47
48
# File 'lib/junkfood/rack/sessions.rb', line 46

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

Returns a Rack response from the application.

Parameters:

  • env

    the Rack environment.

Returns:

  • a Rack response from the application.



54
55
56
57
58
# File 'lib/junkfood/rack/sessions.rb', line 54

def call(env)
  new_env = env.dup
  new_env['rack.session'] = {}
  @app.call(new_env)
end