Class: ActiveCampaign::Faraday::Middleware::Response

Inherits:
Faraday::Response::Middleware
  • Object
show all
Includes:
TransformHash
Defined in:
lib/active_campaign/faraday/middleware/response.rb

Overview

Gem specific response middleware for Faraday

Author:

Instance Method Summary collapse

Instance Method Details

#debug(env) ⇒ Object



33
34
35
36
# File 'lib/active_campaign/faraday/middleware/response.rb', line 33

def debug(env)
  formatter.request(env)
  formatter.response(env)
end

#formatterObject



38
39
40
41
42
43
# File 'lib/active_campaign/faraday/middleware/response.rb', line 38

def formatter
  @formatter ||= ::Faraday::Logging::Formatter.new(
    logger: ::Logger.new(STDOUT),
    options: { headers: true, bodies: true }
  )
end

#on_complete(env) ⇒ Object

Override this to modify the environment after the response has finished. Calls the parse method if defined



21
22
23
24
# File 'lib/active_campaign/faraday/middleware/response.rb', line 21

def on_complete(env)
  env.body = parse(env.body)
  debug(env) if ActiveCampaign.config.debug?
end

#parse(body) ⇒ Object



26
27
28
29
30
31
# File 'lib/active_campaign/faraday/middleware/response.rb', line 26

def parse(body)
  return body if body.to_s.empty?

  body = ::Oj.load(body, mode: :compat)
  transform_keys(body, :underscore)
end

#transform_key(key, *new_case) ⇒ Symbol Originally defined in module TransformHash

Transform the provided keys case and lastly symbolize it

Parameters:

  • key (String, Symbol)

    the name of the key to change case

  • new_case (Symbol, Symbol)

    the new case eg. :underscore or :camelcase, :lower

Returns:

  • (Symbol)

    the transformed key

#transform_keys(hash, *new_case) ⇒ Hash Originally defined in module TransformHash

Note:

this is used to always output a hash response

Transforms case of all hash keys

Parameters:

  • hash (Hash)

    initial hash before transformation

  • new_case (Symbol, Symbol)

    the new case eg. :underscore or :camelcase, :lower

Returns:

  • (Hash)

#transform_value(value, *new_case) ⇒ Object Originally defined in module TransformHash

Note:

used for nested values like hashes and arrays

Transform all values

Parameters:

  • value (Object)

    the value to transform

  • new_case (Symbol, Symbol)

    the new case eg. :underscore or :camelcase, :lower

Returns:

  • (Object)