Module: IEX::Endpoints::HistoricalPrices

Included in:
Api::Client
Defined in:
lib/iex/endpoints/historial_prices.rb

Instance Method Summary collapse

Instance Method Details

#historical_prices(symbol, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/iex/endpoints/historial_prices.rb', line 4

def historical_prices(symbol, options = {})
  if options[:range] == 'date'
    raise ArgumentError unless options[:date].present?
    raise ArgumentError unless options[:chartByDay].present?
  end

  options = options.dup
  # Historical prices IEX endpoint expects dates passed in a specific format - YYYYMMDD
  options[:date] = options[:date].strftime('%Y%m%d') if options[:date].is_a?(Date)

  path = "stock/#{symbol}/chart"
  path += "/#{options[:range]}" if options.key?(:range)
  path += "/#{options[:date]}" if options[:range] == 'date'

  # We only want options to include query params at this point, remove :range and :date
  options.delete(:range)
  options.delete(:date)

  (get(path, { token: publishable_token }.merge(options)) || []).map do |data|
    IEX::Resources::HistorialPrices.new(data)
  end
rescue Faraday::ResourceNotFound => e
  raise IEX::Errors::SymbolNotFoundError.new(symbol, e.response[:body])
end