Class: UssdEngine::Middleware::NaloProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/ussd_engine/middleware/nalo_processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ NaloProcessor

Returns a new instance of NaloProcessor.



6
7
8
# File 'lib/ussd_engine/middleware/nalo_processor.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
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
# File 'lib/ussd_engine/middleware/nalo_processor.rb', line 10

def call(env)
  input = env["rack.input"].read
  env["rack.input"].rewind
  if input.present?
    params = JSON.parse input
    if params["USERID"].present? && params["MSISDN"].present?
      env["ussd_engine.request"] = {
        provider: :nalo,
        network: nil,
        msisdn: Phonelib.parse(params["MSISDN"]).e164,
        type: params["MSGTYPE"] ? :initial : :response,
        input: params["USERDATA"].presence,
        network: nil,
      }
    end
  end

  status, headers, response = @app.call(env)

  if env["ussd_engine.response"].present? && env["ussd_engine.request"][:provider] == :nalo
    status = 200
    response =
      {
        USERID: params["USERID"],
        MSISDN: params["MSISDN"],
        MSG: env["ussd_engine.response"][:body],
        MSGTYPE: env["ussd_engine.response"][:type] != :terminal,
      }.to_json
    headers = headers.merge({ "Content-Type" => "application/json", "Content-Length" => response.bytesize.to_s })
    response = [response]
  end
  [status, headers, response]
end