Class: Protocol::HTTP::Middleware

Inherits:
Methods
  • Object
show all
Defined in:
lib/protocol/http/middleware.rb,
lib/protocol/http/middleware/builder.rb

Overview

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.

Direct Known Subclasses

AcceptEncoding, ContentEncoding

Defined Under Namespace

Modules: HelloWorld, NotFound, Okay Classes: Builder

Constant Summary

Constants inherited from Methods

Protocol::HTTP::Methods::CONNECT, Protocol::HTTP::Methods::DELETE, Protocol::HTTP::Methods::GET, Protocol::HTTP::Methods::HEAD, Protocol::HTTP::Methods::OPTIONS, Protocol::HTTP::Methods::PATCH, Protocol::HTTP::Methods::POST, Protocol::HTTP::Methods::PUT, Protocol::HTTP::Methods::TRACE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Methods

each, valid?

Constructor Details

#initialize(delegate) ⇒ Middleware

Initialize the middleware with the given delegate.



39
40
41
# File 'lib/protocol/http/middleware.rb', line 39

def initialize(delegate)
	@delegate = delegate
end

Instance Attribute Details

#delegateObject (readonly)

Returns the value of attribute delegate.



44
45
46
# File 'lib/protocol/http/middleware.rb', line 44

def delegate
  @delegate
end

Class Method Details

.build(&block) ⇒ Object

Build a middleware application using the given block.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/protocol/http/middleware/builder.rb', line 47

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

.for(&block) ⇒ Object

Convert a block to a middleware delegate.



28
29
30
31
32
33
34
# File 'lib/protocol/http/middleware.rb', line 28

def self.for(&block)
	# Add a close method to the block.
	def block.close
	end
	
	return self.new(block)
end

Instance Method Details

#call(request) ⇒ Object

Call the middleware with the given request. Invokes the call method on the delegate.



52
53
54
# File 'lib/protocol/http/middleware.rb', line 52

def call(request)
	@delegate.call(request)
end

#closeObject

Close the middleware. Invokes the close method on the delegate.



47
48
49
# File 'lib/protocol/http/middleware.rb', line 47

def close
	@delegate.close
end

#The delegate object that is used for passing along requests that are not handled by *this* middleware.=(delegateobjectthatisused) ⇒ Object



44
# File 'lib/protocol/http/middleware.rb', line 44

attr :delegate