Class: YahooFinance::CurrentQuote
- Inherits:
-
Object
- Object
- YahooFinance::CurrentQuote
- 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
-
#results ⇒ Object
Returns the value of attribute results.
-
#symbol ⇒ Object
Returns the value of attribute symbol.
Instance Method Summary collapse
- #current_format ⇒ Object
- #current_map ⇒ Object
- #current_values ⇒ Object
- #get_current_quote(attempt = 1) ⇒ Object
-
#initialize(symbol) ⇒ CurrentQuote
constructor
A new instance of CurrentQuote.
- #yahoo_current_url ⇒ Object
- #yahoo_quote ⇒ Object
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
#results ⇒ Object
Returns the value of attribute results.
12 13 14 |
# File 'lib/stocktracker/yahoofinance.rb', line 12 def results @results end |
#symbol ⇒ Object
Returns the value of attribute symbol.
12 13 14 |
# File 'lib/stocktracker/yahoofinance.rb', line 12 def symbol @symbol end |
Instance Method Details
#current_format ⇒ Object
64 65 66 |
# File 'lib/stocktracker/yahoofinance.rb', line 64 def current_format CURRENT_FORMAT.keys.join end |
#current_map ⇒ Object
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_values ⇒ Object
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.}" get_current_quote(attempt + 1) rescue => e puts e. nil end |
#yahoo_current_url ⇒ Object
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_quote ⇒ Object
56 57 58 |
# File 'lib/stocktracker/yahoofinance.rb', line 56 def yahoo_quote open(yahoo_current_url).read end |