Module: IEX::Endpoints::Chart

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

Instance Method Summary collapse

Instance Method Details

#chart(symbol, range = nil, 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
28
29
30
31
32
33
34
35
36
# File 'lib/iex/endpoints/chart.rb', line 4

def chart(symbol, range = nil, options = {})
  url = [
    'stock',
    symbol,
    'chart',
    range && range.respond_to?(:strftime) ? "date/#{range.strftime('%Y%m%d')}" : range
  ].compact.join('/')

  params = {}
  if options
    options.each_pair do |k, v|
      k = k.to_s.split('_').map(&:capitalize).join.sub(/^[A-Z]/, &:downcase)
      params[k.to_sym] = v
    end
  end

  response = get(url, params)

  if range && range.to_s == 'dynamic'
    range = response['range']
    response = response['data']
  end
  response.map do |data|
    case range
    when '1d', Date, DateTime
      IEX::Resources::Chart::OneDay.new data
    else
      IEX::Resources::Chart::Default.new data
    end
  end
rescue Faraday::ResourceNotFound => e
  raise IEX::Errors::SymbolNotFoundError.new(symbol, e.response[:body])
end