Class: EconDataReader::Bls

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Bls.



24
25
26
27
# File 'lib/econ_data_reader/bls.rb', line 24

def initialize(series, options={})
  @api_key = options[:bls_api_key] || EconDataReader::Bls.bls_api_key
  @tag = series
end

Class Attribute Details

.bls_api_keyObject

Returns the value of attribute bls_api_key.



21
22
23
# File 'lib/econ_data_reader/bls.rb', line 21

def bls_api_key
  @bls_api_key
end

Instance Attribute Details

#tagObject (readonly)

debug_output $stdout



13
14
15
# File 'lib/econ_data_reader/bls.rb', line 13

def tag
  @tag
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



15
16
17
18
# File 'lib/econ_data_reader/bls.rb', line 15

def self.configure
  yield self
  true
end

Instance Method Details

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/econ_data_reader/bls.rb', line 29

def fetch(start: nil, fin: nil)
  dta = observations({ seriesid: ["\"#{tag}\""] })
  # Rails.logger.info { "#{__FILE__}:#{__LINE__} dta = #{dta.inspect}" }
  dta = dta.parsed_response['Results']['series'].first['data']
  Rails.logger.info { "#{__FILE__}:#{__LINE__} dta = #{dta.inspect}" }

  dates = []
  dta.each do |d|
    if d['period'][0] == 'M'
      dates << "#{d['year']}-#{d['period'][1..-1]}-01".to_date.end_of_month
    elsif d['period'][0] == 'Q'
      dates << "#{d['year']}-#{d['period'][1..-1]*3}-01".to_date.end_of_month
    end
  end
  dates = dates.map{|d| d.to_date }
  vals = dta.map{|d| d['value'].to_f }
  (0..dates.length-1).to_a.reverse.each do |d|
    if (start.present? && dates[d] <= start.to_date) || (fin.present? && dates[d] >= fin.to_date)
      dates.delete_at(d)
      vals.delete_at(d)
    end
  end

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