Class: ActiveCampaign::Faraday::Middleware::Request

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

Overview

Gem specific request middleware for Faraday

Author:

Instance Method Summary collapse

Constructor Details

#initialize(app, headers) ⇒ Request

Returns a new instance of Request.



16
17
18
19
# File 'lib/active_campaign/faraday/middleware/request.rb', line 16

def initialize(app, headers)
  super(app)
  @headers = headers
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/active_campaign/faraday/middleware/request.rb', line 21

def call(env)
  case env.method
  when :post, :patch, :put
    normalize_body(env)
  end

  env[:request_headers].merge!(@headers)

  @app.call(env)
end

#loggerObject



45
46
47
# File 'lib/active_campaign/faraday/middleware/request.rb', line 45

def logger
  @logger ||= Logger.new(STDOUT)
end

#normalize_body(env) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_campaign/faraday/middleware/request.rb', line 32

def normalize_body(env)
  body = env[:request_body]
  if body.key?(:group)
    logger.debug("Using body as is because group #{body}")
  elsif body.key?(:list)
    logger.debug("Using body as is because list #{body}")
  else
    body = transform_keys(body, :camelcase, :lower)
  end

  env[:request_body] = ::Oj.dump(body, mode: :compat)
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)