Module: Quotes

Defined in:
lib/quotes.rb,
lib/quotes/version.rb

Defined Under Namespace

Classes: Quote

Constant Summary collapse

ENDPOINT =

Yahoo API endpoint

"http://query.yahooapis.com/v1/public/yql?q="
VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.get(*symbols) ⇒ Object



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/quotes.rb', line 9

def self.get(*symbols)
  if symbols.length == 0 || symbols.include?("")
    raise ArgumentError, "Enter symbol(s). Example: Quote.get(\"AAPL\")"
  end
  
  syms = symbols.map{|s| "\"#{s}\""}.join(", ")
  url = ENDPOINT + URI.escape("select * from yahoo.finance.quotes where symbol in (#{syms})") + "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="
  resp = ''
  open(url) do |f|
    resp = f.read
  end
  
  info = JSON.parse(resp)['query']['results']['quote']
  
  # returns a hash containing the ticker symbols as keys
  if symbols.length == 1
    quote = {}
    quote[info['symbol']] = Quote.new(info)
    return quote
  else
    quotes = {}
    info.each_with_index do |q, i|
      data = info[i]
      quotes[data['symbol']] = Quote.new(data)
    end
    return quotes
  end
end