Class: Alphavantage::Crypto_Timeseries

Inherits:
Object
  • Object
show all
Includes:
HelperFunctions
Defined in:
lib/Crypto_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: "intraday", market:, symbol:, datatype: "json", file: nil, key:, verbose: false) ⇒ Crypto_Timeseries

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

def initialize type: "intraday", market:, symbol:, datatype: "json", file: nil,
  key:, verbose: false
  check_argument([true, false], verbose, "verbose")
  @client = return_client(key, verbose)
  check_argument(["json", "csv"], datatype, "datatype")
  if datatype == "csv" && file.nil?
    raise Alphavantage::Error.new message: "No file specified where to save the CSV data"
  elsif datatype != "csv" && !file.nil?
    raise Alphavantage::Error.new message: "Hash error: No file necessary"
  end

  @selected_time_series = which_series(type)
  url = "function=#{@selected_time_series}&symbol=#{symbol}&market=#{market}"
  return @client.download(url, file) if datatype == "csv"
  @hash = @client.request(url)
   = @hash.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 = []; @volume = [];
  @open_usd = []; @high_usd = []; @low_usd = []; @close_usd = [];
  @market_cap_usd = [];

  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)
    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.



58
59
60
# File 'lib/Crypto_Timeseries.rb', line 58

def hash
  @hash
end

Instance Method Details

#which_series(type) ⇒ Object



60
61
62
63
64
65
# File 'lib/Crypto_Timeseries.rb', line 60

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