Sha256: f9e519c4989a96474cf69116ae0baf9acaeff1c701c8b12f06b5f189fa026e60

Contents?: true

Size: 777 Bytes

Versions: 4

Compression:

Stored size: 777 Bytes

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2023, 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, &block)
					@use << proc {|app| middleware.new(app, *arguments, &block)}
				end
				
				ruby2_keywords(:use) if respond_to?(:ruby2_keywords, true)
				
				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
				
				builder.instance_eval(&block)
				
				return builder.to_app
			end
		end
	end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
protocol-http-0.24.3 lib/protocol/http/middleware/builder.rb
protocol-http-0.24.2 lib/protocol/http/middleware/builder.rb
protocol-http-0.24.1 lib/protocol/http/middleware/builder.rb
protocol-http-0.24.0 lib/protocol/http/middleware/builder.rb