Class: Rebalance::Fund
- Inherits:
-
Object
- Object
- Rebalance::Fund
- Defined in:
- lib/rebalance/fund.rb
Instance Attribute Summary collapse
-
#asset_class ⇒ Object
Returns the value of attribute asset_class.
-
#name ⇒ Object
Returns the value of attribute name.
-
#price ⇒ Object
Returns the value of attribute price.
-
#shares ⇒ Object
Returns the value of attribute shares.
-
#symbol ⇒ Object
Returns the value of attribute symbol.
Instance Method Summary collapse
- #get_fund_price(symbol) ⇒ Object
-
#initialize(symbol, asset_class, shares, price = nil) ⇒ Fund
constructor
A new instance of Fund.
- #value ⇒ Object
Constructor Details
#initialize(symbol, asset_class, shares, price = nil) ⇒ Fund
Returns a new instance of Fund.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rebalance/fund.rb', line 8 def initialize(symbol, asset_class, shares, price=nil) self.symbol = symbol self.asset_class = asset_class self.shares = shares # Lookup value if not passed in if price.nil? price = get_fund_price(symbol) end self.price = price end |
Instance Attribute Details
#asset_class ⇒ Object
Returns the value of attribute asset_class.
6 7 8 |
# File 'lib/rebalance/fund.rb', line 6 def asset_class @asset_class end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/rebalance/fund.rb', line 6 def name @name end |
#price ⇒ Object
Returns the value of attribute price.
6 7 8 |
# File 'lib/rebalance/fund.rb', line 6 def price @price end |
#shares ⇒ Object
Returns the value of attribute shares.
6 7 8 |
# File 'lib/rebalance/fund.rb', line 6 def shares @shares end |
#symbol ⇒ Object
Returns the value of attribute symbol.
6 7 8 |
# File 'lib/rebalance/fund.rb', line 6 def symbol @symbol end |
Instance Method Details
#get_fund_price(symbol) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rebalance/fund.rb', line 21 def get_fund_price(symbol) yql_url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22#{symbol}%22)&env=store://datatables.org/alltableswithkeys&format=json" response = open(yql_url) parsed_response = JSON.parse(response.read) if !parsed_response['query']['results']['quote']['ErrorIndicationreturnedforsymbolchangedinvalid'].nil? raise "The symbol #{symbol} can't be found" end price = parsed_response['query']['results']['quote']['LastTradePriceOnly'] price = price.to_f # Cash funds don't always return a price, so just assume $1.00 if asset_class == "Cash" price = 1.00 end price end |
#value ⇒ Object
41 42 43 |
# File 'lib/rebalance/fund.rb', line 41 def value (price * shares).round(2) end |