Module: ActiveCampaign::Faraday::Middleware

Defined in:
lib/active_campaign/faraday/middleware.rb,
lib/active_campaign/faraday/middleware/request.rb,
lib/active_campaign/faraday/middleware/response.rb

Overview

Utility functions for managing Faraday middlewares

Author:

Defined Under Namespace

Classes: Request, Response

Class Method Summary collapse

Class Method Details

.add_middleware(faraday, key, options) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/active_campaign/faraday/middleware.rb', line 41

def self.add_middleware(faraday, key, options)
  if options.is_a?(Hash) && options.any?
    faraday.response key, options
  else
    faraday.response key
  end
end

.add_request_middleware(faraday, config = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_campaign/faraday/middleware.rb', line 14

def self.add_request_middleware(faraday, config = {})
  faraday.request :active_campaign,
                  'Accept' => 'application/json',
                  'Content-Type' => 'application/json',
                  'Api-Token' => config[:api_token],
                  'User-Agent' => "ActiveCampaign Ruby Client (v#{ActiveCampaign::VERSION})"

  return unless (middleware = config[:request_middleware])
  return if middleware.none?

  middleware.each do |key, options|
    faraday.request key, options unless key.to_s.empty?
  end
end

.add_response_middleware(faraday, middleware = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/active_campaign/faraday/middleware.rb', line 29

def self.add_response_middleware(faraday, middleware = {})
  faraday.response :active_campaign

  return if middleware.none?

  middleware.each do |key, **options|
    next if key.to_s.empty?

    add_middleware(faraday, key, options)
  end
end