Module: ActiveCampaign::TransformHash
- Included in:
- Client, Faraday::Middleware::Request, Faraday::Middleware::Response
- Defined in:
- lib/active_campaign/transform_hash.rb
Overview
Utility module for manipulating hashes, arrays and values
Instance Method Summary collapse
-
#transform_key(key, *new_case) ⇒ Symbol
Transform the provided keys case and lastly symbolize it.
-
#transform_keys(hash, *new_case) ⇒ Hash
Transforms case of all hash keys.
-
#transform_value(value, *new_case) ⇒ Object
Transform all values.
Instance Method Details
#transform_key(key, *new_case) ⇒ Symbol
Transform the provided keys case and lastly symbolize it
35 36 37 |
# File 'lib/active_campaign/transform_hash.rb', line 35 def transform_key(key, *new_case) key.to_s.public_send(*new_case).to_sym end |
#transform_keys(hash, *new_case) ⇒ Hash
Note:
this is used to always output a hash response
Transforms case of all hash keys
21 22 23 24 25 |
# File 'lib/active_campaign/transform_hash.rb', line 21 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
Note:
used for nested values like hashes and arrays
Transform all values
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/active_campaign/transform_hash.rb', line 48 def transform_value(value, *new_case) case value when Hash transform_keys(value, *new_case) when Array value.map { |item| transform_keys(item, *new_case) } else value end end |