Class: Hanami::API::Middleware::Stack Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/api/middleware.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Middleware stack

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initializeStack

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Stack.

Since:

  • 0.1.0



24
25
26
27
# File 'lib/hanami/api/middleware.rb', line 24

def initialize
  @prefix = ROOT_PREFIX
  @stack = Hash.new { |hash, key| hash[key] = [] }
end

Instance Method Details

#each(&blk) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



68
69
70
71
# File 'lib/hanami/api/middleware.rb', line 68

def each(&blk)
  uniq!
  @stack.each(&blk)
end

#finalize(app) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hanami/api/middleware.rb', line 47

def finalize(app) # rubocop:disable Metrics/MethodLength
  uniq!
  return app if @stack.empty?

  s = self

  Rack::Builder.new do
    s.each do |prefix, stack|
      s.mapped(self, prefix) do
        stack.each do |middleware, args, blk|
          use(middleware, *args, &blk)
        end
      end

      run app
    end
  end
end

#mapped(builder, prefix, &blk) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



75
76
77
78
79
80
81
# File 'lib/hanami/api/middleware.rb', line 75

def mapped(builder, prefix, &blk)
  if prefix == ROOT_PREFIX
    builder.instance_eval(&blk)
  else
    builder.map(prefix, &blk)
  end
end

#use(middleware, args, &blk) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



31
32
33
# File 'lib/hanami/api/middleware.rb', line 31

def use(middleware, args, &blk)
  @stack[@prefix].push([middleware, args, blk])
end

#with(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



37
38
39
40
41
42
43
# File 'lib/hanami/api/middleware.rb', line 37

def with(path)
  prefix = @prefix
  @prefix = path
  yield
ensure
  @prefix = prefix
end