Module: XeroRuby::StringSerialization

Included in:
ApiClient, Where
Defined in:
lib/xero-ruby/string_serialization.rb

Instance Method Summary collapse

Instance Method Details

#camelize(word, first_upper = true) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/xero-ruby/string_serialization.rb', line 25

def camelize(word, first_upper = true)
  if first_upper
    str = word.to_s
    str = gsubbed(str, /(?:^|_)([^_\s]+)/)
    str = gsubbed(str, %r{/([^/]*)}, "::")
    str
  else
    parts = word.split("_", 2)
    parts[0] << camelize(parts[1]) if parts.size > 1
    parts[0] || ""
  end
end

#camelize_key(key, first_upper = true) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/xero-ruby/string_serialization.rb', line 15

def camelize_key(key, first_upper = true)
  if key.is_a? Symbol
    camelize(key.to_s, first_upper).to_sym
  elsif key.is_a? String
    camelize(key, first_upper)
  else
    key # can't camelize anything except strings and symbols
  end
end

#capitalize_first(word) ⇒ Object



46
47
48
49
# File 'lib/xero-ruby/string_serialization.rb', line 46

def capitalize_first(word)
  split = word.split('')
  "#{split[0].capitalize}#{split[1..-1].join}"
end

#gsubbed(str, pattern, extra = "") ⇒ Object



38
39
40
41
42
43
44
# File 'lib/xero-ruby/string_serialization.rb', line 38

def gsubbed(str, pattern, extra = "")
  key_map_scronyms = {}
  str = str.gsub(pattern) do
    extra + (key_map_scronyms[Regexp.last_match(1)] || capitalize_first(Regexp.last_match(1)))
  end
  str
end

#to_camel_keys(value = self) ⇒ Object

START - Re-serializes snake_cased params to PascalCase required by XeroAPI



4
5
6
7
8
9
10
11
12
13
# File 'lib/xero-ruby/string_serialization.rb', line 4

def to_camel_keys(value = self)
  case value
  when Array
    value.map { |v| to_camel_keys(v) }
  when Hash
    Hash[value.map { |k, v| [camelize_key(k), to_camel_keys(v)] }]
  else
    value
  end
end