Class: StockInfo::Stock
- Inherits:
-
Object
- Object
- StockInfo::Stock
- Defined in:
- lib/stock_info/stock.rb
Constant Summary collapse
- @@trending =
[]
- @@all =
[]
Instance Attribute Summary collapse
-
#company ⇒ Object
Returns the value of attribute company.
-
#daily_change ⇒ Object
Returns the value of attribute daily_change.
-
#earnings ⇒ Object
Returns the value of attribute earnings.
-
#news ⇒ Object
Returns the value of attribute news.
-
#optionable ⇒ Object
Returns the value of attribute optionable.
-
#price ⇒ Object
Returns the value of attribute price.
-
#rvol ⇒ Object
Returns the value of attribute rvol.
-
#short_ratio ⇒ Object
Returns the value of attribute short_ratio.
-
#symbol ⇒ Object
Returns the value of attribute symbol.
Class Method Summary collapse
- .check_symbol(symbol) ⇒ Object
- .create_or_return(symbol) ⇒ Object
- .market_open ⇒ Object
- .print_trending ⇒ Object
- .trending ⇒ Object
Instance Method Summary collapse
- #get_info(symbol) ⇒ Object
-
#initialize(symbol) ⇒ Stock
constructor
A new instance of Stock.
- #print_info ⇒ Object
- #print_news ⇒ Object
Constructor Details
#initialize(symbol) ⇒ Stock
Returns a new instance of Stock.
5 6 7 8 9 10 |
# File 'lib/stock_info/stock.rb', line 5 def initialize(symbol) @symbol = symbol @news = [] get_info(symbol) @@all << self end |
Instance Attribute Details
#company ⇒ Object
Returns the value of attribute company.
2 3 4 |
# File 'lib/stock_info/stock.rb', line 2 def company @company end |
#daily_change ⇒ Object
Returns the value of attribute daily_change.
2 3 4 |
# File 'lib/stock_info/stock.rb', line 2 def daily_change @daily_change end |
#earnings ⇒ Object
Returns the value of attribute earnings.
2 3 4 |
# File 'lib/stock_info/stock.rb', line 2 def earnings @earnings end |
#news ⇒ Object
Returns the value of attribute news.
2 3 4 |
# File 'lib/stock_info/stock.rb', line 2 def news @news end |
#optionable ⇒ Object
Returns the value of attribute optionable.
2 3 4 |
# File 'lib/stock_info/stock.rb', line 2 def optionable @optionable end |
#price ⇒ Object
Returns the value of attribute price.
2 3 4 |
# File 'lib/stock_info/stock.rb', line 2 def price @price end |
#rvol ⇒ Object
Returns the value of attribute rvol.
2 3 4 |
# File 'lib/stock_info/stock.rb', line 2 def rvol @rvol end |
#short_ratio ⇒ Object
Returns the value of attribute short_ratio.
2 3 4 |
# File 'lib/stock_info/stock.rb', line 2 def short_ratio @short_ratio end |
#symbol ⇒ Object
Returns the value of attribute symbol.
2 3 4 |
# File 'lib/stock_info/stock.rb', line 2 def symbol @symbol end |
Class Method Details
.check_symbol(symbol) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/stock_info/stock.rb', line 31 def self.check_symbol(symbol) open("https://finviz.com/quote.ashx?t=#{symbol}").read true rescue StandardError => e false end |
.create_or_return(symbol) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/stock_info/stock.rb', line 12 def self.create_or_return(symbol) if self.market_open || @@all.any? {|stock| stock.symbol == symbol} if self.market_open stock = @@all.select {|stock| stock.symbol == symbol }[0] stock.get_info(symbol) stock else stock = @@all.select {|stock| stock.symbol == symbol}[0] end else self.new(symbol) end end |
.market_open ⇒ Object
26 27 28 29 |
# File 'lib/stock_info/stock.rb', line 26 def self.market_open time = Time.now.localtime('-05:00') (time.saturday? || time.sunday? || time.hour > 20 || time.hour < 4) ? false : true end |
.print_trending ⇒ Object
61 62 63 64 65 |
# File 'lib/stock_info/stock.rb', line 61 def self.print_trending self.trending.each do |stock| puts "#{stock.symbol} - Price: #{stock.price} - Change: #{stock.daily_change} - Relative Volume: #{stock.rvol}" end end |
.trending ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/stock_info/stock.rb', line 67 def self.trending if self.market_open || @@trending.empty? i = 0 doc = Nokogiri::HTML(open('https://finviz.com/screener.ashx?v=110&s=ta_mostactive')) 10.times do symbol = doc.css('tr.table-dark-row-cp a.screener-link-primary')[i].text i += 1 @@trending << self.create_or_return(symbol) end end @@trending end |
Instance Method Details
#get_info(symbol) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/stock_info/stock.rb', line 38 def get_info(symbol) doc = Nokogiri::HTML(open("https://finviz.com/quote.ashx?t=#{symbol}")) @company = doc.css('table.fullview-title tr')[1].text @rvol = doc.css('table.snapshot-table2 td.snapshot-td2')[-14].text @daily_change = doc.css('table.snapshot-table2 td.snapshot-td2')[-1].text @price = doc.css('table.snapshot-table2 td.snapshot-td2')[-7].text @optionable = doc.css('table.snapshot-table2 td.snapshot-td2')[-18].text @short_ratio = doc.css('table.snapshot-table2 td.snapshot-td2')[22].text @earnings = doc.css('table.snapshot-table2 td.snapshot-td2')[-10].text @earnings = 'None (ETF)' if earnings == '-' end |
#print_info ⇒ Object
57 58 59 |
# File 'lib/stock_info/stock.rb', line 57 def print_info puts "#{company} - Price: #{price} - Change: #{daily_change} - Relative Volume: #{rvol} - Earnings Date: #{earnings} - Optionable: #{optionable} - Short Ratio: #{short_ratio}" end |