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

#covidObject



131
132
133
# File 'lib/Client.rb', line 131

def covid
  request("/covid19/us")
end

#crypto_exchange(name:) ⇒ Object



98
99
100
# File 'lib/Client.rb', line 98

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

#crypto_exchanges(plain: false) ⇒ Object



91
92
93
94
95
96
# File 'lib/Client.rb', line 91

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:, hash: nil) ⇒ Object



126
127
128
129
# File 'lib/Client.rb', line 126

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

#economic_codes(plain: false) ⇒ Object



119
120
121
122
123
124
# File 'lib/Client.rb', line 119

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

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

#forex_exchange(name:) ⇒ Object



87
88
89
# File 'lib/Client.rb', line 87

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

#forex_exchanges(plain: false) ⇒ Object



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

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



108
109
110
111
112
113
# File 'lib/Client.rb', line 108

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



115
116
117
# File 'lib/Client.rb', line 115

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

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



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

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

#request(url, method: :get, body: nil) ⇒ 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
46
47
48
49
50
51
52
# File 'lib/Client.rb', line 15

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

  case method
  when :get
    puts "\nGET #{send_url}\n" if @verbose
    response = HTTParty.get(send_url)
  when :post
    puts "\nPOST #{send_url}, body: #{body}\n" if @verbose
    response = HTTParty.post(send_url, body: Oj.dump(body, mode: :json))
  end

  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



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

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

#stock_exchanges(plain: false) ⇒ Object



69
70
71
72
73
74
# File 'lib/Client.rb', line 69

def stock_exchanges(plain: false)
  output = request("/stock/exchange")
  return output if plain

  output.map{|o| Finnhub::Stock_Exchange.new(client: self, code: o[:code], hash: o)}
end

#webhookObject



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

def webhook
  Finnhub::Webhook.new(client: self)
end

#webhooks(plain: false) ⇒ Object



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

def webhooks(plain: false)
  output = request("/webhook/list")
  return output if plain

  output.map{|o| Finnhub::Webhook.new(client: self, id: o[:id], hash: o)}
end

#websocketObject



54
55
56
# File 'lib/Client.rb', line 54

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