Class: Alphavantage::Batch
- Inherits:
-
Object
- Object
- Alphavantage::Batch
- Includes:
- HelperFunctions
- Defined in:
- lib/Batch.rb
Instance Attribute Summary collapse
-
#datatype ⇒ Object
readonly
Returns the value of attribute datatype.
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#stock_quote ⇒ Object
readonly
Returns the value of attribute stock_quote.
-
#symbols ⇒ Object
readonly
Returns the value of attribute symbols.
Instance Method Summary collapse
-
#initialize(symbols:, datatype: "json", key:, verbose: false, file: nil) ⇒ Batch
constructor
A new instance of Batch.
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.}", 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
#datatype ⇒ Object (readonly)
Returns the value of attribute datatype.
50 51 52 |
# File 'lib/Batch.rb', line 50 def datatype @datatype end |
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
50 51 52 |
# File 'lib/Batch.rb', line 50 def hash @hash end |
#stock_quote ⇒ Object (readonly)
Returns the value of attribute stock_quote.
50 51 52 |
# File 'lib/Batch.rb', line 50 def stock_quote @stock_quote end |
#symbols ⇒ Object (readonly)
Returns the value of attribute symbols.
50 51 52 |
# File 'lib/Batch.rb', line 50 def symbols @symbols end |