Class: Ruboty::Handlers::StockPriceJp

Inherits:
Base
  • Object
show all
Defined in:
lib/ruboty/handlers/stock_price_jp.rb

Instance Method Summary collapse

Instance Method Details

#stock_price(message) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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