Class: Gruf::Interceptors::Context

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/gruf/interceptors/context.rb

Overview

Runs interceptors in a given request context

Instance Method Summary collapse

Methods included from Loggable

#logger

Constructor Details

#initialize(interceptors = nil) ⇒ Context

Initialize the interception context

Parameters:



31
32
33
# File 'lib/gruf/interceptors/context.rb', line 31

def initialize(interceptors = nil)
  @interceptors = interceptors || []
end

Instance Method Details

#intercept!(&block) ⇒ Object

Intercept the given request and run interceptors in a FIFO execution order



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/gruf/interceptors/context.rb', line 38

def intercept!(&block)
  return yield if @interceptors.none?

  i = @interceptors.shift
  return yield unless i

  logger.debug "Intercepting request with interceptor: #{i.class}"

  i.call do
    if @interceptors.any?
      intercept!(&block)
    else
      yield
    end
  end
end