Sha256: ce5ffa5774f7cc4d0619e8961be2ff986275c507994ef1c8e7f03d8cef68740e

Contents?: true

Size: 825 Bytes

Versions: 9

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

9 entries across 9 versions & 1 rubygems

Version Path
protocol-http-0.45.0 lib/protocol/http/middleware/builder.rb
protocol-http-0.44.0 lib/protocol/http/middleware/builder.rb
protocol-http-0.43.0 lib/protocol/http/middleware/builder.rb
protocol-http-0.42.0 lib/protocol/http/middleware/builder.rb
protocol-http-0.41.0 lib/protocol/http/middleware/builder.rb
protocol-http-0.40.0 lib/protocol/http/middleware/builder.rb
protocol-http-0.39.0 lib/protocol/http/middleware/builder.rb
protocol-http-0.38.0 lib/protocol/http/middleware/builder.rb
protocol-http-0.37.0 lib/protocol/http/middleware/builder.rb