module OmniHooks::Strategy

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.

Constants

CURRENT_PATH_REGEX
EMPTY_STRING

Attributes

app[R]
env[R]
options[R]

Public Class Methods

included(base) click to toggle source
# 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
new(app, *args) { |class, options| ... } click to toggle source

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.

@param app [Rack application] The application on which this middleware is applied.

@overload new(app, options = {})

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

@overload new(app, *args, options = {})

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

@yield [Class, Options] Yields Parent class and options to block for further configuration.

# 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

Public Instance Methods

call(env) click to toggle source

Duplicates this instance and runs call! on it. @param [Hash] The Rack environment.

# File lib/omnihooks/strategy.rb, line 214
def call(env)
  dup.call!(env)
end
call!(env) click to toggle source

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

@param env [Hash] The Rack environment.

# 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
inspect() click to toggle source
# File lib/omnihooks/strategy.rb, line 208
def inspect
  "#<#{self.class}>"
end
request() click to toggle source
# File lib/omnihooks/strategy.rb, line 229
def request
  @request ||= Rack::Request.new(@env)
end

Protected Instance Methods

log(level, message) click to toggle source

Direct access to the OmniAuth logger, automatically prefixed with this strategy's name.

@example

log :warn, "This is a warning."
# File lib/omnihooks/strategy.rb, line 241
def log(level, message)
  OmniHooks.logger.send(level, "(#{name}) #{message}")
end

Private Instance Methods

current_path() click to toggle source
# File lib/omnihooks/strategy.rb, line 292
def current_path
  @current_path ||= request.path_info.downcase.sub(CURRENT_PATH_REGEX, EMPTY_STRING)
end
get_event() click to toggle source
# File lib/omnihooks/strategy.rb, line 300
def get_event
  self.class.event_stack(self).last
end
get_event_type() click to toggle source
# File lib/omnihooks/strategy.rb, line 296
def get_event_type
  self.class.event_type_stack(self).last
end
instrument() click to toggle source
# File lib/omnihooks/strategy.rb, line 250
def instrument
  # instance needs to lookup and from the paylook the event type
  begin
    evt = get_event
    evt_type = get_event_type

    self.class.instrument(evt_type, evt) if evt
  rescue Exception => e
    log(:error, e.message)
    log(:error, e.backtrace.join("\n"))
    [500, {}, [EMPTY_STRING]]
  else
    log(:debug, "success")
    # Send a 200 response back to
    [200, {}, [EMPTY_STRING]]
  end
end
name() click to toggle source
# File lib/omnihooks/strategy.rb, line 272
def name
  options.name
end
on_path?(path) click to toggle source
# File lib/omnihooks/strategy.rb, line 288
def on_path?(path)
  current_path.casecmp(path) == 0
end
on_request_path?() click to toggle source
# File lib/omnihooks/strategy.rb, line 280
def on_request_path?
  if options.request_path.respond_to?(:call)
    options.request_path.call(env)
  else
    on_path?(request_path)
  end
end
path_prefix() click to toggle source
# File lib/omnihooks/strategy.rb, line 268
def path_prefix
  options[:path_prefix] || OmniHooks.config.path_prefix
end
request_path() click to toggle source
# File lib/omnihooks/strategy.rb, line 276
def request_path
  @request_path ||= options[:request_path].is_a?(String) ? options[:request_path] : "#{path_prefix}/#{name}"
end