Class: UssdEngine::Middleware::ResumableSessions

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ResumableSessions

Returns a new instance of ResumableSessions.



4
5
6
# File 'lib/ussd_engine/middleware/resumable_sessions.rb', line 4

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



8
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
# File 'lib/ussd_engine/middleware/resumable_sessions.rb', line 8

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

    env["ussd_engine.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 env["ussd_engine.request"][:type] == :initial && can_resume_session?(session)
      env["ussd_engine.request"][:type] = :response
      env["ussd_engine.resumable_sessions"][:resumed] = true
    end

    res = @app.call(env)

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

    res
  else
    @app.call(env)
  end
end