Sha256: 81ec03b26116686e3251aa1a5d0a8cefc0d7c24f23b449fb626b7cfc925b3028

Contents?: true

Size: 825 Bytes

Versions: 16

Compression:

Stored size: 825 Bytes

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2024, by Samuel Williams.

require_relative '../middleware'

module Protocol
	module HTTP
		class Middleware
			class Builder
				def initialize(default_app = NotFound)
					@use = []
					@app = default_app
				end
				
				def use(middleware, *arguments, **options, &block)
					@use << proc {|app| middleware.new(app, *arguments, **options, &block)}
				end
				
				def run(app)
					@app = app
				end
				
				def to_app
					@use.reverse.inject(@app) {|app, use| use.call(app)}
				end
			end
			
			def self.build(&block)
				builder = Builder.new
				
				if block_given?
					if block.arity == 0
						builder.instance_exec(&block)
					else
						yield builder
					end
				end
				
				return builder.to_app
			end
		end
	end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
protocol-http-0.36.0 lib/protocol/http/middleware/builder.rb
protocol-http-0.35.0 lib/protocol/http/middleware/builder.rb
protocol-http-0.34.0 lib/protocol/http/middleware/builder.rb
protocol-http-0.33.0 lib/protocol/http/middleware/builder.rb
protocol-http-0.32.0 lib/protocol/http/middleware/builder.rb
protocol-http-0.31.0 lib/protocol/http/middleware/builder.rb
protocol-http-0.30.0 lib/protocol/http/middleware/builder.rb
protocol-http-0.29.0 lib/protocol/http/middleware/builder.rb
protocol-http-0.28.2 lib/protocol/http/middleware/builder.rb
protocol-http-0.28.1 lib/protocol/http/middleware/builder.rb
protocol-http-0.28.0 lib/protocol/http/middleware/builder.rb
protocol-http-0.27.0 lib/protocol/http/middleware/builder.rb
protocol-http-0.26.8 lib/protocol/http/middleware/builder.rb
protocol-http-0.26.7 lib/protocol/http/middleware/builder.rb
protocol-http-0.26.6 lib/protocol/http/middleware/builder.rb
protocol-http-0.26.5 lib/protocol/http/middleware/builder.rb