Class: ActiveCampaign::Faraday::Middleware::Request
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- ActiveCampaign::Faraday::Middleware::Request
- Includes:
- TransformHash
- Defined in:
- lib/active_campaign/faraday/middleware/request.rb
Overview
Gem specific request middleware for Faraday
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, headers) ⇒ Request
constructor
A new instance of Request.
- #logger ⇒ Object
- #normalize_body(env) ⇒ Object
-
#transform_key(key, *new_case) ⇒ Symbol
included
from TransformHash
Transform the provided keys case and lastly symbolize it.
-
#transform_keys(hash, *new_case) ⇒ Hash
included
from TransformHash
Transforms case of all hash keys.
-
#transform_value(value, *new_case) ⇒ Object
included
from TransformHash
Transform all values.
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 |
#logger ⇒ Object
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
#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
#transform_value(value, *new_case) ⇒ Object Originally defined in module TransformHash
Note:
used for nested values like hashes and arrays
Transform all values