class StockInfo::Stock
Attributes
company[RW]
daily_change[RW]
earnings[RW]
news[RW]
optionable[RW]
price[RW]
rvol[RW]
short_ratio[RW]
symbol[RW]
Public Class Methods
check_symbol(symbol)
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# 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
new(symbol)
click to toggle source
# File lib/stock_info/stock.rb, line 5 def initialize(symbol) @symbol = symbol @news = [] get_info(symbol) @@all << self end
print_trending()
click to toggle source
# 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()
click to toggle source
# 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
Public Instance Methods
get_info(symbol)
click to toggle source
# 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()
click to toggle source
# 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
print_news()
click to toggle source
# File lib/stock_info/stock.rb, line 50 def print_news StockInfo::News.get_news(self) @news.each.with_index(1) do |article, i| puts "#{i} | #{article.title} - #{article.source}" end end