Module: EasyPost::InternalUtilities
- Defined in:
- lib/easypost/internal_utilities.rb
Defined Under Namespace
Modules: Constants, Json, StaticMapper, System
Class Method Summary collapse
-
.build_dict_key(keys) ⇒ Object
Build a dict key from a list of keys.
-
.form_encode_params(hash, parent_keys = [], parent_dict = {}) ⇒ Object
Form-encode a multi-layer dictionary to a one-layer dictionary.
-
.normalize_string_list(lst) ⇒ Object
Normalizes a list of strings.
-
.objects_to_ids(obj) ⇒ Object
Converts an object to an object ID.
-
.to_snake_case(str) ⇒ Object
Convert a string to snake case.
Class Method Details
.build_dict_key(keys) ⇒ Object
Build a dict key from a list of keys. Example: [code, number] -> code
30 31 32 33 34 35 36 37 38 |
# File 'lib/easypost/internal_utilities.rb', line 30 def self.build_dict_key(keys) result = keys[0].to_s keys[1..].each do |key| result += "[#{key}]" end result end |
.form_encode_params(hash, parent_keys = [], parent_dict = {}) ⇒ Object
Form-encode a multi-layer dictionary to a one-layer dictionary.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/easypost/internal_utilities.rb', line 12 def self.form_encode_params(hash, parent_keys = [], parent_dict = {}) result = parent_dict or {} keys = parent_keys or [] hash.each do |key, value| if value.instance_of?(Hash) keys << key result = form_encode_params(value, keys, result) else dict_key = build_dict_key(keys + [key]) result[dict_key] = value end end result end |
.normalize_string_list(lst) ⇒ Object
Normalizes a list of strings.
57 58 59 60 |
# File 'lib/easypost/internal_utilities.rb', line 57 def self.normalize_string_list(lst) lst = lst.is_a?(String) ? lst.split(',') : Array(lst) lst.map(&:to_s).map(&:downcase).map(&:strip) end |
.objects_to_ids(obj) ⇒ Object
Converts an object to an object ID.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/easypost/internal_utilities.rb', line 41 def self.objects_to_ids(obj) case obj when EasyPost::Models::EasyPostObject { id: obj.id } when Hash result = {} obj.each { |k, v| result[k] = objects_to_ids(v) unless v.nil? } result when Array obj.map { |v| objects_to_ids(v) } else obj end end |
.to_snake_case(str) ⇒ Object
Convert a string to snake case
5 6 7 8 9 |
# File 'lib/easypost/internal_utilities.rb', line 5 def self.to_snake_case(str) str.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .downcase end |