Class: Stealth::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/stealth/session.rb

Constant Summary collapse

SLUG_SEPARATOR =
'->'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_id:) ⇒ Session

Returns a new instance of Session.



11
12
13
14
15
16
17
18
19
20
# File 'lib/stealth/session.rb', line 11

def initialize(user_id:)
  @user_id = user_id

  unless defined?($redis)
    raise(Stealth::Errors::RedisNotConfigured, "Please make sure REDIS_URL is configured before using sessions.")
  end

  get
  self
end

Instance Attribute Details

#flowObject (readonly)

Returns the value of attribute flow.



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

def flow
  @flow
end

#sessionObject (readonly)

Returns the value of attribute session.



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

def session
  @session
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

#user_idObject (readonly)

Returns the value of attribute user_id.



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

def user_id
  @user_id
end

Class Method Details

.flow_and_state_from_session_slug(slug:) ⇒ Object



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

def self.flow_and_state_from_session_slug(slug:)
  {
    flow: slug&.split(SLUG_SEPARATOR)&.first,
    state: slug&.split(SLUG_SEPARATOR)&.last
  }
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/stealth/session.rb', line 63

def blank?
  !present?
end

#flow_stringObject



41
42
43
# File 'lib/stealth/session.rb', line 41

def flow_string
  session&.split(SLUG_SEPARATOR)&.first
end

#getObject



49
50
51
# File 'lib/stealth/session.rb', line 49

def get
  @session ||= $redis.get(user_id)
end

#present?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/stealth/session.rb', line 59

def present?
  session.present?
end

#set(flow:, state:) ⇒ Object



53
54
55
56
57
# File 'lib/stealth/session.rb', line 53

def set(flow:, state:)
  @session = canonical_session_slug(flow: flow, state: state)
  flow
  $redis.set(user_id, canonical_session_slug(flow: flow, state: state))
end

#state_stringObject



45
46
47
# File 'lib/stealth/session.rb', line 45

def state_string
  session&.split(SLUG_SEPARATOR)&.last
end