Class: Coach::Handler
- Inherits:
-
Object
- Object
- Coach::Handler
- Defined in:
- lib/coach/handler.rb
Instance Method Summary collapse
-
#build_request_chain(sequence, context) ⇒ Object
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.
-
#build_sequence(item, context) ⇒ Object
Traverse the middlware tree to build a linear middleware sequence, containing only middlewares that apply to this request.
-
#call(env) ⇒ Object
The Rack interface to handler - builds a middleware chain based on the current request, and invokes it.
-
#initialize(middleware) ⇒ Handler
constructor
A new instance of Handler.
Constructor Details
#initialize(middleware) ⇒ Handler
Returns a new instance of Handler.
5 6 7 8 |
# File 'lib/coach/handler.rb', line 5 def initialize(middleware) @root_item = MiddlewareItem.new(middleware) validate! end |
Instance Method Details
#build_request_chain(sequence, context) ⇒ Object
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.
51 52 53 54 55 56 |
# File 'lib/coach/handler.rb', line 51 def build_request_chain(sequence, context) chain_items = filter_sequence(sequence, context) chain_items.reverse.reduce(nil) do |successor, item| item.build_middleware(context, successor) end end |
#build_sequence(item, context) ⇒ Object
Traverse the middlware tree to build a linear middleware sequence, containing only middlewares that apply to this request.
38 39 40 41 42 43 44 45 46 |
# File 'lib/coach/handler.rb', line 38 def build_sequence(item, context) sub_sequence = item.middleware.middleware_dependencies filtered_sub_sequence = filter_sequence(sub_sequence, context) flattened_sub_sequence = filtered_sub_sequence.flat_map do |child_item| build_sequence(child_item, context) end dedup_sequence(flattened_sub_sequence + [item]) end |
#call(env) ⇒ Object
The Rack interface to handler - builds a middleware chain based on the current request, and invokes it.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/coach/handler.rb', line 15 def call(env) context = { request: ActionDispatch::Request.new(env) } sequence = build_sequence(@root_item, context) chain = build_request_chain(sequence, context) start_event = start_event(context) start = Time.now publish('coach.handler.start', start_event.dup) response = chain.instrument.call finish = Time.now publish('coach.handler.finish', start, finish, nil, start_event.merge( response: { status: response[0] }, metadata: context.fetch(:_metadata, {}))) response end |