class OwApi::Interactors::HashKeysToSnakeCase

Public Instance Methods

call(hash) click to toggle source
# File lib/ow_api/interactors/hash_keys_to_snake_case.rb, line 4
def call(hash)
  convert_hash_keys(hash)
end

Private Instance Methods

convert_hash_keys(value) click to toggle source
# File lib/ow_api/interactors/hash_keys_to_snake_case.rb, line 10
def convert_hash_keys(value)
  case value
  when Array
    value.map { |v| convert_hash_keys(v) }
  when Hash
    Hash[value.map { |k, v| [underscore_key(k), convert_hash_keys(v)] }]
  else
    value
  end
end
underscore_key(key) click to toggle source
# File lib/ow_api/interactors/hash_keys_to_snake_case.rb, line 21
def underscore_key(key)
  key.
    to_s.
    gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end