Module: VectorAmp::Utils

Defined in:
lib/vector_amp/utils.rb

Class Method Summary collapse

Class Method Details

.coerce_vector_id(id) ⇒ Object

Coerce a vector record id into a JSON-safe value that preserves numeric ids.

Integers (and integer-valued floats) are returned as Integers so they serialize as JSON numbers rather than strings. Everything else is left as given (strings stay strings). This prevents the API from rewriting numeric ids that were sent as quoted strings.

Parameters:

  • id (Object)

    vector id.

Returns:

  • (Object)

    the id, with numeric ids preserved as numbers.



28
29
30
31
32
33
34
# File 'lib/vector_amp/utils.rb', line 28

def coerce_vector_id(id)
  case id
  when Integer then id
  when Float then id == id.to_i ? id.to_i : id
  else id
  end
end

.compact_hash(hash) ⇒ Object



7
8
9
10
11
# File 'lib/vector_amp/utils.rb', line 7

def compact_hash(hash)
  hash.each_with_object({}) do |(key, value), result|
    result[key] = value unless value.nil?
  end
end

.ensure_no_unknown!(unknown, method_name) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
# File 'lib/vector_amp/utils.rb', line 13

def ensure_no_unknown!(unknown, method_name)
  return if unknown.empty?

  keys = unknown.keys.map(&:to_s).join(", ")
  raise ArgumentError, "unknown #{method_name} option(s): #{keys}"
end

.normalize_vectors(vectors) ⇒ Array<Hash>

Normalize a list of vector records so numeric ids stay numeric.

Parameters:

  • vectors (Array<Hash>)

    vector records.

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vector_amp/utils.rb', line 39

def normalize_vectors(vectors)
  Array(vectors).map do |vector|
    next vector unless vector.is_a?(Hash)
    next vector unless vector.key?(:id) || vector.key?("id")

    copy = vector.dup
    if copy.key?(:id)
      copy[:id] = coerce_vector_id(copy[:id])
    else
      copy["id"] = coerce_vector_id(copy["id"])
    end
    copy
  end
end