Class: FinanceYtd
- Inherits:
-
Object
- Object
- FinanceYtd
- Defined in:
- lib/finance-ytd.rb
Direct Known Subclasses
ApmexGoldFinanceYtd, ApmexSilverFinanceYtd, BloombergFinanceYtd, CnnFinanceYtd, CnnMarketFinanceYtd
Instance Attribute Summary collapse
-
#css ⇒ Object
Returns the value of attribute css.
-
#css_text ⇒ Object
Returns the value of attribute css_text.
-
#friendly_name ⇒ Object
Returns the value of attribute friendly_name.
-
#symbol ⇒ Object
Returns the value of attribute symbol.
-
#url ⇒ Object
Returns the value of attribute url.
-
#ytd_return ⇒ Object
Returns the value of attribute ytd_return.
Instance Method Summary collapse
- #calculate ⇒ Object
-
#initialize(options) ⇒ FinanceYtd
constructor
A new instance of FinanceYtd.
- #match ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(options) ⇒ FinanceYtd
12 13 14 15 16 17 |
# File 'lib/finance-ytd.rb', line 12 def initialize() @symbol = [:symbol] @friendly_name = [:friendly_name] @decimal_places = [:decimal_places] @price_last_year = [:price_last_year] end |
Instance Attribute Details
#css ⇒ Object
Returns the value of attribute css.
10 11 12 |
# File 'lib/finance-ytd.rb', line 10 def css @css end |
#css_text ⇒ Object
Returns the value of attribute css_text.
10 11 12 |
# File 'lib/finance-ytd.rb', line 10 def css_text @css_text end |
#friendly_name ⇒ Object
Returns the value of attribute friendly_name.
10 11 12 |
# File 'lib/finance-ytd.rb', line 10 def friendly_name @friendly_name end |
#symbol ⇒ Object
Returns the value of attribute symbol.
10 11 12 |
# File 'lib/finance-ytd.rb', line 10 def symbol @symbol end |
#url ⇒ Object
Returns the value of attribute url.
10 11 12 |
# File 'lib/finance-ytd.rb', line 10 def url @url end |
#ytd_return ⇒ Object
Returns the value of attribute ytd_return.
10 11 12 |
# File 'lib/finance-ytd.rb', line 10 def ytd_return @ytd_return end |
Instance Method Details
#calculate ⇒ Object
24 25 26 |
# File 'lib/finance-ytd.rb', line 24 def calculate @ytd_return = @css_text.tr('\+%', '').to_f / 100.0 end |
#match ⇒ Object
19 20 21 22 |
# File 'lib/finance-ytd.rb', line 19 def match page = Nokogiri::HTML(open(@url)) @css_text = page.css(@css).text.gsub(/[$,]/, '') end |
#to_s ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/finance-ytd.rb', line 28 def to_s ytd_return_rounded = (@ytd_return * 100.0).round(@decimal_places) ytd_return_rounded_string = ('%.' + @decimal_places.to_s + 'f') % ytd_return_rounded.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse + '%' s = @friendly_name + ' (' + @symbol + ') ' + ytd_return_rounded_string if ytd_return_rounded >= 0.0 s.bg_green else s.bg_red end end |