Module: Roda::RodaPlugins::Sessions::RequestMethods

Defined in:
lib/roda/plugins/sessions.rb

Instance Method Summary collapse

Instance Method Details

#persist_session(headers, session) ⇒ Object

Persist the session data as a cookie. If transparently upgrading from Rack::Session::Cookie, mark the related cookie for expiration so it isn’t sent in the future.



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/roda/plugins/sessions.rb', line 232

def persist_session(headers, session)
  opts = roda_class.opts[:sessions]

  if session.empty?
    if env[SESSION_SERIALIZED]
      # If session was submitted and is now empty, remove the cookie
      Rack::Utils.delete_cookie_header!(headers, opts[:key])
    # else
      # If no session was submitted, and the session is empty
      # then there is no need to do anything
    end
  elsif cookie_value = _serialize_session(session)
    cookie = Hash[opts[:cookie_options]]
    cookie[:value] = cookie_value
    cookie[:secure] = true if !cookie.has_key?(:secure) && ssl?
    Rack::Utils.set_cookie_header!(headers, opts[:key], cookie)
  end
  
  if env[SESSION_DELETE_RACK_COOKIE]
    Rack::Utils.delete_cookie_header!(headers, opts[:upgrade_from_rack_session_cookie_key], opts[:upgrade_from_rack_session_cookie_options])
  end

  nil
end

#sessionObject

Load the session information from the cookie. With the sessions plugin, you must call this method to get the session, instead of trying to access the session directly through the request environment. For maximum compatibility with other software that uses rack sessions, this method stores the session in ‘rack.session’ in the request environment, but that does not happen until this method is called.



225
226
227
# File 'lib/roda/plugins/sessions.rb', line 225

def session
  @env['rack.session'] ||= _load_session
end