Module: TreezorConnect::Util
- Defined in:
- lib/treezor_connect/util.rb
Class Method Summary collapse
- .camelize(string, uppercase_first_letter: true) ⇒ Object
- .convert_to_treezor_object(data, opts = {}) ⇒ Object
- .deep_transform_keys(hash) ⇒ Object
- .normalize_params(params) ⇒ Object
- .snake_case(string) ⇒ Object
Class Method Details
.camelize(string, uppercase_first_letter: true) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/treezor_connect/util.rb', line 25 def self.camelize(string, uppercase_first_letter: true) string = if uppercase_first_letter string.sub(/^[a-z\d]*/, &:capitalize) else string.sub(/^(?:(?=\b|[A-Z_])|\w)/, &:downcase) end string.gsub(%r{(?:_|(/))([a-z\d]*)}) do "#{::Regexp.last_match(1)}#{::Regexp.last_match(2).capitalize}" end.gsub('/', '::') end |
.convert_to_treezor_object(data, opts = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/treezor_connect/util.rb', line 40 def self.convert_to_treezor_object(data, opts = {}) case data when Array data.map { |record| convert_to_treezor_object(record, opts) } when Hash TreezorObject.construct_from(data, opts) else data end end |
.deep_transform_keys(hash) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/treezor_connect/util.rb', line 13 def self.deep_transform_keys(hash, &) hash.each_with_object({}) do |(key, value), new_hash| new_key = yield(key) new_hash[new_key] = if value.is_a?(Hash) deep_transform_keys(value, &) else value end end end |
.normalize_params(params) ⇒ Object
7 8 9 10 11 |
# File 'lib/treezor_connect/util.rb', line 7 def self.normalize_params(params) Util.deep_transform_keys(params) do |key| Util.camelize(key.to_s, uppercase_first_letter: false).to_sym end end |
.snake_case(string) ⇒ Object
36 37 38 |
# File 'lib/treezor_connect/util.rb', line 36 def self.snake_case(string) string.gsub(/([a-z])([A-Z])/, '\1_\2').downcase end |