Class: StockInfo::Stock

Inherits:
Object
  • Object
show all
Defined in:
lib/stock_info/stock.rb

Constant Summary collapse

[]
@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#companyObject

Returns the value of attribute company.



2
3
4
# File 'lib/stock_info/stock.rb', line 2

def company
  @company
end

#daily_changeObject

Returns the value of attribute daily_change.



2
3
4
# File 'lib/stock_info/stock.rb', line 2

def daily_change
  @daily_change
end

#earningsObject

Returns the value of attribute earnings.



2
3
4
# File 'lib/stock_info/stock.rb', line 2

def earnings
  @earnings
end

#newsObject

Returns the value of attribute news.



2
3
4
# File 'lib/stock_info/stock.rb', line 2

def news
  @news
end

#optionableObject

Returns the value of attribute optionable.



2
3
4
# File 'lib/stock_info/stock.rb', line 2

def optionable
  @optionable
end

#priceObject

Returns the value of attribute price.



2
3
4
# File 'lib/stock_info/stock.rb', line 2

def price
  @price
end

#rvolObject

Returns the value of attribute rvol.



2
3
4
# File 'lib/stock_info/stock.rb', line 2

def rvol
  @rvol
end

#short_ratioObject

Returns the value of attribute short_ratio.



2
3
4
# File 'lib/stock_info/stock.rb', line 2

def short_ratio
  @short_ratio
end

#symbolObject

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_openObject



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


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


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


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


50
51
52
53
54
55
# 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