module EasyPost::InternalUtilities

Public Class Methods

build_dict_key(keys) click to toggle source

Build a dict key from a list of keys. Example: [code, number] -> code

# 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 = {}) click to toggle source

Form-encode a multi-layer dictionary to a one-layer dictionary.

# 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) click to toggle source

Normalizes a list of strings.

# 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) click to toggle source

Converts an object to an object ID.

# 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) click to toggle source

Convert a string to snake case

# 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