Module: ActiveCampaignWrapper::Helpers

Included in:
Configuration
Defined in:
lib/active_campaign_wrapper/helpers.rb

Class Method Summary collapse

Class Method Details

.handle_errors(response) ⇒ Object

Raises:

  • (class_name.new(response))


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/active_campaign_wrapper/helpers.rb', line 88

def handle_errors(response)
  return if response.success?

  class_name = if response.forbidden?
                 ActiveCampaignWrapper::Forbidden
               elsif response.not_found?
                 ActiveCampaignWrapper::NotFound
               elsif response.unprocessable_entity?
                 ActiveCampaignWrapper::UnprocessableEntity
               elsif response.too_many_requests?
                 ActiveCampaignWrapper::TooManyRequests
               else
                 ActiveCampaignWrapper::Error
               end
  raise class_name.new(response),
        response['message'] || response['errors']&.join(', ') || response['error'],
        caller
end

.normalize_body(params, skip_normalization = []) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/active_campaign_wrapper/helpers.rb', line 20

def normalize_body(params, skip_normalization = [])
  return unless params.is_a?(Hash)

  params = transform_keys(
    params || {},
    %i[camelcase lower],
    skip_normalization
  )
  params.to_json
end

.normalize_response(response) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/active_campaign_wrapper/helpers.rb', line 10

def normalize_response(response)
  handle_errors(response)

  if response&.body.present?
    transform_keys(response, [:underscore])
  else
    {}
  end
end

.transform_array(collection, case_style, skip_normalization = []) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/active_campaign_wrapper/helpers.rb', line 77

def transform_array(collection, case_style, skip_normalization = [])
  collection.map do |element|
    case element
    when Hash
      transform_keys(element, case_style, skip_normalization)
    else
      element
    end
  end
end

.transform_key(key, case_style, skip_normalization = []) ⇒ Symbol

Transform the provided keys case and lastly symbolize it

Parameters:

  • key (String, Symbol)

    the name of the key to change case

  • [:underscore] (Symbol, Symbol)

    or [:camelcase, :lower] DEFAULT: underscore

Returns:

  • (Symbol)

    the transformed key



51
52
53
54
55
56
# File 'lib/active_campaign_wrapper/helpers.rb', line 51

def transform_key(key, case_style, skip_normalization = [])
  unless skip_normalization.include?(key)
    key = key.to_s.public_send(*case_style)
  end
  key.to_sym
end

.transform_keys(hash, case_style, skip_normalization = []) ⇒ Hash

Note:

this is used to always output a hash response

Transforms case of all hash keys

Parameters:

  • hash (Hash)

    initial hash before transformation

  • [:underscore] (Symbol, Symbol)

    or [:camelcase, :lower] DEFAULT: underscore

Returns:

  • (Hash)


38
39
40
41
42
# File 'lib/active_campaign_wrapper/helpers.rb', line 38

def transform_keys(hash, case_style, skip_normalization = [])
  hash.each_with_object({}) do |(key, value), memo|
    memo[transform_key(key, case_style, skip_normalization)] = transform_value(value, case_style, skip_normalization)
  end
end

.transform_value(value, case_style, skip_normalization = []) ⇒ Object

Note:

used for nested values like hashes and arrays

Transform all values

Parameters:

  • value (Object)

    the value to transform

  • [:underscore] (Symbol, Symbol)

    or [:camelcase, :lower] DEFAULT: underscore

Returns:

  • (Object)


66
67
68
69
70
71
72
73
74
75
# File 'lib/active_campaign_wrapper/helpers.rb', line 66

def transform_value(value, case_style, skip_normalization = [])
  case value
  when Hash
    transform_keys(value, case_style, skip_normalization)
  when Array
    transform_array(value, case_style, skip_normalization)
  else
    value
  end
end