Class: Whoosh::Middleware::PluginHooks

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

Instance Method Summary collapse

Constructor Details

#initialize(app, plugins:, configs:) ⇒ PluginHooks

Returns a new instance of PluginHooks.



6
7
8
9
10
# File 'lib/whoosh/middleware/plugin_hooks.rb', line 6

def initialize(app, plugins:, configs:)
  @app = app
  @plugins = plugins.select { |p| p.respond_to?(:middleware?) && p.middleware? }
  @configs = configs
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/whoosh/middleware/plugin_hooks.rb', line 12

def call(env)
  @plugins.each do |plugin|
    plugin.before_request(env, @configs[plugin] || {}) if plugin.respond_to?(:before_request)
  end

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

  @plugins.each do |plugin|
    plugin.after_response([status, headers, body], @configs[plugin] || {}) if plugin.respond_to?(:after_response)
  end

  [status, headers, body]
end