module Roda::RodaPlugins::Sessions::RequestMethods

  1. lib/roda/plugins/sessions.rb

Public Instance methods

persist_session(headers, session)

Persist the session data as a cookie.

[show source]
    # File lib/roda/plugins/sessions.rb
304 def persist_session(headers, session)
305   config = roda_class.sessions_config
306 
307   if session.empty?
308     if env['roda.session.serialized']
309       # If session was submitted and is now empty, remove the cookie
310       Rack::Utils.delete_cookie_header!(headers, config.key, config.remove_cookie_options)
311     # else
312       # If no session was submitted, and the session is empty
313       # then there is no need to do anything
314     end
315   elsif cookie_value = _serialize_session(session)
316     cookie = Hash[config.cookie_options]
317     cookie[:value] = cookie_value
318     cookie[:secure] = true if !cookie.has_key?(:secure) && ssl?
319 
320     before_size = if (set_cookie_before = headers[RodaResponseHeaders::SET_COOKIE]).is_a?(String)
321       set_cookie_before.bytesize
322     else
323       0
324     end
325 
326     Rack::Utils.set_cookie_header!(headers, config.key, cookie)
327 
328     cookie_size = case set_cookie_after = headers[RodaResponseHeaders::SET_COOKIE]
329     when String
330       # Rack < 3 always takes this branch, combines cookies into string, subtract previous size
331       # Rack 3+ takes this branch if this is the first cookie set, in which case before size is 0
332       set_cookie_after.bytesize - before_size
333     else # when Array
334       # Rack 3+ takes branch if this is not the first cookie set, and last element of the array
335       # is most recently added cookie
336       set_cookie_after.last.bytesize
337     end
338 
339     if cookie_size >= 4096
340       raise CookieTooLarge, "attempted to create cookie larger than 4096 bytes (bytes: #{cookie_size})"
341     end
342   end
343   
344   if env['roda.session.delete_rack_session_cookie']
345     opts = roda_class.sessions_opts
346     Rack::Utils.delete_cookie_header!(headers, opts[:upgrade_from_rack_session_cookie_key], opts[:upgrade_from_rack_session_cookie_options])
347   end
348 
349   nil
350 end
session()

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.

[show source]
    # File lib/roda/plugins/sessions.rb
287 def session
288   @env[roda_class.sessions_config.env_key] ||= _load_session
289 end
session_created_at()

The time the session was originally created. nil if there is no active session.

[show source]
    # File lib/roda/plugins/sessions.rb
292 def session_created_at
293   session
294   Time.at(@env['roda.session.created_at']) if @env['roda.session.serialized']
295 end
session_updated_at()

The time the session was last updated. nil if there is no active session.

[show source]
    # File lib/roda/plugins/sessions.rb
298 def session_updated_at
299   session
300   Time.at(@env['roda.session.updated_at']) if @env['roda.session.serialized']
301 end