Class: Grape::MiddlewareStack
- Inherits:
-
Object
- Object
- Grape::MiddlewareStack
- Defined in:
- lib/grape/middleware_stack.rb
Instance Attribute Summary collapse
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
Instance Method Summary collapse
-
#initialize ⇒ MiddlewareStack
constructor
A new instance of MiddlewareStack.
-
#to_app(app) ⇒ Object
Apply this middleware stack to a Rack application.
-
#use(klass, *args) ⇒ Object
Add a new middleware to the stack.
Constructor Details
#initialize ⇒ MiddlewareStack
Returns a new instance of MiddlewareStack.
7 8 9 |
# File 'lib/grape/middleware_stack.rb', line 7 def initialize @stack = [] end |
Instance Attribute Details
#stack ⇒ Object (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.
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 |