Class: FlowChat::Ussd::Middleware::ResumableSession

Inherits:
Object
  • Object
show all
Defined in:
lib/flow_chat/ussd/middleware/resumable_session.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ResumableSession

Returns a new instance of ResumableSession.



5
6
7
# File 'lib/flow_chat/ussd/middleware/resumable_session.rb', line 5

def initialize(app)
  @app = app
end

Instance Method Details

#call(context) ⇒ Object



9
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
# File 'lib/flow_chat/ussd/middleware/resumable_session.rb', line 9

def call(context)
  if Config.resumable_sessions_enabled && context["ussd.request"].present?
    request = Rack::Request.new(context)
    session = request.session

    context["ussd.resumable_sessions"] = {}

    # If this is a new session but we have the flag set, this means the call terminated before
    # the session closed. Force it to resume.
    # This is safe since a new session is started if the old session does not indeed exist.
    if context["ussd.request"][:type] == :initial && can_resume_session?(session)
      context["ussd.request"][:type] = :response
      context["ussd.resumable_sessions"][:resumed] = true
    end

    res = @app.call(context)

    if context["ussd.response"].present?
      if context["ussd.response"][:type] == :terminal || context["ussd.resumable_sessions"][:disable]
        session.delete "ussd.resumable_sessions"
      else
        session["ussd.resumable_sessions"] = Time.now.to_i
      end
    end

    res
  else
    @app.call(context)
  end
end