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 = []) ⇒ Context

Initialize the interception context

Parameters:



29
30
31
# File 'lib/gruf/interceptors/context.rb', line 29

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

Instance Method Details

#intercept!Object

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



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

def intercept!
  return yield if @interceptors.none?

  i = @interceptors.pop
  return yield unless i

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

  i.call do
    if @interceptors.any?
      intercept! { yield }
    else
      yield
    end
  end
end