class Coach::Handler
Constants
- STATUS_CODE_FOR_EXCEPTIONS
Attributes
Public Class Methods
Source
# File lib/coach/handler.rb, line 12 def initialize(middleware, config = {}) @config = config if middleware.is_a?(String) @name = middleware else @middleware = middleware @name = middleware.name # This triggers validation of the middleware chain, to raise any errors early on. root_item end end
Public Instance Methods
Source
# File lib/coach/handler.rb, line 71 def build_request_chain(sequence, context) sequence.reverse.reduce(nil) do |successor, item| item.build_middleware(context, successor) end end
Given a middleware sequence, filter out items not applicable to the current request, and set up a chain of instantiated middleware objects, ready to serve a request.
Source
# File lib/coach/handler.rb, line 60 def build_sequence(item, context) sequence = item.middleware.middleware_dependencies.map do |child_item| build_sequence(child_item.set_parent(item), context) end dedup_sequence([*sequence, item].flatten) end
Traverse the middlware tree to build a linear middleware sequence, containing only middlewares that apply to this request.
Source
# File lib/coach/handler.rb, line 28 def call(env) context = { request: ActionDispatch::Request.new(env) } sequence = build_sequence(root_item, context) chain = build_request_chain(sequence, context) event = build_event(context) publish("start_handler.coach", event.dup) instrument("finish_handler.coach", event) do response = chain.instrument.call rescue StandardError => e raise ensure # We want to populate the response and metadata fields after the middleware # chain has completed so that the end of the instrumentation can see them. The # simplest way to do this is pass the event by reference to ActiveSupport, then # modify the hash to contain this detail before the instrumentation completes. # # This way, the last finish_handler.coach event will have all the details. status = response.try(:first) || STATUS_CODE_FOR_EXCEPTIONS event.merge!( response: { status: status, exception: e, }.compact, metadata: context.fetch(:_metadata, {}), ) end end
The Rack interface to handler - builds a middleware chain based on the current request, and invokes it.
Private Instance Methods
Source
# File lib/coach/handler.rb, line 104 def build_event(context) { middleware: name, request: context[:request] } end
Event to send with notifications
Source
# File lib/coach/handler.rb, line 99 def dedup_sequence(sequence) sequence.uniq { |item| [item.class, item.middleware, item.config] } end
Remove middleware that have been included multiple times with the same config, leaving only the first instance
Source
# File lib/coach/handler.rb, line 93 def middleware @middleware ||= ActiveSupport::Inflector.constantize(name) end
Source
# File lib/coach/handler.rb, line 85 def root_item @root_item ||= MiddlewareItem.new(middleware, config).tap(&:validate!) rescue Coach::Errors::MiddlewareDependencyNotMet => e # Remove noise of validation stack trace, reset to the handler callsite e.backtrace.clear.concat(Thread.current.backtrace) raise e end