Class: Coach::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/coach/handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(middleware, config = {}) ⇒ Handler

Returns a new instance of Handler.



5
6
7
8
9
10
11
12
# File 'lib/coach/handler.rb', line 5

def initialize(middleware, config = {})
  @root_item = MiddlewareItem.new(middleware, config)
  validate!
rescue Coach::Errors::MiddlewareDependencyNotMet => error
  # Remove noise of validation stack trace, reset to the handler callsite
  error.backtrace.clear.push(*Thread.current.backtrace)
  raise error
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.



53
54
55
56
57
# File 'lib/coach/handler.rb', line 53

def build_request_chain(sequence, context)
  sequence.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.



42
43
44
45
46
47
48
# File 'lib/coach/handler.rb', line 42

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

#call(env) ⇒ Object

The Rack interface to handler - builds a middleware chain based on the current request, and invokes it.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/coach/handler.rb', line 19

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