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
Public Class Methods
# 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
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
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
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
# File lib/omnihooks/strategy.rb, line 208 def inspect "#<#{self.class}>" end
# File lib/omnihooks/strategy.rb, line 229 def request @request ||= Rack::Request.new(@env) end
Protected Instance Methods
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
# File lib/omnihooks/strategy.rb, line 292 def current_path @current_path ||= request.path_info.downcase.sub(CURRENT_PATH_REGEX, EMPTY_STRING) end
# File lib/omnihooks/strategy.rb, line 300 def get_event self.class.event_stack(self).last end
# File lib/omnihooks/strategy.rb, line 296 def get_event_type self.class.event_type_stack(self).last end
# 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
# File lib/omnihooks/strategy.rb, line 272 def name options.name end
# File lib/omnihooks/strategy.rb, line 288 def on_path?(path) current_path.casecmp(path) == 0 end
# 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
# File lib/omnihooks/strategy.rb, line 268 def path_prefix options[:path_prefix] || OmniHooks.config.path_prefix end
# 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