Class: YahooFinance::CurrentQuote

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

Constant Summary collapse

CURRENT_FORMAT =
{
  "s"   =>  :symbol,
  "n"   =>  :name,
  "l1"  =>  [ :last_trade, "to_f" ],
  "d1"  =>  :date,
  "t1"  =>  :time,
  "c"   =>  :change,
  "c1"  =>  [ :change_points, "to_f" ],
  "p2"  =>  [ :change_percent, "to_f" ],
  "p"   =>  [ :previous_close, "to_f" ],
  "o"   =>  [ :open, "to_f" ],
  "h"   =>  [ :day_high, "to_f" ],
  "g"   =>  [ :day_low, "to_f" ],
  "v"   =>  [ :volume, "to_i" ],
  "m"   =>  :day_range,
  "l"   =>  :last_trade_with_time,
  "t7"  =>  :ticker_trend,
  "a2"  =>  [ :average_daily_volume, "to_i" ],
  "b"   =>  [ :bid, "to_f" ],
  "a"   =>  [ :ask, "to_f" ]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol) ⇒ CurrentQuote

Returns a new instance of CurrentQuote.



36
37
38
39
# File 'lib/stocktracker/yahoofinance.rb', line 36

def initialize(symbol)
  self.symbol= symbol
  self.results= get_current_quote
end

Instance Attribute Details

#resultsObject

Returns the value of attribute results.



12
13
14
# File 'lib/stocktracker/yahoofinance.rb', line 12

def results
  @results
end

#symbolObject

Returns the value of attribute symbol.



12
13
14
# File 'lib/stocktracker/yahoofinance.rb', line 12

def symbol
  @symbol
end

Instance Method Details

#current_formatObject



64
65
66
# File 'lib/stocktracker/yahoofinance.rb', line 64

def current_format
  CURRENT_FORMAT.keys.join
end

#current_mapObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/stocktracker/yahoofinance.rb', line 72

def current_map
  map = {}
  values = current_values
  CURRENT_FORMAT.each_with_index do |v,i|
    if v[1].is_a?(Array)
      map[v[1][0]] = values[i].send(v[1][1])
    else
      map[v[1]] = values[i]
    end
  end
  map
end

#current_valuesObject



68
69
70
# File 'lib/stocktracker/yahoofinance.rb', line 68

def current_values
  CSV.parse(yahoo_quote)[0]
end

#get_current_quote(attempt = 1) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/stocktracker/yahoofinance.rb', line 41

def get_current_quote(attempt = 1)
  if attempt <= 3
    puts "Attempt #{attempt} to pull quote for #{self.symbol}"
    current_map || get_current_quote(attempt + 1)
  else
    raise 'Failed to get current quote within 3 attempts'
  end
rescue Errno::ECONNRESET => e
  puts "Connection Error #{e.message}"
  get_current_quote(attempt + 1)
rescue => e
  puts e.message
  nil
end

#yahoo_current_urlObject



60
61
62
# File 'lib/stocktracker/yahoofinance.rb', line 60

def yahoo_current_url
  URI.encode "http://download.finance.yahoo.com/d/quotes.csv?s=#{self.symbol}&f=#{current_format}"
end

#yahoo_quoteObject



56
57
58
# File 'lib/stocktracker/yahoofinance.rb', line 56

def yahoo_quote
  open(yahoo_current_url).read
end