Module: MangoPay::JsonTagConverter
- Defined in:
- lib/mangopay/common/json_tag_converter.rb
Overview
Converts field names between Ruby and API formats.
Class Method Summary collapse
-
.from_json_tag(tag) ⇒ Object
Converts an API-returned UpperCamelCase-named JSON tag to its Ruby-standard snake_case counterpart.
-
.to_json_tag(field) ⇒ Object
Converts a snake_case-named field name to its API UpperCamelCase counterpart.
Class Method Details
.from_json_tag(tag) ⇒ Object
Converts an API-returned UpperCamelCase-named JSON tag to its Ruby-standard snake_case counterpart.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/mangopay/common/json_tag_converter.rb', line 17 def from_json_tag(tag) tag = tag.sub('UBO', 'Ubo').sub('AVS', 'Avs') parts = tag.split(/(?=[A-Z])/) parts = compress_upcase_strings(parts) field = '' parts.each.with_index do |part, index| decapitalize! part field << '_' if !field.empty? && (part.length > 1\ || (part == 'e' && parts[index + 1] == 'Money')) field << part end field end |
.to_json_tag(field) ⇒ Object
Converts a snake_case-named field name to its API UpperCamelCase counterpart.
9 10 11 12 13 |
# File 'lib/mangopay/common/json_tag_converter.rb', line 9 def to_json_tag(field) field.split('_').collect do |word| apply_capitalization! word end.join end |