Class: BankofcanadaAsDataframe::Client
- Inherits:
-
Object
- Object
- BankofcanadaAsDataframe::Client
- Includes:
- HTTParty
- Defined in:
- lib/bankofcanada_as_dataframe/client.rb
Instance Attribute Summary collapse
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
Class Method Summary collapse
Instance Method Summary collapse
- #fetch(start: nil, fin: nil) ⇒ Object
-
#initialize(series, options = {}) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(series, options = {}) ⇒ Client
Returns a new instance of Client.
15 16 17 |
# File 'lib/bankofcanada_as_dataframe/client.rb', line 15 def initialize(series, ={}) @tag = series end |
Instance Attribute Details
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
13 14 15 |
# File 'lib/bankofcanada_as_dataframe/client.rb', line 13 def tag @tag end |
Class Method Details
.list_series ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/bankofcanada_as_dataframe/client.rb', line 30 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
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/bankofcanada_as_dataframe/client.rb', line 19 def fetch(start: nil, fin: nil) dta = observations({}).parsed_response['observations'] dta = dta.select{|d| start.nil? ? true : Date.parse(d['d']) >= _parse_date(start) } unless start.nil? dta = dta.select{|d| fin.nil? ? true : Date.parse(d['d']) <= _parse_date(fin) } unless fin.nil? dates = dta.map{|d| Date.parse(d['d']) } vals = dta.map{|d| d[tag]['v'].to_f } Polars::DataFrame.new({Timestamps: dates, Values: vals}) end |