module AgnosticBackend::Utilities::InstanceMethods

Public Instance Methods

convert_bool_values_to_string_in(document) click to toggle source
# File lib/agnostic_backend/utilities.rb, line 125
def convert_bool_values_to_string_in(document)
  document.each do |k, v|
    if v.is_a?(TrueClass)
      document[k] = 'true'
    elsif v.is_a?(FalseClass)
      document[k] = 'false'
    end
  end
end
convert_to(type, value) click to toggle source
# File lib/agnostic_backend/utilities.rb, line 135
def convert_to(type, value)
  case type
  when :integer
    if value.is_a?(Fixnum)
      value
    elsif is_integer?(value)
      value.to_i
    else
      value
    end
  when :date, :date_array
    if value.is_a?(Time)
      value
    elsif is_date?(value)
      value.to_time.utc
    else
      value
    end
  when :double
    if value.is_a?(Float)
      value
    elsif is_float?(value)
      value.to_f
    else
      value
    end
  when :boolean
    if value.is_a?(TrueClass) || value.is_a?(FalseClass)
      value
    elsif is_boolean?(value)
      convert_to_boolean(value)
    else
      value
    end
  when :string,:string_array,:text,:text_array
    if value.is_a?(String)
      value
    else
      value.to_s
    end
  else
    value
  end
end
convert_to_boolean(value) click to toggle source
# File lib/agnostic_backend/utilities.rb, line 196
def convert_to_boolean(value)
  case value
  when 'true'
    true
  when 'false'
    false
  end
end
exponential_backoff_base() click to toggle source
# File lib/agnostic_backend/utilities.rb, line 38
def exponential_backoff_base
  @exponential_backoff_max_time ||= 0.2
end
exponential_backoff_max_attempts() click to toggle source
# File lib/agnostic_backend/utilities.rb, line 34
def exponential_backoff_max_attempts
  @exponential_backoff_max_time ||= 10
end
exponential_backoff_max_time() click to toggle source
# File lib/agnostic_backend/utilities.rb, line 30
def exponential_backoff_max_time
  @exponential_backoff_max_time ||= 4
end
exponential_backoff_sleep_time(max, base, attempts) click to toggle source
# File lib/agnostic_backend/utilities.rb, line 42
def exponential_backoff_sleep_time(max, base, attempts)
  temp = [max, base * 2 ** attempts].min.to_f
  temp/2 + rand(0.0...temp/2)
end
flat_hash(document, delimiter) click to toggle source
# File lib/agnostic_backend/utilities.rb, line 48
def flat_hash(document, delimiter)
  flatten_doc = {}
  document.each do |k, v|
    if v.is_a? Hash
      # if we have a nested hash, traverse the hash until we find a value
      # When a value is found, we return it and merge the keys with the key of the previous recursion iteration
      flat_hash(v, delimiter).map do |doc_k, doc_v|
        flatten_doc["#{k}"+ delimiter + "#{doc_k}"] = doc_v
      end
    else
      flatten_doc[k.to_s] = v
    end
  end

  return flatten_doc
end
flatten(document, delimiter = '__') click to toggle source
# File lib/agnostic_backend/utilities.rb, line 47
def flatten(document, delimiter = '__')
  def flat_hash(document, delimiter)
    flatten_doc = {}
    document.each do |k, v|
      if v.is_a? Hash
        # if we have a nested hash, traverse the hash until we find a value
        # When a value is found, we return it and merge the keys with the key of the previous recursion iteration
        flat_hash(v, delimiter).map do |doc_k, doc_v|
          flatten_doc["#{k}"+ delimiter + "#{doc_k}"] = doc_v
        end
      else
        flatten_doc[k.to_s] = v
      end
    end

    return flatten_doc
  end

  flat_hash(document, delimiter)
end
is_boolean?(value) click to toggle source
# File lib/agnostic_backend/utilities.rb, line 188
def is_boolean?(value)
  value == 'true' || value == 'false'
end
is_date?(value) click to toggle source
# File lib/agnostic_backend/utilities.rb, line 192
def is_date?(value)
  value.to_time.present? rescue false
end
is_float?(value) click to toggle source
# File lib/agnostic_backend/utilities.rb, line 184
def is_float?(value)
  (/^[-+]?(\d*[.])?\d+$/ =~ value.to_s).present?
end
is_integer?(value) click to toggle source
# File lib/agnostic_backend/utilities.rb, line 180
def is_integer?(value)
  (/^[-+]?\d+$/ =~ value.to_s).present?
end
reject_blank_values_from(flat_document) click to toggle source
# File lib/agnostic_backend/utilities.rb, line 121
def reject_blank_values_from(flat_document)
  flat_document.reject { |_, v| (v.is_a?(FalseClass) ? false : v.blank?) }
end
transform_nested_values(hash, proc) click to toggle source
# File lib/agnostic_backend/utilities.rb, line 92
def transform_nested_values(hash, proc)
  hash.keys.each do |key|
    if hash[key].kind_of? Hash
      transform_nested_values(hash[key], proc)
    else
      hash[key] = proc.call(hash[key])
    end
  end
  hash
end
unflatten(document, delimiter='__') click to toggle source
# File lib/agnostic_backend/utilities.rb, line 68
def unflatten(document, delimiter='__')
  unflatten = {}

  def unflatten_hash(hash, delimiter)
    key, value = hash.keys.first, hash.values.first
    components = key.split(delimiter)
    new_key = components.shift
    if components.empty?
      return {new_key => value}
    else
      unflat_hash = {}
      unflat_hash[new_key] = unflatten_hash({components.join(delimiter) => value}, delimiter)
    end

    unflat_hash
  end

  document.each do |k, v|
    unflatten.deep_merge!(unflatten_hash({k => v}, delimiter))
  end

  unflatten
end
unflatten_hash(hash, delimiter) click to toggle source
# File lib/agnostic_backend/utilities.rb, line 71
def unflatten_hash(hash, delimiter)
  key, value = hash.keys.first, hash.values.first
  components = key.split(delimiter)
  new_key = components.shift
  if components.empty?
    return {new_key => value}
  else
    unflat_hash = {}
    unflat_hash[new_key] = unflatten_hash({components.join(delimiter) => value}, delimiter)
  end

  unflat_hash
end
value_for_key(document, key) click to toggle source
# File lib/agnostic_backend/utilities.rb, line 103
def value_for_key(document, key)
  keys = key.split('.')
  key = keys.shift
  if document.is_a?(Hash)
    if document.has_key? key
      value_for_key(document[key], keys.join('.'))
    else
      return nil
    end
  else
    if document.present? && key.nil?
      return document
    else
      return nil
    end
  end
end
with_exponential_backoff(error, &block) click to toggle source
# File lib/agnostic_backend/utilities.rb, line 14
def with_exponential_backoff(error, &block)
  attempts = 0

  begin
    block.call
  rescue error => e
    if attempts < exponential_backoff_max_attempts
      sleep(exponential_backoff_sleep_time(exponential_backoff_max_time, exponential_backoff_base, attempts))
      attempts += 1
      retry
    else
      raise e
    end
  end
end