Class: StockInfo::Stock

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

Defined Under Namespace

Classes: News

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol) ⇒ Stock

Returns a new instance of Stock.



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

def initialize(symbol)
    @symbol = symbol
    self.get_info(symbol)
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



7
8
9
10
11
12
13
14
# File 'lib/stock_info/stock.rb', line 7

def self.check_symbol(symbol)
    begin
        open("https://finviz.com/quote.ashx?t=#{symbol}").read
        true
    rescue => e
        false
    end
end


37
38
39
40
41
# File 'lib/stock_info/stock.rb', line 37

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


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/stock_info/stock.rb', line 42

def self.trending
    trending = []
    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.new(symbol)
    end
    trending
end

Instance Method Details

#get_info(symbol) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/stock_info/stock.rb', line 15

def get_info(symbol)
    doc = Nokogiri::HTML(open("https://finviz.com/quote.ashx?t=#{symbol}"))
    self.company = doc.css('table.fullview-title tr')[1].text
    self.rvol = doc.css("table.snapshot-table2 td.snapshot-td2")[-14].text 
    self.daily_change = doc.css("table.snapshot-table2 td.snapshot-td2")[-1].text
    self.price = doc.css("table.snapshot-table2 td.snapshot-td2")[-7].text
    self.optionable = doc.css("table.snapshot-table2 td.snapshot-td2")[-18].text 
    self.short_ratio = doc.css("table.snapshot-table2 td.snapshot-td2")[22].text
    self.earnings = doc.css("table.snapshot-table2 td.snapshot-td2")[-10].text 
    if self.earnings == '-'
        self.earnings = "None (ETF)"
    end
end


34
35
36
# File 'lib/stock_info/stock.rb', line 34

def print_info
    puts "#{self.company} - Price: #{self.price} - Change: #{self.daily_change} - Relative Volume: #{self.rvol} - Earnings Date: #{self.earnings} - Optionable: #{self.optionable} - Short Ratio: #{self.short_ratio}"
end


28
29
30
31
32
33
# File 'lib/stock_info/stock.rb', line 28

def print_news
    self.news = News.get_news(self.symbol)
    self.news.each.with_index(1) do |article, i|
        puts "#{i} | #{article.title} - #{article.source}"
    end
end