Module: ActiveCampaignRb::TransformHash

Included in:
Faraday::Middleware::Request, Faraday::Middleware::Response
Defined in:
lib/active_campaign_rb/transform_hash.rb

Overview

Utility methods

Class Method Summary collapse

Class Method Details

.transform_array(collection, *new_case) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/active_campaign_rb/transform_hash.rb', line 33

def transform_array(collection, *new_case)
  collection.map do |element|
    case element
    when Hash
      transform_keys(element, *new_case)
    else
      element
    end
  end
end

.transform_key(key, *new_case) ⇒ Object



18
19
20
# File 'lib/active_campaign_rb/transform_hash.rb', line 18

def transform_key(key, *new_case)
  key.to_s.public_send(*new_case).to_sym
end

.transform_keys(hash, *new_case) ⇒ Object



12
13
14
15
16
# File 'lib/active_campaign_rb/transform_hash.rb', line 12

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

.transform_value(value, *new_case) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/active_campaign_rb/transform_hash.rb', line 22

def transform_value(value, *new_case)
  case value
  when Hash
    transform_keys(value, *new_case)
  when Array
    transform_array(value, *new_case)
  else
    value
  end
end