Module: Bakong::OpenApi::Helpers::CaseHelper

Defined in:
lib/bakong/open_api/helpers/case_helper.rb

Overview

Convert between Ruby’s snake_case (used everywhere in this gem’s public API) and the Bakong API’s camelCase wire format. Recursive on hashes and arrays.

Class Method Summary collapse

Class Method Details

.camel_to_snake(string) ⇒ Object



41
42
43
44
45
46
# File 'lib/bakong/open_api/helpers/case_helper.rb', line 41

def camel_to_snake(string)
  string
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .downcase
end

.snake_to_camel(string) ⇒ Object



36
37
38
39
# File 'lib/bakong/open_api/helpers/case_helper.rb', line 36

def snake_to_camel(string)
  parts = string.split("_")
  ([parts.first] + parts.drop(1).map(&:capitalize)).join
end

.to_camel(value) ⇒ Object

snake_case symbol/string keys → camelCase symbol keys for requests.



13
14
15
16
17
18
19
20
21
22
# File 'lib/bakong/open_api/helpers/case_helper.rb', line 13

def to_camel(value)
  case value
  when Hash
    value.each_with_object({}) { |(k, v), out| out[snake_to_camel(k.to_s).to_sym] = to_camel(v) }
  when Array
    value.map { |v| to_camel(v) }
  else
    value
  end
end

.to_snake(value) ⇒ Object

camelCase keys → snake_case symbol keys for responses.



25
26
27
28
29
30
31
32
33
34
# File 'lib/bakong/open_api/helpers/case_helper.rb', line 25

def to_snake(value)
  case value
  when Hash
    value.each_with_object({}) { |(k, v), out| out[camel_to_snake(k.to_s).to_sym] = to_snake(v) }
  when Array
    value.map { |v| to_snake(v) }
  else
    value
  end
end