Class: LHC::InterceptorProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/lhc/interceptor_processor.rb

Overview

Handles interceptions during the lifecycle of a request

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ InterceptorProcessor

Intitalizes the processor and determines if global or local interceptors are used



7
8
9
10
11
# File 'lib/lhc/interceptor_processor.rb', line 7

def initialize(target)
  options = target.options if target.is_a? LHC::Request
  options ||= target.request.options if target.is_a? LHC::Response
  self.interceptors = (options[:interceptors] || LHC.config.interceptors).map { |i| i.new }
end

Instance Attribute Details

#interceptorsObject

Returns the value of attribute interceptors.



4
5
6
# File 'lib/lhc/interceptor_processor.rb', line 4

def interceptors
  @interceptors
end

Instance Method Details

#intercept(name, target) ⇒ Object

Forwards messages to interceptors and handles provided responses.



14
15
16
17
18
19
20
21
22
23
# File 'lib/lhc/interceptor_processor.rb', line 14

def intercept(name, target)
  interceptors.each do |interceptor|
    result = interceptor.send(name, target)
    if result.is_a? LHC::Response
      fail 'Response already set from another interceptor' if @response
      request = target.is_a?(LHC::Request) ? target : target.request
      @response = request.response = result
    end
  end
end