Class: Booth::Requests::Session

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/booth/requests/session.rb

Overview

Convenience wrapper for ActionDispatch::Session::CookieStore.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope:, namespace:, session:) ⇒ Session

Returns a new instance of Session.



11
12
13
14
15
# File 'lib/booth/requests/session.rb', line 11

def initialize(scope:, namespace:, session:)
  @scope = ::Booth::Syntaxes::Scope.call(scope).normalized_scope
  @namespace = namespace
  @session = session
end

Instance Attribute Details

#scopeObject (readonly)

Returns the value of attribute scope.



9
10
11
# File 'lib/booth/requests/session.rb', line 9

def scope
  @scope
end

Instance Method Details

#[](key) ⇒ Object


Getters and Setters


Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
# File 'lib/booth/requests/session.rb', line 21

def [](key)
  return if timed_out?
  raise ArgumentError if key.to_s == 'created_at'

  reset_if_too_old!
  session[path(key)]
end

#[]=(key, value) ⇒ Object

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
# File 'lib/booth/requests/session.rb', line 29

def []=(key, value)
  return if timed_out?
  raise ArgumentError if key.to_s == 'created_at'

  ensure_ticking
  reset_if_too_old!
  session[path(key)] = value
end

#delete(key) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
# File 'lib/booth/requests/session.rb', line 38

def delete(key)
  raise ArgumentError if key.to_s == 'created_at'

  reset_if_too_old!
  session.delete path(key)
end

#lifespanObject


Timer




57
58
59
# File 'lib/booth/requests/session.rb', line 57

def lifespan
  ::Booth.config.interaction_timeout.to_i
end

#resetObject



45
46
47
48
49
50
51
# File 'lib/booth/requests/session.rb', line 45

def reset
  log { "Resetting #{namespace.inspect} cookie data in scope #{scope.inspect}" }

  # There is no `#delete_if` in a `ActionDispatch::Request::Session`.
  keys_to_delete = session.keys.select { it.to_s.start_with?(path(nil)) }
  keys_to_delete.each { session.delete(it) }
end

#seconds_until_auto_resetObject



61
62
63
64
65
# File 'lib/booth/requests/session.rb', line 61

def seconds_until_auto_reset
  return 0 if @reset_due_to_timeout

  lifespan - age
end

#timed_out?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/booth/requests/session.rb', line 67

def timed_out?
  reset_if_too_old!

  !!@reset_due_to_timeout
end