class Ruboty::Handlers::StockPriceJp

Public Instance Methods

stock_price(message) click to toggle source
# File lib/ruboty/handlers/stock_price_jp.rb, line 10
def stock_price(message)
  url = "https://www.google.com/finance?q=#{message['ticker']}"

  charset = nil
  html = open(url) do |f|
    charset = f.charset
    f.read
  end
  page = Nokogiri::HTML.parse(html, nil, charset)

  price = page.css('meta[itemprop="price"]').first['content']
  company = page.css('meta[itemprop="name"]').first['content']
  change = page.css('meta[itemprop="priceChange"]').first['content']
  percent = page.css('meta[itemprop="priceChangePercent"]').first['content']
  emoji = case change[0]
          when '+'
            ':chart_with_upwards_trend:'
          when '-'
            ':chart_with_downwards_trend:'
          end

  message.reply("*#{company}*: #{price} _#{change}(#{percent}%)_ #{emoji}")
end