Class: StockScraper::Stock

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#change_percentObject

Returns the value of attribute change_percent.



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

def change_percent
  @change_percent
end

#last_priceObject

Returns the value of attribute last_price.



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

def last_price
  @last_price
end

#market_timeObject

Returns the value of attribute market_time.



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

def market_time
  @market_time
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#symbolObject

Returns the value of attribute symbol.



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

def symbol
  @symbol
end

Class Method Details

.scrape_yahooObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/stock_scraper/stock.rb', line 4

def self.scrape_yahoo
  doc = Nokogiri::HTML(open("https://finance.yahoo.com/trending-tickers/"))
  scraped_stocks = []
  doc.css("tbody tr").each do |column|
    stock_obj = self.new
    stock_obj.symbol = column.css("td.data-col0")[0].children[0].values[2]
    stock_obj.name = column.css("td.data-col0")[0].children[0].values[1]
    stock_obj.last_price = column.css("td.data-col2")[0].children.inner_text
    stock_obj.market_time = column.css("td.data-col3")[0].children.inner_text
    stock_obj.change_percent = column.css("td.data-col5")[0].children.inner_text
    scraped_stocks << stock_obj
  end
  scraped_stocks
end