class Finnhub::Timeseries

Attributes

output[R]
timestamps[R]

Public Class Methods

new(client:, stock:, resolution: "D", count: 100, from: nil, to: nil, format: nil, adjusted: false) click to toggle source
# File lib/Timeseries.rb, line 3
def initialize(client:, stock:, resolution: "D", count: 100,
  from: nil, to: nil, format: nil, adjusted: false)
  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?
  url += "&adjusted=#{adjusted}" if adjusted
  @output = client.request(url)
  if @output.is_a?(Hash) && @output[:s] == "ok"
    @timestamps = @output[:t]&.map{|t| Time.strptime(t.to_s,'%s')}
  else
    @timestamps = []
  end
end

Public Instance Methods

close() click to toggle source
# File lib/Timeseries.rb, line 35
def close
  operation(:c)
end
high() click to toggle source
# File lib/Timeseries.rb, line 27
def high
  operation(:h)
end
low() click to toggle source
# File lib/Timeseries.rb, line 31
def low
  operation(:l)
end
open() click to toggle source
# File lib/Timeseries.rb, line 23
def open
  operation(:o)
end
status() click to toggle source
# File lib/Timeseries.rb, line 43
def status
  raise Finnhub::Error message: "Output is not a hash" unless @output.is_a?(Hash)
  @output[:s]
end
volume() click to toggle source
# File lib/Timeseries.rb, line 39
def volume
  operation(:v)
end

Private Instance Methods

operation(type) click to toggle source
# File lib/Timeseries.rb, line 50
def operation(type)
  raise Finnhub::Error message: "Output is not a hash" unless @output.is_a?(Hash)
  return [] if @output[type].nil?
  @timestamps.zip(@output[type])
end