Module: JoyUssdEngine::SessionManager

Included in:
Core
Defined in:
lib/joy_ussd_engine/session_manager.rb

Instance Method Summary collapse

Instance Method Details

#expire_minsObject



4
5
6
# File 'lib/joy_ussd_engine/session_manager.rb', line 4

def expire_mins
  @selected_provider.expiration.blank? ? 60.seconds : @selected_provider.expiration
end

#get_stateObject

Retrive Session Data



13
14
15
16
17
18
19
20
# File 'lib/joy_ussd_engine/session_manager.rb', line 13

def get_state
  session_id = @params[:session_id]
  REDIS.with do |conn|
    data = conn.get(session_id)
    return {} if data.blank?
    JSON.parse(data, symbolize_names: true)
  end
end

#reset_stateObject

Delete Session payload



34
35
36
37
38
39
# File 'lib/joy_ussd_engine/session_manager.rb', line 34

def reset_state
  session_id = @params[:session_id]
  REDIS.with do |conn|
    conn.del(session_id)
  end
end

#set_state(payload = {}) ⇒ Object

Store USSD sessions in Redis with Expiry



23
24
25
26
27
28
29
30
31
# File 'lib/joy_ussd_engine/session_manager.rb', line 23

def set_state(payload = {})
  session_id = @params[:session_id]
  current_data = get_state
  REDIS.with do |conn|
    payload = current_data.merge(params.merge(payload))
    conn.set(session_id, payload.to_json)
    conn.expire(session_id, expire_mins)
  end
end

#user_mobile_numberObject



8
9
10
# File 'lib/joy_ussd_engine/session_manager.rb', line 8

def user_mobile_number
  @user_mobile_number ||= get_state[:"#{@params[:session_id]}"]
end