Class: EconDataReader::Fred

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Fred.



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

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

Class Attribute Details

.fred_api_keyObject

Returns the value of attribute fred_api_key.



18
19
20
# File 'lib/econ_data_reader/fred.rb', line 18

def fred_api_key
  @fred_api_key
end

Instance Attribute Details

#tagObject (readonly)

Returns the value of attribute tag.



10
11
12
# File 'lib/econ_data_reader/fred.rb', line 10

def tag
  @tag
end

Class Method Details

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

Yields:

  • (_self)

Yield Parameters:



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

def self.configure
  yield self
  true
end

Instance Method Details

#fetch(start: nil, fin: nil, interval: '1d') ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/econ_data_reader/fred.rb', line 26

def fetch(start: nil, fin: nil, interval: '1d')
  data = _fetch_data(interval)
  i = interval[0..-2].to_i

  begin
    data = data.select{ |date, _| (start.nil? || date >= start.to_date) && (fin.nil? || date <= fin.to_date) }
    data = data.each_slice(i).map(&:first)
  rescue KeyError => e
    if data.keys[3].to_s[7, 5] == "Error"
      raise IOError, "Failed to get the data. Check that '#{nm}' is a valid FRED series."
    else
      raise e
    end
  end

  Polars::DataFrame.new({Timestamps: data.map(&:first), tag.to_sym => data.map(&:last)})
end