Module: ImageKitIo::Utils::Formatter

Included in:
OptionValidator
Defined in:
lib/imagekitio/utils/formatter.rb

Class Method Summary collapse

Class Method Details

.camel_to_snake(camel_word) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/imagekitio/utils/formatter.rb', line 21

def camel_to_snake(camel_word)
  # convert camel case to snake case
  camel_word.to_s.gsub(/::/, "/")
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr("-", "_")
    .downcase
end

.format_to_json(options, key, expected_class) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/imagekitio/utils/formatter.rb', line 38

def format_to_json(options, key, expected_class)
  options ||= {}
  val = options[key]
  if !val.nil? && val.is_a?(expected_class)
    options[key] = options[key].to_json
  end
  options
end

.request_formatter(data) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/imagekitio/utils/formatter.rb', line 30

def request_formatter(data)
  result = {}
  data.each do |key, val|
    result[snake_to_camel(key.to_s)] = val
  end
  result
end

.snake_to_camel(word) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/imagekitio/utils/formatter.rb', line 8

def snake_to_camel(word)
  word_list = word.split("_")
  result = []
  word_list&.each { |i|
    if i == word_list[0]
      result.push(i)
    else
      result.push(i.capitalize)
    end
  }
  result.join
end