Class: Bianchi::USSD::Store
- Inherits:
-
Object
- Object
- Bianchi::USSD::Store
- Defined in:
- lib/bianchi/ussd/store.rb
Instance Attribute Summary collapse
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Instance Method Summary collapse
- #all ⇒ Object
- #get(key) ⇒ Object
-
#initialize(session) ⇒ Store
constructor
A new instance of Store.
- #parse_value(value) ⇒ Object
- #parse_values(data) ⇒ Object
- #previous_session ⇒ Object
- #set(key, value) ⇒ Object
- #track_session ⇒ Object
Constructor Details
#initialize(session) ⇒ Store
Returns a new instance of Store.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/bianchi/ussd/store.rb', line 8 def initialize(session) redis_url = ENV.fetch("REDIS_URL", nil) unless redis_url.present? raise ArgumentError, "environment variable REDIS_URL required to track engine session." end @redis = Redis.new(url: redis_url) @session = session end |
Instance Attribute Details
#session ⇒ Object (readonly)
Returns the value of attribute session.
6 7 8 |
# File 'lib/bianchi/ussd/store.rb', line 6 def session @session end |
Instance Method Details
#all ⇒ Object
45 46 47 |
# File 'lib/bianchi/ussd/store.rb', line 45 def all parse_values @redis.hgetall("#{session.id}-#{session.mobile_number}-store") end |
#get(key) ⇒ Object
41 42 43 |
# File 'lib/bianchi/ussd/store.rb', line 41 def get(key) parse_value @redis.hget("#{session.id}-#{session.mobile_number}-store", key) end |
#parse_value(value) ⇒ Object
57 58 59 60 61 |
# File 'lib/bianchi/ussd/store.rb', line 57 def parse_value(value) JSON.parse(value) rescue StandardError value end |
#parse_values(data) ⇒ Object
53 54 55 |
# File 'lib/bianchi/ussd/store.rb', line 53 def parse_values(data) data.transform_values { |v| parse_value v } end |
#previous_session ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/bianchi/ussd/store.rb', line 29 def previous_session previous_activity = @redis.hgetall("#{session.id}-#{session.mobile_number}-activity") return if previous_activity.empty? previous_activity_data = JSON.parse(previous_activity["data"]).with_indifferent_access = Menu.new(previous_activity_data[:menu]) page_number = previous_activity_data[:page_number] params = previous_activity_data[:params] Session.new(params, , page_number) end |
#set(key, value) ⇒ Object
49 50 51 |
# File 'lib/bianchi/ussd/store.rb', line 49 def set(key, value) @redis.hset("#{session.id}-#{session.mobile_number}-store", { key => value.to_json }) end |
#track_session ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/bianchi/ussd/store.rb', line 19 def track_session data = { params: session.params, menu: session..name, page_number: session.page_number }.to_json @redis.hset("#{session.id}-#{session.mobile_number}-activity", data: data) end |