Class: ActiveCampaignRb::Faraday::Middleware::Response

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

Overview

Gem specific response middleware for Faraday

Author:

Instance Method Summary collapse

Methods included from TransformHash

transform_array, transform_key, transform_keys, transform_value

Instance Method Details

#debug(env) ⇒ Object



35
36
37
38
# File 'lib/active_campaign_rb/faraday/middleware/response.rb', line 35

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

#formatterObject



40
41
42
43
44
45
# File 'lib/active_campaign_rb/faraday/middleware/response.rb', line 40

def formatter
  @formatter ||= ::Faraday::Logging::Formatter.new(
    logger: LOGGER,
    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



23
24
25
26
# File 'lib/active_campaign_rb/faraday/middleware/response.rb', line 23

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

#parse(body) ⇒ Object



28
29
30
31
32
33
# File 'lib/active_campaign_rb/faraday/middleware/response.rb', line 28

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

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