Class: Timeprice::CpiLookup

Inherits:
Object
  • Object
show all
Defined in:
lib/timeprice/cpi_lookup.rb

Overview

Resolves CPI keys (“YYYY” or “YYYY-MM”) to a CpiPoint against a single country’s parsed CPI data hash. Knowing the JSON shape (“monthly” / “annual” string keys) is isolated here — Inflation just asks for points.

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ CpiLookup

Returns a new instance of CpiLookup.



15
16
17
18
19
# File 'lib/timeprice/cpi_lookup.rb', line 15

def initialize(data)
  @data = data
  @monthly = data.dig("series", "monthly") || {}
  @annual  = data.dig("series", "annual")  || {}
end

Instance Method Details

#at(key) ⇒ CpiPoint

Parameters:

  • key (String)

    “YYYY” or “YYYY-MM”

Returns:

Raises:

  • (DataNotFound)

    if no CPI value covers ‘key`

  • (ArgumentError)

    on malformed ‘key`



25
26
27
28
29
30
31
32
# File 'lib/timeprice/cpi_lookup.rb', line 25

def at(key)
  key = key.to_s
  case key
  when /\A\d{4}-\d{2}\z/ then monthly_or_annual_fallback(key)
  when /\A\d{4}\z/       then annual_or_monthly_average(key)
  else raise ArgumentError, "Invalid date format: #{key.inspect} (use YYYY or YYYY-MM)"
  end
end