Sha256: b5c499a46bfae2600d7b4b76322febca80d5b63786a0d81f3aabb6c1ed7e000c

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

require "rack/builder"

module Hanami
  class API
    # Hanami::API middleware stack
    #
    # @since 0.1.0
    # @api private
    module Middleware
      # Middleware stack
      #
      # @since 0.1.0
      # @api private
      class Stack
        # @since 0.1.0
        # @api private
        ROOT_PREFIX = "/"
        private_constant :ROOT_PREFIX

        # @since 0.1.0
        # @api private
        def initialize
          @prefix = ROOT_PREFIX
          @stack = Hash.new { |hash, key| hash[key] = [] }
        end

        # @since 0.1.0
        # @api private
        def use(middleware, args, &blk)
          @stack[@prefix].push([middleware, args, blk])
        end

        # @since 0.1.0
        # @api private
        def with(path)
          prefix = @prefix
          @prefix = path
          yield
        ensure
          @prefix = prefix
        end

        # @since 0.1.0
        # @api private
        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

        # @since 0.1.0
        # @api private
        def each(&blk)
          uniq!
          @stack.each(&blk)
        end

        # @since 0.1.0
        # @api private
        def mapped(builder, prefix, &blk)
          if prefix == ROOT_PREFIX
            builder.instance_eval(&blk)
          else
            builder.map(prefix, &blk)
          end
        end

        private

        # @since 0.1.0
        # @api private
        def uniq!
          @stack.each_value(&:uniq!)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hanami-api-0.1.0 lib/hanami/api/middleware.rb