Sha256: 12b292140369cfb84a508b7628be16f0349cef76e52230f4779539373249ad4d

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module JoyUssdEngine::SessionManager
    def expire_mins
      @selected_provider.expiration.blank? ? 60.seconds : @selected_provider.expiration
    end
  
    def user_mobile_number
      @user_mobile_number ||= get_state[:"#{@session_id}"]
    end
  
    # Retrive Session Data
    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
  
    # Store USSD sessions in Redis with Expiry
    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
  
    # Delete Session payload
    def reset_state
      session_id = params[:"#{@session_id}"]
      REDIS.with do |conn|
        conn.del(session_id)
      end
    end
  end
  

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
joy_ussd_engine-0.1.10 lib/joy_ussd_engine/session_manager.rb