Class: RTicker::Equity
Overview
Represents stocks/equities (e.g. AAPL, MSFT)
Instance Attribute Summary collapse
-
#source ⇒ Object
Equities can be retrieved via either Google Finance or Yahoo Finance.
-
#start_value ⇒ Object
Equities can be retrieved via either Google Finance or Yahoo Finance.
Attributes inherited from Entry
#bold, #curr_value, #description, #last_changed, #purchase_count, #purchase_price, #symbol
Class Method Summary collapse
- .update(entries, source = :google) ⇒ Object
- .update_google(entries) ⇒ Object
- .update_yahoo(entries) ⇒ Object
Instance Method Summary collapse
-
#initialize(symbol, description = nil, purchase_count = nil, purchase_price = nil, bold = false) ⇒ Equity
constructor
A new instance of Equity.
Constructor Details
#initialize(symbol, description = nil, purchase_count = nil, purchase_price = nil, bold = false) ⇒ Equity
Returns a new instance of Equity.
18 19 20 21 22 |
# File 'lib/rticker/equity.rb', line 18 def initialize (symbol, description=nil, purchase_count=nil, purchase_price=nil, bold=false) super(symbol, description, purchase_count, purchase_price, bold) @start_value=nil @source=:google end |
Instance Attribute Details
#source ⇒ Object
Equities can be retrieved via either Google Finance or Yahoo Finance.
16 17 18 |
# File 'lib/rticker/equity.rb', line 16 def source @source end |
#start_value ⇒ Object
Equities can be retrieved via either Google Finance or Yahoo Finance.
16 17 18 |
# File 'lib/rticker/equity.rb', line 16 def start_value @start_value end |
Class Method Details
.update(entries, source = :google) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/rticker/equity.rb', line 24 def Equity.update (entries, source=:google) if source == :google Equity.update_google(entries) elsif source == :yahoo Equity.update_yahoo(entries) else raise ArgumentError, "Unexpected symbol: #{source.to_s}" end end |
.update_google(entries) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rticker/equity.rb', line 34 def Equity.update_google (entries) symbols = entries.map { |e| e.symbol } uri = "http://www.google.com/finance/info?client=ig&q=%s" % CGI::escape(symbols.join(",")) response = RTicker::Net.get_response(uri) rescue return return if response =~ /illegal/ # Google didn't like our request. results = response.split("{")[1..-1] return if results.nil? entries.zip(results) do |entry, result| # Fish out the info we want. Could use a JSON library, but that's # one more gem that would be required of the user to install. Opted # instead to just use some ugly regexs price = /,"l" : "([^"]*)"/.match(result)[1].tr(",", "") rescue return change = /,"c" : "([^"]*)"/.match(result)[1].tr(",", "") rescue return if price.to_f != entry.curr_value and not entry.curr_value.nil? # The price has changed entry.last_changed = Time.now() end entry.curr_value = price.to_f entry.start_value = entry.curr_value - change.to_f end end |
.update_yahoo(entries) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rticker/equity.rb', line 56 def Equity.update_yahoo (entries) symbols = entries.map { |e| e.symbol } uri = "http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=l1c1d1va2xj1b4j4dyekjm3m4rr5p5p6s7" % CGI::escape(symbols.join(",")) response = RTicker::Net.get_response(uri) rescue return return if response.nil? results = response.split("\n") entries.zip(results) do |entry, result| # Yahoo uses A CSV format. return if result.nil? fields = result.split(",") price = fields[0] change = fields[1] last_date = fields[2] return if last_date.nil? or Date.strptime(last_date, '"%m/%d/%Y"') != Date.today if price.to_f != entry.curr_value and not entry.curr_value.nil? # The price has changed entry.last_changed = Time.now() end entry.curr_value = price.to_f entry.start_value = entry.curr_value - change.to_f end end |