Module: Sink::Keys

Defined in:
lib/sink/keys.rb

Class Method Summary collapse

Class Method Details

.camelize(key) ⇒ Object



15
16
17
18
# File 'lib/sink/keys.rb', line 15

def camelize(key)
  head, *tail = key.to_s.split("_")
  [head, *tail.map(&:capitalize)].join
end

.camelize_keys(hash) ⇒ Object



26
27
28
# File 'lib/sink/keys.rb', line 26

def camelize_keys(hash)
  hash.to_h { |key, value| [camelize(key), value] }
end

.underscore(key) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/sink/keys.rb', line 7

def underscore(key)
  key.to_s
     .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
     .gsub(/([a-z\d])([A-Z])/, '\1_\2')
     .downcase
     .to_sym
end

.underscore_keys(hash) ⇒ Object

Sink payloads are flat, so nested hashes such as geo keep their original keys (country codes) instead of being converted to snake_case.



22
23
24
# File 'lib/sink/keys.rb', line 22

def underscore_keys(hash)
  hash.to_h { |key, value| [underscore(key), value] }
end