Module: LlmCostTracker::Reconciliation::Sources::Coercion

Defined in:
lib/llm_cost_tracker/reconciliation/sources/coercion.rb

Class Method Summary collapse

Class Method Details

.coerce_hash(response, label:) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/llm_cost_tracker/reconciliation/sources/coercion.rb', line 26

def coerce_hash(response, label:)
  return {} if response.nil?
  return symbolize(response) if response.is_a?(Hash)

  parsed = JSON.parse(response.to_s)
  raise ArgumentError, "#{label} payload must be a JSON object" unless parsed.is_a?(Hash)

  symbolize(parsed)
rescue JSON::ParserError => e
  raise ArgumentError, "Unable to parse #{label} payload: #{e.message}"
end

.normalized_epoch(value) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/llm_cost_tracker/reconciliation/sources/coercion.rb', line 18

def normalized_epoch(value)
  return value.to_i if value.is_a?(Numeric)

  Time.parse(value.to_s).utc.to_i
rescue ArgumentError
  value.to_s
end

.symbolize(hash) ⇒ Object



12
13
14
15
16
# File 'lib/llm_cost_tracker/reconciliation/sources/coercion.rb', line 12

def symbolize(hash)
  return hash if hash.is_a?(Hash) && hash.keys.all?(Symbol)

  hash.to_h.transform_keys { |key| key.to_s.to_sym }
end