Class: TopStockMovers::Stocks

Inherits:
Object
  • Object
show all
Defined in:
lib/top_stock_movers/stocks.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#changeObject

Returns the value of attribute change.



2
3
4
# File 'lib/top_stock_movers/stocks.rb', line 2

def change
  @change
end

#market_capObject

Returns the value of attribute market_cap.



2
3
4
# File 'lib/top_stock_movers/stocks.rb', line 2

def market_cap
  @market_cap
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/top_stock_movers/stocks.rb', line 2

def name
  @name
end

#percent_changeObject

Returns the value of attribute percent_change.



2
3
4
# File 'lib/top_stock_movers/stocks.rb', line 2

def percent_change
  @percent_change
end

#priceObject

Returns the value of attribute price.



2
3
4
# File 'lib/top_stock_movers/stocks.rb', line 2

def price
  @price
end

#ratingObject

Returns the value of attribute rating.



2
3
4
# File 'lib/top_stock_movers/stocks.rb', line 2

def rating
  @rating
end

#sectorObject

Returns the value of attribute sector.



2
3
4
# File 'lib/top_stock_movers/stocks.rb', line 2

def sector
  @sector
end

#ticker_symbolObject

Returns the value of attribute ticker_symbol.



2
3
4
# File 'lib/top_stock_movers/stocks.rb', line 2

def ticker_symbol
  @ticker_symbol
end

#urlObject

Returns the value of attribute url.



2
3
4
# File 'lib/top_stock_movers/stocks.rb', line 2

def url
  @url
end

#volumeObject

Returns the value of attribute volume.



2
3
4
# File 'lib/top_stock_movers/stocks.rb', line 2

def volume
  @volume
end

Class Method Details

.allObject



6
7
8
# File 'lib/top_stock_movers/stocks.rb', line 6

def self.all
  @@all
end

.scrape_tradingview(category) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/top_stock_movers/stocks.rb', line 10

def self.scrape_tradingview(category)
  doc = Nokogiri::HTML(open("https://www.tradingview.com/markets/stocks-usa/market-movers-#{category}/"))

  doc.css('body div div#js-category-content div div div div#js-screener-container div table tbody tr').each do |row|

    stock = self.new
    stock.url = "https://www.tradingview.com#{row.css('a').attr('href').value}"

    stock_info = row.css('td').collect{|td| td.text}

    stock.ticker_symbol = stock_info[0].split("\n\t\t\t\t\t\t").reject{|c| c.empty?}[0]
    stock.name = stock_info[0].split("\n\t\t\t\t\t\t").reject{|c| c.empty?}[1].gsub(/INC|LTD|PLC|3X|LLC|BOND|SPONSORED|\s{2,}|[,(-]/, "++").split("++")[0]
    stock.price = stock_info[1].to_f
    stock.percent_change = stock_info[2].to_f
    stock.change = stock_info[3].to_f
    stock.rating = stock_info[4]
    stock.volume = stock_info[5].to_f
    stock.market_cap = stock_info[6].to_f.round(1)
    stock.sector = stock_info[10]
    @@all << stock
  end
end