class Grape::Middleware::Stack
Class to handle the stack of middlewares based on ActionDispatch::MiddlewareStack It allows to insert and insert after
Attributes
Public Class Methods
Source
# File lib/grape/middleware/stack.rb, line 50 def initialize @middlewares = [] @others = [] end
Public Instance Methods
Source
# File lib/grape/middleware/stack.rb, line 84 def build Rack::Builder.new.tap do |builder| others.shift(others.size).each { |m| merge_with(m) } middlewares.each do |m| m.build(builder) end end end
@return [Rack::Builder] the builder object with our middlewares applied
Source
# File lib/grape/middleware/stack.rb, line 95 def concat(other_specs) use, not_use = other_specs.partition { |o| o.first == :use } others << not_use merge_with(use) end
@description Add middlewares with :use operation to the stack. Store others with :insert_* operation for later @param [Array] other_specs An array of middleware specifications (e.g. [[:use, klass], [:insert_before, *args]])
Source
# File lib/grape/middleware/stack.rb, line 55 def insert(index, klass, *args, &block) index = assert_index(index, :before) middlewares.insert(index, self.class::Middleware.new(klass, args, block)) end
Also aliased as: insert_before
Source
# File lib/grape/middleware/stack.rb, line 62 def insert_after(index, *args, &block) index = assert_index(index, :after) insert(index + 1, *args, &block) end
Source
# File lib/grape/middleware/stack.rb, line 72 def merge_with(middleware_specs) middleware_specs.each do |operation, klass, *args| if args.last.is_a?(Proc) last_proc = args.pop public_send(operation, klass, *args, &last_proc) else public_send(operation, klass, *args) end end end
Source
# File lib/grape/middleware/stack.rb, line 67 def use(klass, *args, &block) middleware = self.class::Middleware.new(klass, args, block) middlewares.push(middleware) end
Protected Instance Methods
Source
# File lib/grape/middleware/stack.rb, line 103 def assert_index(index, where) i = index.is_a?(Integer) ? index : middlewares.index(index) i || raise("No such middleware to insert #{where}: #{index.inspect}") end