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


37
38
39
40
# File 'lib/grape/middleware/stack.rb', line 37

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

Instance Attribute Details

#middlewaresObject

Returns the value of attribute middlewares


35
36
37
# File 'lib/grape/middleware/stack.rb', line 35

def middlewares
  @middlewares
end

#othersObject

Returns the value of attribute others


35
36
37
# File 'lib/grape/middleware/stack.rb', line 35

def others
  @others
end

Instance Method Details

#[](i) ⇒ Object


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

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


87
88
89
90
91
92
93
# File 'lib/grape/middleware/stack.rb', line 87

def build(builder = Rack::Builder.new)
  others.shift(others.size).each(&method(:merge_with))
  middlewares.each do |m|
    m.block ? builder.use(m.klass, *m.args, &m.block) : builder.use(m.klass, *m.args)
  end
  builder
end

#concat(other_specs) ⇒ Object

Parameters:

  • other_specs (Array)

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


97
98
99
100
# File 'lib/grape/middleware/stack.rb', line 97

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


42
43
44
# File 'lib/grape/middleware/stack.rb', line 42

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

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


58
59
60
61
62
# File 'lib/grape/middleware/stack.rb', line 58

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


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

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

#lastObject


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

def last
  middlewares.last
end

#merge_with(middleware_specs) ⇒ Object


76
77
78
79
80
81
82
83
84
# File 'lib/grape/middleware/stack.rb', line 76

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

#sizeObject


46
47
48
# File 'lib/grape/middleware/stack.rb', line 46

def size
  middlewares.size
end

#use(*args, &block) ⇒ Object


71
72
73
74
# File 'lib/grape/middleware/stack.rb', line 71

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