Class: MaybeLater::Middleware

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

Constant Summary collapse

RACK_AFTER_REPLY =
"rack.after_reply"

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



5
6
7
# File 'lib/maybe_later/middleware.rb', line 5

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ 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/maybe_later/middleware.rb', line 9

def call(env)
  config = MaybeLater.config

  status, headers, body = @app.call(env)
  if Store.instance.callbacks.any?
    if env.key?(RACK_AFTER_REPLY)
      env[RACK_AFTER_REPLY] << -> {
        RunsCallbacks.new.call
      }
    elsif !config.invoke_even_if_server_is_unsupported
      warn "        This server may not support '\#{RACK_AFTER_REPLY}' callbacks. To\n        ensure that your tasks are executed, consider enabling:\n\n          config.invoke_even_if_server_is_unsupported = true\n\n        Note that this option, when combined with `inline: true` can result\n        in delayed flushing of HTTP responses by the server (defeating the\n        purpose of the gem.\n      MSG\n    else\n      RunsCallbacks.new.call\n    end\n\n    if Store.instance.callbacks.any? { |cb| cb.inline }\n      headers[\"Connection\"] = \"close\"\n    end\n  end\n  [status, headers, body]\nend\n"