Class: Finnhub::Client

Inherits:
Object
  • Object
show all
Includes:
Calendar
Defined in:
lib/Client.rb

Constant Summary collapse

BASE_URI =
'https://finnhub.io/api/v1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Calendar

#earnings_calendar, #economic_calendar, #ico_calendar, #ipo_calendar

Constructor Details

#initialize(key:, verbose: false) ⇒ Client

Returns a new instance of Client.



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

def initialize(key:, verbose: false)
  @apikey = key
  @verbose = verbose
end

Instance Attribute Details

#apikeyObject (readonly)

Returns the value of attribute apikey.



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

def apikey
  @apikey
end

#verboseObject

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Instance Method Details

#crypto_exchange(name:) ⇒ Object



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

def crypto_exchange(name:)
  Finnhub::Crypto_Exchange.new(client: self, name: name)
end

#crypto_exchanges(plain: false) ⇒ Object



66
67
68
69
70
71
# File 'lib/Client.rb', line 66

def crypto_exchanges(plain: false)
  output = request("/crypto/exchange")
  return output if plain

  output.map{|o| Finnhub::Crypto_Exchange.new(client: self, name: o)}
end

#economic_code(code:, description: nil) ⇒ Object



101
102
103
104
# File 'lib/Client.rb', line 101

def economic_code(code:, description: nil)
  Finnhub::Economic_Code.new(client: self, code: code,
    description: description)
end

#economic_codes(plain: false) ⇒ Object



94
95
96
97
98
99
# File 'lib/Client.rb', line 94

def economic_codes(plain: false)
  output = request("/economic/code")
  return output if plain

  output.map{|o| Finnhub::Economic_Code.new(client: self, code: o[0], description: o[1])}
end

#forex_exchange(name:) ⇒ Object



62
63
64
# File 'lib/Client.rb', line 62

def forex_exchange(name:)
  Finnhub::Forex_Exchange.new(client: self, name: name)
end

#forex_exchanges(plain: false) ⇒ Object



55
56
57
58
59
60
# File 'lib/Client.rb', line 55

def forex_exchanges(plain: false)
  output = request("/forex/exchange")
  return output if plain

  output.map{|o| Finnhub::Forex_Exchange.new(client: self, name: o)}
end

#merge_countries(plain: false) ⇒ Object



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

def merge_countries(plain: false)
  output = request("/merger/country")
  return output if plain

  output.map{|o| Finnhub::Merge_Country.new(client: self, country: o)}
end

#merge_country(country:) ⇒ Object



90
91
92
# File 'lib/Client.rb', line 90

def merge_country(country:)
  Finnhub::Merge_Country.new(client: self, country: country)
end

#news(category: "general", minId: nil) ⇒ Object



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

def news(category: "general", minId: nil)
  url = "/news?category=#{category}"
  url += "&minId=#{minId}" unless minId.nil?
  request(url)
end

#request(url) ⇒ Object



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

def request(url)
  send_url = "#{BASE_URI}#{url}"
  send_url += send_url.include?("?") ? "&" : "?"
  send_url += "token=#{@apikey}"

  puts "\nGET #{send_url}\n" if @verbose
  response = HTTParty.get(send_url)
  if @verbose
    puts "\nCODE: #{response.code}\n"
    puts "OUTPUT: #{response.body}\n"
  end

  if response.code == 200
    data = response.body
    if data[0] == "[" || data[0] == "{"
      data = Oj.load(data, symbol_keys: true)
      return data
    elsif data.include?("\n")
      return data
    else
      raise Finnhub::Error.new message: data, code: response.code
    end
  else
    raise Finnhub::Error.new message: data, code: response.code
  end

rescue Finnhub::Error => e
  raise e
rescue StandardError => e
  raise Finnhub::Error.new message: "Failed request: #{e.message}"
end

#stock(symbol:) ⇒ Object



51
52
53
# File 'lib/Client.rb', line 51

def stock(symbol:)
  Finnhub::Stock.new(client: self, symbol: symbol)
end

#websocketObject



47
48
49
# File 'lib/Client.rb', line 47

def websocket
  Finnhub::Websocket.new(@apikey)
end