Module: ApiUtils

Defined in:
lib/api_utils.rb,
lib/api_utils/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.camelize(underscored_word) ⇒ String

Convert a string from “something_like_this” to “somethingLikeThis”

Parameters:

  • underscored_word (String)

Returns:

  • (String)


9
10
11
# 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 }) ⇒ Hash

camelize all hash keys

Parameters:

  • hash (Hash)
  • handler (Proc) (defaults to: ->(val) { val })

    Value will be result of handler

Returns:

  • (Hash)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Hash

symbolize all hash keys

Parameters:

  • hash (Hash)

Returns:

  • (Hash)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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