Class: FredAsDataframe::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/fred_as_dataframe/client.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.



19
20
21
22
# File 'lib/fred_as_dataframe/client.rb', line 19

def initialize(series, options={})
  @api_key = options[:api_key] || FredAsDataframe::Client.api_key
  @tag = series
end

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



16
17
18
# File 'lib/fred_as_dataframe/client.rb', line 16

def api_key
  @api_key
end

Instance Attribute Details

#tagObject (readonly)

Returns the value of attribute tag.



8
9
10
# File 'lib/fred_as_dataframe/client.rb', line 8

def tag
  @tag
end

Class Method Details

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

Yields:

  • (_self)

Yield Parameters:



10
11
12
13
# File 'lib/fred_as_dataframe/client.rb', line 10

def self.configure
  yield self
  true
end

Instance Method Details

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fred_as_dataframe/client.rb', line 24

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