Module: NetHttpWrapper::DSL
- Included in:
- NetHttpWrapper
- Defined in:
- lib/net_http_wrapper.rb
Overview
Defines methods to be available on NetHttpWrapper class.
Instance Method Summary collapse
-
#after_request(&block) ⇒ Object
Registers a callback that will be called upon request completion.
-
#after_request_callbacks ⇒ Object
List of registered callbacks to be executed upon the request completion.
- #disable ⇒ Object
- #enable ⇒ Object
- #enabled? ⇒ Boolean
Instance Method Details
#after_request(&block) ⇒ Object
Registers a callback that will be called upon request completion.
Callback will receive following parameters:
-
http: HTTP instance,
-
request: HTTP Request,
-
response: HTTP Response,
-
start_time: Request Start Time
Example:
NetHttpWrapper.after_request do |http:, request:, response:, start_time:|
request_duration = (Time.now - start_time).round(3)
request_url =
URI.decode("http#{"s" if http.use_ssl?}://#{http.address}:#{http.port}#{request.path}")
Rails.logger.info(method: request.method,
url: request_url,
status: response.code,
duration: request_duration)
end
43 44 45 |
# File 'lib/net_http_wrapper.rb', line 43 def after_request(&block) after_request_callbacks << block end |
#after_request_callbacks ⇒ Object
List of registered callbacks to be executed upon the request completion.
48 49 50 |
# File 'lib/net_http_wrapper.rb', line 48 def after_request_callbacks @after_request_callbacks ||= [] end |
#disable ⇒ Object
16 17 18 |
# File 'lib/net_http_wrapper.rb', line 16 def disable @enabled = false end |
#enable ⇒ Object
11 12 13 14 |
# File 'lib/net_http_wrapper.rb', line 11 def enable Net::HTTP.prepend NetHttpWrapper @enabled = true end |
#enabled? ⇒ Boolean
20 21 22 |
# File 'lib/net_http_wrapper.rb', line 20 def enabled? @enabled end |