Class: FlowChat::Ussd::Gateway::Nalo

Inherits:
Object
  • Object
show all
Includes:
Instrumentation
Defined in:
lib/flow_chat/ussd/gateway/nalo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Instrumentation

#instrument, instrument

Constructor Details

#initialize(app) ⇒ Nalo

Returns a new instance of Nalo.



9
10
11
# File 'lib/flow_chat/ussd/gateway/nalo.rb', line 9

def initialize(app)
  @app = app
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/flow_chat/ussd/gateway/nalo.rb', line 7

def context
  @context
end

Instance Method Details

#call(context) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/flow_chat/ussd/gateway/nalo.rb', line 13

def call(context)
  @context = context
  params = context.controller.request.params

  context["request.id"] = params["USERID"]
  context["request.msisdn"] = FlowChat::PhoneNumberUtil.to_e164(params["MSISDN"])
  context["request.user_id"] = context["request.msisdn"]
  context["request.message_id"] = SecureRandom.uuid
  context["request.timestamp"] = Time.current.iso8601
  context["request.gateway"] = :nalo
  context["request.platform"] = :ussd
  context["request.network"] = nil
  # context["request.type"] = params["MSGTYPE"] ? :initial : :response
  context.input = params["USERDATA"].presence

  # Instrument message received when user provides input using new scalable approach
  if context.input.present?
    instrument(Events::MESSAGE_RECEIVED, {
      from: context["request.user_id"],
      message: context.input,
      timestamp: context["request.timestamp"]
    })
  end

  # Process the request and instrument the response
  type, prompt, choices, media = @app.call(context)

  # Instrument message sent using new scalable approach
  instrument(Events::MESSAGE_SENT, {
    to: context["request.msisdn"],
    session_id: context["request.id"],
    message: context.input || "",
    message_type: (type == :prompt) ? "prompt" : "terminal",
    gateway: :nalo,
    platform: :ussd,
    content_length: prompt.to_s.length,
    timestamp: context["request.timestamp"]
  })

  context.controller.render json: {
    USERID: params["USERID"],
    MSISDN: params["MSISDN"],
    MSG: render_prompt(prompt, choices, media),
    MSGTYPE: type == :prompt
  }
end