Module: Boldsign::CaseConvert

Defined in:
lib/boldsign/case_convert.rb

Overview

Converts arbitrary Ruby hash keys (snake_case or camelCase, Symbol or String) to PascalCase string keys recursively, matching the casing the BoldSign REST API expects in request bodies. Non-Hash and non-Array values — including binary IO objects, FilePart instances, scalars, dates, etc. —pass through untouched.

Class Method Summary collapse

Class Method Details

.capitalize_word(str) ⇒ Object



37
38
39
40
41
# File 'lib/boldsign/case_convert.rb', line 37

def capitalize_word(str)
  return str if str.empty?

  str[0].upcase + str[1..]
end

.pascalize(obj) ⇒ Object

Recursively PascalCase every Hash key reachable from ‘obj`.

Parameters:

  • obj (Object)

Returns:

  • (Object)

    new object with the same shape and PascalCase keys



13
14
15
16
17
18
19
20
21
22
# File 'lib/boldsign/case_convert.rb', line 13

def pascalize(obj)
  case obj
  when Hash
    obj.each_with_object({}) { |(k, v), acc| acc[pascalize_key(k)] = pascalize(v) }
  when Array
    obj.map { |v| pascalize(v) }
  else
    obj
  end
end

.pascalize_key(key) ⇒ String

Returns PascalCase string.

Parameters:

  • key (String, Symbol)

Returns:

  • (String)

    PascalCase string



26
27
28
29
30
31
32
33
34
35
# File 'lib/boldsign/case_convert.rb', line 26

def pascalize_key(key)
  str = key.to_s
  return str if str.empty?

  if str.include?("_")
    str.split("_").map { |part| capitalize_word(part) }.join
  else
    capitalize_word(str)
  end
end