module ApiUtils

Constants

VERSION

Public Instance Methods

camelize(underscored_word) click to toggle source

Convert a string from “something_like_this” to “somethingLikeThis” @param [String] underscored_word @return [String]

# File lib/api_utils.rb, line 9
def camelize(underscored_word)
  underscored_word.gsub(/(?:_)(.)/) { Regexp.last_match(1).upcase }
end
camelize_keys(hash, handler = ->(val) { val } click to toggle source

camelize all hash keys @param [Hash] hash @return [Hash] @param [Proc] handler Value will be result of handler

# File lib/api_utils.rb, line 17
def camelize_keys(hash, handler = ->(val) { val })
  hash.each.with_object({}) do |(key, val), obj|
    obj[camelize(key.to_s)] =
      case val
      when Hash
        camelize_keys(val)
      when Array
        val.map do |item|
          item.instance_of?(Hash) ? camelize_keys(item) : handler.call(item)
        end
      else
        handler.call(val)
      end
  end
end
symbolize_keys(hash) click to toggle source

symbolize all hash keys @param [Hash] hash @return [Hash]

# File lib/api_utils.rb, line 36
def symbolize_keys(hash) # rubocop:disable Metrics/MethodLength
  hash.each.with_object({}) do |(key, val), obj|
    obj[key.to_sym] =
      case val
      when Hash
        symbolize_keys(val)
      when Array
        val.map do |item|
          item.instance_of?(Hash) ? symbolize_keys(item) : item
        end
      else
        val
      end
  end
end