Class: Finnhub::Timeseries

Inherits:
Object
  • Object
show all
Defined in:
lib/Timeseries.rb

Direct Known Subclasses

Crypto_Timeseries, Forex_Timeseries

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, stock:, resolution: "D", count: 100, from: nil, to: nil, format: nil) ⇒ Timeseries

Returns a new instance of Timeseries.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/Timeseries.rb', line 3

def initialize(client:, stock:, resolution: "D", count: 100,
  from: nil, to: nil, format: nil)
  url = "/stock/candle?symbol=#{stock.symbol}&resolution=#{resolution}"
  url += "&count=#{count}" unless count.nil?
  from = from.to_i if from.is_a?(Time)
  url += "&from=#{from}" unless from.nil?
  to = to.to_i if to.is_a?(Time)
  url += "&to=#{to}" unless to.nil?
  url += "&format=#{format}" unless format.nil?
  @output = client.request(url)
  if @output.is_a?(Hash) && @output[:s] == "ok"
    @timestamps = @output[:t]&.map{|t| DateTime.strptime(t.to_s,'%s')}
  else
    @timestamps = []
  end
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



20
21
22
# File 'lib/Timeseries.rb', line 20

def output
  @output
end

#timestampsObject (readonly)

Returns the value of attribute timestamps.



20
21
22
# File 'lib/Timeseries.rb', line 20

def timestamps
  @timestamps
end

Instance Method Details

#closeObject



34
35
36
# File 'lib/Timeseries.rb', line 34

def close
  operation(:c)
end

#highObject



26
27
28
# File 'lib/Timeseries.rb', line 26

def high
  operation(:h)
end

#lowObject



30
31
32
# File 'lib/Timeseries.rb', line 30

def low
  operation(:l)
end

#openObject



22
23
24
# File 'lib/Timeseries.rb', line 22

def open
  operation(:o)
end

#statusObject



42
43
44
45
# File 'lib/Timeseries.rb', line 42

def status
  raise Finnhub::Error message: "Output is not a hash" unless @output.is_a?(Hash)
  @output[:s]
end

#volumeObject



38
39
40
# File 'lib/Timeseries.rb', line 38

def volume
  operation(:v)
end