Class: Timeprice::CpiLookup
- Inherits:
-
Object
- Object
- Timeprice::CpiLookup
- Defined in:
- lib/timeprice/cpi_lookup.rb
Overview
Resolves CPI keys (“YYYY”, “YYYY-MM”, or “YYYY-Qn”) to a CpiPoint against a single country’s parsed CPI data hash. Knowing the JSON shape (“monthly” / “quarterly” / “annual” string keys) is isolated here — Inflation just asks for points.
Constant Summary collapse
- QUARTER_RE =
/\A(\d{4})-Q([1-4])\z/
Instance Method Summary collapse
- #at(key) ⇒ CpiPoint
-
#initialize(data) ⇒ CpiLookup
constructor
A new instance of CpiLookup.
Constructor Details
#initialize(data) ⇒ CpiLookup
Returns a new instance of CpiLookup.
18 19 20 21 22 23 |
# File 'lib/timeprice/cpi_lookup.rb', line 18 def initialize(data) @data = data @monthly = data.dig("series", "monthly") || {} @quarterly = data.dig("series", "quarterly") || {} @annual = data.dig("series", "annual") || {} end |
Instance Method Details
#at(key) ⇒ CpiPoint
29 30 31 32 33 34 35 36 37 |
# File 'lib/timeprice/cpi_lookup.rb', line 29 def at(key) key = key.to_s case key when QUARTER_RE then quarterly_or_fallbacks(key) when /\A\d{4}-\d{2}\z/ then monthly_or_fallbacks(key) when /\A\d{4}\z/ then annual_or_derived(key) else raise ArgumentError, "Invalid date format: #{key.inspect} (use YYYY, YYYY-MM, or YYYY-Qn)" end end |