Class: Alphavantage::Batch

Inherits:
Object
  • Object
show all
Includes:
HelperFunctions
Defined in:
lib/Batch.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(symbols:, datatype: "json", key:, verbose: false, file: nil) ⇒ Batch

Returns a new instance of Batch.



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
# File 'lib/Batch.rb', line 5

def initialize symbols:, datatype: "json", key:, verbose: false, file: nil
  check_argument([true, false], verbose, "verbose")
  symbols = [symbols] unless symbols.is_a?(Array)
  @client = return_client(key, verbose)
  @symbols = symbols
  @datatype = datatype
  @verbose = verbose
  @file = file
  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
  name_symbols = symbols.join(",")
  name_symbols.upcase!
  url = "function=BATCH_STOCK_QUOTES&symbols=#{name_symbols}"
  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
    stock_quote = @hash.find{|key, val| key.include?("Stock Quotes")}[1]
  rescue Exception => e
    raise Alphavantage::Error.new message: "No Stock Quotes found: #{e.message}",
      data: @hash
  end

  @stock_quote = []
  stock_quote.each do |sq|
    sval = {}
    sq.each do |key, val|
      key_sym = key.downcase.gsub(/[0-9.]/, "").lstrip.gsub(" ", "_")
      sval[key_sym] = val
    end
    @stock_quote << sval
  end
end

Dynamic Method Handling

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

Instance Attribute Details

#datatypeObject (readonly)

Returns the value of attribute datatype.



50
51
52
# File 'lib/Batch.rb', line 50

def datatype
  @datatype
end

#hashObject (readonly)

Returns the value of attribute hash.



50
51
52
# File 'lib/Batch.rb', line 50

def hash
  @hash
end

#stock_quoteObject (readonly)

Returns the value of attribute stock_quote.



50
51
52
# File 'lib/Batch.rb', line 50

def stock_quote
  @stock_quote
end

#symbolsObject (readonly)

Returns the value of attribute symbols.



50
51
52
# File 'lib/Batch.rb', line 50

def symbols
  @symbols
end