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. If transparently upgrading from Rack::Session::Cookie, mark the related cookie for expiration so it isn’t sent in the future.

[show source]
    # File lib/roda/plugins/sessions.rb
261 def persist_session(headers, session)
262   opts = roda_class.opts[:sessions]
263 
264   if session.empty?
265     if env[SESSION_SERIALIZED]
266       # If session was submitted and is now empty, remove the cookie
267       Rack::Utils.delete_cookie_header!(headers, opts[:key], opts[:remove_cookie_options])
268     # else
269       # If no session was submitted, and the session is empty
270       # then there is no need to do anything
271     end
272   elsif cookie_value = _serialize_session(session)
273     cookie = Hash[opts[:cookie_options]]
274     cookie[:value] = cookie_value
275     cookie[:secure] = true if !cookie.has_key?(:secure) && ssl?
276 
277     before_size = if (set_cookie_before = headers[RodaResponseHeaders::SET_COOKIE]).is_a?(String)
278       set_cookie_before.bytesize
279     else
280       0
281     end
282 
283     Rack::Utils.set_cookie_header!(headers, opts[:key], cookie)
284 
285     cookie_size = case set_cookie_after = headers[RodaResponseHeaders::SET_COOKIE]
286     when String
287       # Rack < 3 always takes this branch, combines cookies into string, subtract previous size
288       # Rack 3+ takes this branch if this is the first cookie set, in which case before size is 0
289       set_cookie_after.bytesize - before_size
290     else # when Array
291       # Rack 3+ takes branch if this is not the first cookie set, and last element of the array
292       # is most recently added cookie
293       set_cookie_after.last.bytesize
294     end
295 
296     if cookie_size >= 4096
297       raise CookieTooLarge, "attempted to create cookie larger than 4096 bytes (bytes: #{cookie_size})"
298     end
299   end
300   
301   if env[SESSION_DELETE_RACK_COOKIE]
302     Rack::Utils.delete_cookie_header!(headers, opts[:upgrade_from_rack_session_cookie_key], opts[:upgrade_from_rack_session_cookie_options])
303   end
304 
305   nil
306 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
242 def session
243   @env['rack.session'] ||= _load_session
244 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
247 def session_created_at
248   session
249   Time.at(@env[SESSION_CREATED_AT]) if @env[SESSION_SERIALIZED]
250 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
253 def session_updated_at
254   session
255   Time.at(@env[SESSION_UPDATED_AT]) if @env[SESSION_SERIALIZED]
256 end