Class: Alphavantage::Exchange_Timeseries

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HelperFunctions

#check_argument, #check_datatype, #method_missing, #open_struct, #recreate_metadata_key, #return_client, #return_int_val, #return_matype, #return_series, #return_value, #which_series

Constructor Details

#initialize(type: "intraday", from:, to:, datatype: "json", file: nil, key:, verbose: false, outputsize: "compact", interval: nil) ⇒ Exchange_Timeseries

Returns a new instance of Exchange_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
# File 'lib/Exchange_Timeseries.rb', line 5

def initialize type: "intraday", from:, to:, datatype: "json", file: nil,
  key:, verbose: false, outputsize: "compact", interval: nil
  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")
    interval = "&interval=#{interval}"
  else
    check_argument([nil], interval, "interval")
    interval = ""
  end
  check_argument(["compact", "full"], outputsize, "outputsize")
  check_argument(["json", "csv"], datatype, "datatype")
  check_datatype(datatype, file)

  @selected_time_series = which_series(type, "FX")
  url = "function=#{@selected_time_series}&from_symbol=#{from}&to_symbol=#{to}#{interval}&outputsize=#{outputsize}"
  return @client.download(url, file) if datatype == "csv"
  @output = @client.request(url)
   = @output.dig("Meta Data") || {}
  .each do |key, val|
    key_sym = key.downcase.gsub(/[0-9.]/, "").lstrip.gsub(" ", "_").to_sym
    define_singleton_method(key_sym) do
      return val
    end
  end
  @open = []; @high = []; @low = []; @close = [];

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

  series = {}
  convert_key = {}
  time_series.values[0].keys.each do |key|
    key_sym = (key)
    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

#outputObject (readonly)

Returns the value of attribute output.



61
62
63
# File 'lib/Exchange_Timeseries.rb', line 61

def output
  @output
end