Class: QuoteOnly

Inherits:
Object
  • Object
show all
Defined in:
lib/quote-only.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ QuoteOnly

Returns a new instance of QuoteOnly.



7
8
9
10
11
# File 'lib/quote-only.rb', line 7

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

Instance Attribute Details

#cssObject

Returns the value of attribute css.



5
6
7
# File 'lib/quote-only.rb', line 5

def css
  @css
end

#friendly_nameObject

Returns the value of attribute friendly_name.



5
6
7
# File 'lib/quote-only.rb', line 5

def friendly_name
  @friendly_name
end

#quoteObject

Returns the value of attribute quote.



5
6
7
# File 'lib/quote-only.rb', line 5

def quote
  @quote
end

#symbolObject

Returns the value of attribute symbol.



5
6
7
# File 'lib/quote-only.rb', line 5

def symbol
  @symbol
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/quote-only.rb', line 5

def url
  @url
end

Instance Method Details

#matchObject



13
14
15
16
# File 'lib/quote-only.rb', line 13

def match
	page = Nokogiri::HTML(open(@url))
	@quote = page.css(@css).text.gsub(/[$,]/, '').to_f
end

#to_sObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/quote-only.rb', line 18

def to_s
	decimal_places_string = '0'

	if(@decimal_places > 0)
		decimal_places_string = @decimal_places.to_s
	end

	quote_rounded = ('%.' + decimal_places_string + 'f') % @quote.round(@decimal_places)
	s = @friendly_name + ' (' + @symbol + ') ' + quote_rounded.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
end