Class: Grape::Middleware::Stack

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/grape/middleware/stack.rb

Overview

Class to handle the stack of middlewares based on ActionDispatch::MiddlewareStack It allows to insert and insert after

Defined Under Namespace

Classes: Middleware

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStack

Returns a new instance of Stack.


52
53
54
55
# File 'lib/grape/middleware/stack.rb', line 52

def initialize
  @middlewares = []
  @others = []
end

Instance Attribute Details

#middlewaresObject

Returns the value of attribute middlewares


50
51
52
# File 'lib/grape/middleware/stack.rb', line 50

def middlewares
  @middlewares
end

#othersObject

Returns the value of attribute others


50
51
52
# File 'lib/grape/middleware/stack.rb', line 50

def others
  @others
end

Instance Method Details

#[](i) ⇒ Object


69
70
71
# File 'lib/grape/middleware/stack.rb', line 69

def [](i)
  middlewares[i]
end

#build(builder = Rack::Builder.new) ⇒ Rack::Builder

Returns the builder object with our middlewares applied.

Returns:

  • (Rack::Builder)

    the builder object with our middlewares applied


103
104
105
106
107
108
109
# File 'lib/grape/middleware/stack.rb', line 103

def build(builder = Rack::Builder.new)
  others.shift(others.size).each(&method(:merge_with))
  middlewares.each do |m|
    m.use_in(builder)
  end
  builder
end

#concat(other_specs) ⇒ Object

Parameters:

  • other_specs (Array)

    An array of middleware specifications (e.g. [[:use, klass], [:insert_before, *args]])


113
114
115
116
# File 'lib/grape/middleware/stack.rb', line 113

def concat(other_specs)
  @others << Array(other_specs).reject { |o| o.first == :use }
  merge_with(Array(other_specs).select { |o| o.first == :use })
end

#eachObject


57
58
59
# File 'lib/grape/middleware/stack.rb', line 57

def each
  @middlewares.each { |x| yield x }
end

#insert(index, *args, &block) ⇒ Object Also known as: insert_before


73
74
75
76
77
# File 'lib/grape/middleware/stack.rb', line 73

def insert(index, *args, &block)
  index = assert_index(index, :before)
  middleware = self.class::Middleware.new(*args, &block)
  middlewares.insert(index, middleware)
end

#insert_after(index, *args, &block) ⇒ Object


81
82
83
84
# File 'lib/grape/middleware/stack.rb', line 81

def insert_after(index, *args, &block)
  index = assert_index(index, :after)
  insert(index + 1, *args, &block)
end

#lastObject


65
66
67
# File 'lib/grape/middleware/stack.rb', line 65

def last
  middlewares.last
end

#merge_with(middleware_specs) ⇒ Object


91
92
93
94
95
96
97
98
99
100
# File 'lib/grape/middleware/stack.rb', line 91

def merge_with(middleware_specs)
  middleware_specs.each do |operation, *args|
    if args.last.is_a?(Proc)
      last_proc = args.pop
      public_send(operation, *args, &last_proc)
    else
      public_send(operation, *args)
    end
  end
end

#sizeObject


61
62
63
# File 'lib/grape/middleware/stack.rb', line 61

def size
  middlewares.size
end

#use(*args, &block) ⇒ Object


86
87
88
89
# File 'lib/grape/middleware/stack.rb', line 86

def use(*args, &block)
  middleware = self.class::Middleware.new(*args, &block)
  middlewares.push(middleware)
end