Class: Alphavantage::Timeseries

Inherits:
Object
  • Object
show all
Includes:
HelperFunctions
Defined in:
lib/Timeseries.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HelperFunctions

#check_argument, #method_missing, #recreate_metadata_key, #return_client, #return_int_val, #return_matype, #return_series, #return_value

Constructor Details

#initialize(type: "daily", interval: nil, outputsize: "compact", symbol:, datatype: "json", file: nil, key:, verbose: false, adjusted: false) ⇒ Timeseries

Returns a new instance of Timeseries.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/Timeseries.rb', line 5

def initialize type: "daily", interval: nil, outputsize: "compact",
  symbol:, datatype: "json", file: nil, key:, verbose: false,
  adjusted: false
  check_argument([true, false], verbose, "verbose")
  @client = return_client(key, verbose)
  if type == "intraday"
    interval ||= "1min"
    check_argument(["1min", "5min", "15min", "30min", "60min"], interval, "interval")
    check_argument([false], adjusted, "adjusted")
    interval = "&interval=#{interval}"
  else
    check_argument([nil], interval, "interval")
    interval = ""
  end
  check_argument(["compact", "full"], outputsize, "outputsize")
  check_argument(["json", "csv"], datatype, "datatype")
  if datatype == "csv" && file.nil?
    raise Alphavantage::Error.new message: "No file specified where to save the CSV ata"
  elsif datatype != "csv" && !file.nil?
    raise Alphavantage::Error.new message: "Hash error: No file necessary"
  end

  @selected_time_series = which_series(type, adjusted)
  url = "function=#{@selected_time_series}&symbol=#{symbol}#{interval}&outputsize=#{outputsize}"
  return @client.download(url, file) if datatype == "csv"
  @hash = @client.request(url)
   = @hash.dig("Meta Data") || {}
  .each do |key, val|
    key_sym = (key)
    define_singleton_method(key_sym) do
      return val
    end
  end

  begin
    time_series = @hash.find{|key, val| key.include?("Time Series")}[1]
  rescue Exception => e
    raise Alphavantage::Error.new message: "No Time Series found: #{e.message}",
      data: @hash
  end

  series = {}
  convert_key = {}
  time_series.values[0].keys.each do |key|
    key_sym = key.downcase.gsub(/[0-9.]/, "").lstrip.gsub(" ", "_").to_sym
    series[key_sym] = []
    convert_key[key] = key_sym
  end
  time_series.each do |time, ts_hash|
    ts_hash.each do |key, value|
      series[convert_key[key]] << [time, value]
    end
  end
  series.keys.each do |key_sym|
    define_singleton_method(key_sym) do |*args|
      args ||= []
      return return_series(series[key_sym], args[0])
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class HelperFunctions

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



66
67
68
# File 'lib/Timeseries.rb', line 66

def hash
  @hash
end

Instance Method Details

#which_series(type, adjusted) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/Timeseries.rb', line 68

def which_series(type, adjusted)
  check_argument(["intraday", "daily", "weekly", "monthly"], type, "type")
  series = "TIME_SERIES_"
  series += type.upcase
  series += "_ADJUSTED" if adjusted
  return series
end