Class: EconDataReader::BankOfCanada

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/econ_data_reader/bank_of_canada.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(series, options = {}) ⇒ BankOfCanada

Returns a new instance of BankOfCanada.



14
15
16
# File 'lib/econ_data_reader/bank_of_canada.rb', line 14

def initialize(series, options={})
  @tag = series
end

Instance Attribute Details

#tagObject (readonly)

Returns the value of attribute tag.



12
13
14
# File 'lib/econ_data_reader/bank_of_canada.rb', line 12

def tag
  @tag
end

Class Method Details

.list_seriesObject



28
29
30
31
32
33
34
35
36
# File 'lib/econ_data_reader/bank_of_canada.rb', line 28

def self.list_series
  dta = (get('/lists/series/json').parsed_response)['series']

  series = dta.keys
  desc = series.map{|s| [dta[s]['label'], dta[s]['description']].join('; ') }

  Polars::Config.set_tbl_rows(-1)
  Polars::DataFrame.new({Series: series, Description: desc})
end

Instance Method Details

#fetch(start: nil, fin: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/econ_data_reader/bank_of_canada.rb', line 18

def fetch(start: nil, fin: nil)
  dta = observations({}).parsed_response['observations']
  dta = dta.select{|d| start.nil? ? true : d['d'].to_date >= start.to_date }.select{|d| fin.nil? ? true : d['d'].to_date <= fin.to_date }

  dates = dta.map{|d| d['d'].to_date }
  vals = dta.map{|d| d[tag]['v'].to_f }

  Polars::DataFrame.new({Timestamps: dates, Values: vals})
end