README.md in net-http-middleware-0.1.0 vs README.md in net-http-middleware-0.2.0
- old
+ new
@@ -2,37 +2,43 @@
Middlewares for Net::HTTP request method.
## Usage
+There are couple of requirements for middleware to be implemented
+1. Chain as a first argument for initializer of your middleware
+2. 2 params for call: Request-object and body to be passed to call
+3. Return the result of @chain.call(req, body)
+
```ruby
# Example of tracer, which will add header to each outgoing request
class OpenTraceId
def initialize(chain)
@chain = chain
end
- def call(req, body = nil, &block)
+ def call(req, body = nil)
request.add_field('X-Custom-CrossService-RequestId', SecureRandom.uuid)
- @chain.call(req, body, block)
+ @chain.call(req, body)
end
end
# Example of Loging for
class LogRequest
def initialize(chain, logger)
@chain = chain
@logger = logger
end
- def call(req, body = nil, &block)
+ def call(req, body = nil)
@logger.info('We log before')
- response = @chain.call(req, body, block)
+ response = @chain.call(req, body)
@logger.info('We log after')
response
end
end
+require 'net/http/middleware'
# Adding middlewares to configurator
Net::HTTP.configure_middleware do |chain|
chain.use LogRequest, Logger.new(STDOUT)
chain.use OpenTraceId
end