Class: RodaSessionMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/roda/session_middleware.rb

Overview

Session middleware that can be used in any Rack application that uses Roda’s sessions plugin for encrypted and signed cookies. See Roda::RodaPlugins::Sessions for details on options.

Defined Under Namespace

Modules: RequestMethods Classes: SessionHash

Instance Method Summary collapse

Constructor Details

#initialize(app, opts) ⇒ RodaSessionMiddleware

Setup the middleware, passing opts as the Roda sessions plugin options.



160
161
162
163
164
165
166
# File 'lib/roda/session_middleware.rb', line 160

def initialize(app, opts)
  mid = Class.new(Roda)
  mid.plugin :sessions, opts
  @req_class = mid::RodaRequest
  @req_class.send(:include, RequestMethods)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

Initialize the session hash in the environment before calling the next application, and if the session has been loaded after the result has been returned, then persist the session in the cookie.



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/roda/session_middleware.rb', line 171

def call(env)
  session = env['rack.session'] = SessionHash.new(@req_class.new(nil, env))

  res = @app.call(env)

  if session.loaded?
    session.req.persist_session(res[1], session.data)
  end

  res
end