Class: Alphavantage::Client

Inherits:
Object
  • Object
show all
Includes:
HelperFunctions
Defined in:
lib/Client.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(key:, verbose: false) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
# File 'lib/Client.rb', line 5

def initialize key:, verbose: false
  check_argument([true, false], verbose, "verbose")
  @apikey = key
  @base_uri = 'https://www.alphavantage.co'
  @verbose = verbose
end

Dynamic Method Handling

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

Instance Attribute Details

#verboseObject

Returns the value of attribute verbose.



12
13
14
# File 'lib/Client.rb', line 12

def verbose
  @verbose
end

Instance Method Details

#crypto(symbol:, market:, datatype: "json") ⇒ Object



81
82
83
# File 'lib/Client.rb', line 81

def crypto(symbol:, market:, datatype: "json")
  Alphavantage::Crypto.new symbol: symbol, key: self, datatype: datatype, market: market
end

#download(url, file) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/Client.rb', line 42

def download(url, file)
  send_url = "#{@base_uri}/query?#{url}&datatype=csv&apikey=#{@apikey}"
  begin
    puts send_url if @verbose
    uri = URI.parse(send_url)
    uri.open{|csv| IO.copy_stream(csv, file)}
  rescue Exception => e
    raise Alphavantage::Error.new message: "Failed to save the CSV file: #{e.message}"
  end
  return "CSV saved in #{file}"
end

#exchange(from:, to:, datatype: "json") ⇒ Object



77
78
79
# File 'lib/Client.rb', line 77

def exchange(from:, to:, datatype: "json")
  Alphavantage::Exchange.new from: from, to: to, key: self, datatype: datatype
end

#request(url) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/Client.rb', line 19

def request(url)
  send_url = "#{@base_uri}/query?#{url}&apikey=#{@apikey}"
  puts "\n#{send_url}\n" if @verbose
  begin
    response = HTTParty.get(send_url)
  rescue Exception => e
    raise Alphavantage::Error.new message: "Failed request: #{e.message}"
  end
  data = response.body
  begin
    data = JSON.parse(data)
  rescue Exception => e
    raise Alphavantage::Error.new message: "Parsing failed",
      data: data
  end
  if !data["Error Message"].nil?
    raise Alphavantage::Error.new message:  data["Error Message"], data: data
  elsif !data["Information"].nil?
    raise Alphavantage::Error.new message: data["Information"], data: data
  end
  return data
end

#search(keywords:, datatype: "json", file: nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/Client.rb', line 54

def search(keywords:, datatype: "json", file: nil)
  check_datatype(datatype, file)
  url = "function=SYMBOL_SEARCH&keywords=#{keywords}"
  return download(url, file) if datatype == "csv"
  output = OpenStruct.new
  output.output = request(url)
  bestMatches = output.output.dig("bestMatches") || {}
  output.stocks = bestMatches.map do |bm|
    val = OpenStruct.new
    bm.each do |key, valz|
      key_sym = (key)
      val[key_sym] = valz
    end
    val.stock = Alphavantage::Stock.new(symbol: bm["1. symbol"], key: self)
    val
  end
  return output
end

#sectorObject



85
86
87
# File 'lib/Client.rb', line 85

def sector
  Alphavantage::Sector.new key: self
end

#stock(symbol:, datatype: "json") ⇒ Object



73
74
75
# File 'lib/Client.rb', line 73

def stock(symbol:, datatype: "json")
  Alphavantage::Stock.new symbol: symbol, key: self, datatype: datatype
end