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

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

Instance Attribute Summary collapse

Attributes included from GatewayAsyncSupport

#controller

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GatewayAsyncSupport

#enqueue_async_job, #extract_body_for_background, #extract_headers_for_background, #extract_host, #extract_path, #extract_remote_ip, #in_background?, #should_enqueue_async?

Methods included from Instrumentation

#instrument, instrument, report_api_error

Constructor Details

#initialize(app) ⇒ Nalo

Returns a new instance of Nalo.



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

def initialize(app)
  @app = app
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

Class Method Details

.configure_middleware_stack(builder, custom_middleware) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/flow_chat/ussd/gateway/nalo.rb', line 67

def self.configure_middleware_stack(builder, custom_middleware)
  FlowChat.logger.debug { "FlowChat::Ussd::Gateway::Nalo: Configuring middleware stack" }

  builder.use FlowChat::Ussd::Middleware::Pagination
  FlowChat.logger.debug { "FlowChat::Ussd::Gateway::Nalo: Added Ussd::Middleware::Pagination" }

  builder.use custom_middleware
  FlowChat.logger.debug { "FlowChat::Ussd::Gateway::Nalo: Added custom middleware" }

  builder.use FlowChat::Ussd::Middleware::ChoiceMapper
  FlowChat.logger.debug { "FlowChat::Ussd::Gateway::Nalo: Added Ussd::Middleware::ChoiceMapper" }
end

Instance Method Details

#async_supported?Boolean

USSD does not support async processing due to synchronous protocol requirements

Returns:

  • (Boolean)


15
16
17
# File 'lib/flow_chat/ussd/gateway/nalo.rb', line 15

def async_supported?
  false
end

#call(context) ⇒ Object



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
59
60
61
62
63
64
65
# File 'lib/flow_chat/ussd/gateway/nalo.rb', line 19

def call(context)
  @context = context
  @controller = context.controller
  params = @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.body"] = (params.respond_to?(:to_unsafe_h) ? params.to_unsafe_h : params.to_h).transform_keys(&:to_s)
  # 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"]
  })

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