Sha256: 7f684fcae49dbaf441412788893787afc0c9f33c617895458e64a1f86a22c9db

Contents?: true

Size: 1.45 KB

Versions: 26

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

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

require_relative 'methods'
require_relative 'headers'
require_relative 'request'
require_relative 'response'

module Protocol
	module HTTP
		# The middleware interface provides a convenient wrapper for implementing HTTP middleware.
		#
		# A middleware instance generally needs to respond to two methods:
		#
		# - `call(request)` -> `response`
		# - `close()`
		#
		# The call method is called for each request. The close method is called when the server is shutting down.
		#
		# You do not need to use the Middleware class to implement middleware. You can implement the interface directly.
		class Middleware < Methods
			# Convert a block to a middleware delegate.
			def self.for(&block)
				def block.close
				end
				
				return self.new(block)
			end
			
			def initialize(delegate)
				@delegate = delegate
			end
			
			attr :delegate
			
			def close
				@delegate.close
			end
			
			def call(request)
				@delegate.call(request)
			end
			
			module Okay
				def self.close
				end
				
				def self.call(request)
					Response[200]
				end
			end
			
			module NotFound
				def self.close
				end
				
				def self.call(request)
					Response[404]
				end
			end
			
			module HelloWorld
				def self.close
				end
				
				def self.call(request)
					Response[200, Headers['content-type' => 'text/plain'], ["Hello World!"]]
				end
			end
		end
	end
end

Version data entries

26 entries across 26 versions & 1 rubygems

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