Class: Grape::MiddlewareStack

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/middleware_stack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMiddlewareStack

Returns a new instance of MiddlewareStack.



7
8
9
# File 'lib/grape/middleware_stack.rb', line 7

def initialize
  @stack = []
end

Instance Attribute Details

#stackObject (readonly)

Returns the value of attribute stack.



5
6
7
# File 'lib/grape/middleware_stack.rb', line 5

def stack
  @stack
end

Instance Method Details

#to_app(app) ⇒ Object

Apply this middleware stack to a Rack application.



26
27
28
29
30
31
32
33
# File 'lib/grape/middleware_stack.rb', line 26

def to_app(app)
  b = Rack::Builder.new
  for middleware in stack
    b.use *middleware
  end
  b.run app
  b.to_app
end

#use(klass, *args) ⇒ Object

Add a new middleware to the stack. Syntax is identical to normal middleware #use functionality.

Parameters:

  • klass (Class)

    The middleware class.



16
17
18
19
20
21
22
# File 'lib/grape/middleware_stack.rb', line 16

def use(klass, *args)
  if index = @stack.index(@stack.find{|a| a.first == klass})
    @stack[index] = [klass, *args]
  else
    @stack << [klass, *args]
  end
end