Sha256: 3f762708bdaffdf5d53fa1adbcb97c59f750875a869f2220a84a131c820207df

Contents?: true

Size: 1 KB

Versions: 4

Compression:

Stored size: 1 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
		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

4 entries across 4 versions & 1 rubygems

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