Class: FinanceYtd

Inherits:
Object
  • Object
show all
Defined in:
lib/finance-ytd.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ FinanceYtd



12
13
14
15
16
17
# File 'lib/finance-ytd.rb', line 12

def initialize(options)
  @symbol = options[:symbol]
  @friendly_name = options[:friendly_name]
  @decimal_places = options[:decimal_places]
  @price_last_year = options[:price_last_year]
end

Instance Attribute Details

#cssObject

Returns the value of attribute css.



10
11
12
# File 'lib/finance-ytd.rb', line 10

def css
  @css
end

#css_textObject

Returns the value of attribute css_text.



10
11
12
# File 'lib/finance-ytd.rb', line 10

def css_text
  @css_text
end

#friendly_nameObject

Returns the value of attribute friendly_name.



10
11
12
# File 'lib/finance-ytd.rb', line 10

def friendly_name
  @friendly_name
end

#symbolObject

Returns the value of attribute symbol.



10
11
12
# File 'lib/finance-ytd.rb', line 10

def symbol
  @symbol
end

#urlObject

Returns the value of attribute url.



10
11
12
# File 'lib/finance-ytd.rb', line 10

def url
  @url
end

#ytd_returnObject

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

#calculateObject



24
25
26
# File 'lib/finance-ytd.rb', line 24

def calculate
  @ytd_return = @css_text.tr('\+%', '').to_f / 100.0
end

#matchObject



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_sObject



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