Module: OmniHooks::Strategy

Included in:
OmniHooks::Strategies::Developer
Defined in:
lib/omnihooks/strategy.rb

Overview

The Strategy is the base unit of OmniHooks’s ability to wrangle multiple providers. Each strategy provided by OmniHooks includes this mixin to gain the default functionality necessary to be compatible with the OmniHooks library.

Defined Under Namespace

Modules: ClassMethods Classes: Namespace, NotificationAdapter, Options

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



173
174
175
# File 'lib/omnihooks/strategy.rb', line 173

def options
  @options
end

Class Method Details

.included(base) ⇒ Object

rubocop:disable ModuleLength



9
10
11
12
13
14
15
16
17
18
# File 'lib/omnihooks/strategy.rb', line 9

def self.included(base)
  OmniHooks.strategies << base

  base.extend ClassMethods
  base.class_eval do
    option :backend, ActiveSupport::Notifications
    option :adapter, OmniHooks::Strategy::NotificationAdapter
    option :namespace_delimiter, '.'
  end
end

Instance Method Details

#call(env) ⇒ Object

Duplicates this instance and runs #call! on it.

Parameters:

  • The (Hash)

    Rack environment.



214
215
216
# File 'lib/omnihooks/strategy.rb', line 214

def call(env)
  dup.call!(env)
end

#call!(env) ⇒ Object

The logic for dispatching any additional actions that need to be taken. For instance, calling the request phase if the request path is recognized.

Parameters:

  • env (Hash)

    The Rack environment.



222
223
224
225
226
227
# File 'lib/omnihooks/strategy.rb', line 222

def call!(env) # rubocop:disable CyclomaticComplexity, PerceivedComplexity
  @env = env
  return instrument if on_request_path? && OmniHooks.config.allowed_request_methods.include?(request.request_method.downcase.to_sym)

  @app.call(env)
end

#new(app, options = {}) ⇒ Object #new(app, *args, options = {}) ⇒ Object

Initializes the strategy by passing in the Rack endpoint, the unique URL segment name for this strategy, and any additional arguments. An ‘options` hash is automatically created from the last argument if it is a hash.

Overloads:

  • #new(app, options = {}) ⇒ Object

    If nothing but a hash is supplied, initialized with the supplied options overriding the strategy’s default options via a deep merge.

  • #new(app, *args, options = {}) ⇒ Object

    If the strategy has supplied custom arguments that it accepts, they may will be passed through and set to the appropriate values.

Parameters:

  • app (Rack application)

    The application on which this middleware is applied.

Yields:

  • (Class, Options)

    Yields Parent class and options to block for further configuration.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/omnihooks/strategy.rb', line 190

def initialize(app, *args, &block) # rubocop:disable UnusedMethodArgument
  @app = app
  @env = nil
  @options = self.class.default_options.dup

  options.deep_merge!(args.pop) if args.last.is_a?(Hash)

  self.class.args.each do |arg|
    break if args.empty?
    options[arg] = args.shift
  end

  # Make sure that all of the args have been dealt with, otherwise error out.
  fail(ArgumentError.new("Received wrong number of arguments. #{args.inspect}")) unless args.empty?

  yield self.class, options if block_given?
end

#inspectObject



208
209
210
# File 'lib/omnihooks/strategy.rb', line 208

def inspect
  "#<#{self.class}>"
end

#requestObject



229
230
231
# File 'lib/omnihooks/strategy.rb', line 229

def request
  @request ||= Rack::Request.new(@env)
end