Class: Faraday::ParseDates::Middleware

Inherits:
Middleware
  • Object
show all
Defined in:
lib/faraday/parse_dates/middleware.rb

Overview

This class provides the main implementation for your middleware. Your middleware can implement any of the following methods:

  • on_request - called when the request is being prepared

  • on_complete - called when the response is being processed

Optionally, you can also override the following methods from Faraday::Middleware

  • initialize(app, options = {}) - the initializer method

  • call(env) - the main middleware invocation method. This already calls on_request and on_complete, so you normally don’t need to override it. You may need to in case you need to “wrap” the request or need more control (see “retry” middleware: github.com/lostisland/faraday/blob/main/lib/faraday/request/retry.rb#L142). IMPORTANT: Remember to call ‘@app.call(env)` or `super` to not interrupt the middleware chain!

Constant Summary collapse

ISO_DATE_FORMAT =
/\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|((\+|-)\d{2}:?\d{2}))\Z/xm.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



22
23
24
25
# File 'lib/faraday/parse_dates/middleware.rb', line 22

def initialize(app, options = {})
  @regexp = options[:match] || ISO_DATE_FORMAT
  super(app)
end

Instance Method Details

#on_complete(env) ⇒ Object

This method will be called when the response is being processed. You can alter it as you like, accessing things like response_body, response_headers, and more. Refer to Faraday::Env for a list of accessible fields: github.com/lostisland/faraday/blob/main/lib/faraday/options/env.rb

Parameters:

  • env (Faraday::Env)

    the environment of the response being processed.



33
34
35
# File 'lib/faraday/parse_dates/middleware.rb', line 33

def on_complete(env)
  parse_dates! env[:body]
end