class StockInfo::News

Attributes

source[RW]
symbol[RW]
title[RW]

Public Class Methods

get_news(stock) click to toggle source
# File lib/stock_info/news.rb, line 4
def self.get_news(stock)
  symbol = stock.symbol
  stock.news.clear
  doc = Nokogiri::HTML(open("https://finviz.com/quote.ashx?t=#{symbol}"))
  i = 0
  10.times do
    article = new
    article.symbol = symbol
    sources = doc.css('table#news-table.fullview-news-outer tr span') - doc.css('table#news-table.fullview-news-outer tr span.body-table-news-gain')
    article.title = doc.css('table#news-table.fullview-news-outer tr')[i].css('.tab-link-news').text
    article.source = sources[i].text
    article.link = doc.css('table#news-table.fullview-news-outer tr td a')[i].attribute('href').value
    stock.news << article
    i += 1
  end
end
news_menu(stock) click to toggle source
# File lib/stock_info/news.rb, line 21
def self.news_menu(stock)
  input = nil
  stock.print_news
  while input != 'menu' && input != 'exit'
    puts "Enter the number of an article you would like to open in your web browser, 'menu' to return to the main menu, 'refresh' to check for recent news, or 'exit' to exit."
    input = gets.strip
    if input.to_i > 0 && input.to_i <= 10
      open_url(stock.news[input.to_i - 1].link)
    elsif input.casecmp('refresh').zero?
      stock.print_news
    end
  end
  'exit' if input == 'exit'
end
open_url(link) click to toggle source
# File lib/stock_info/news.rb, line 36
def self.open_url(link)
  if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
    system "start #{link} -s"
  elsif RbConfig::CONFIG['host_os'] =~ /darwin/
    system "open #{link} -s"
  elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/
    system "xdg-open #{link} 2> /dev/null"
  end
end