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.



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

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

Instance Attribute Details

#middlewaresObject

Returns the value of attribute middlewares.



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

def middlewares
  @middlewares
end

#othersObject

Returns the value of attribute others.



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

def others
  @others
end

Instance Method Details

#[](i) ⇒ Object



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

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



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

def build(builder = Rack::Builder.new)
  others.shift(others.size).each { |m| merge_with(m) }
  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]])



107
108
109
110
# File 'lib/grape/middleware/stack.rb', line 107

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

#each(&block) ⇒ Object



48
49
50
# File 'lib/grape/middleware/stack.rb', line 48

def each(&block)
  @middlewares.each(&block)
end

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



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

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



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

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

#lastObject



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

def last
  middlewares.last
end

#merge_with(middleware_specs) ⇒ Object



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

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



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

def size
  middlewares.size
end

#use(*args, &block) ⇒ Object



79
80
81
82
# File 'lib/grape/middleware/stack.rb', line 79

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