Sha256: ebaa635017b0d183c94ef4c574563d2798578bd863db73c27c79cfd055ead581

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

require "lita"

module Lita
  module Handlers
    class Stocks < Handler
      route %r{^stock ([\w .-_]+)$}i, :stock_info, command: true, help: {
        "stock <symbol>" => "Returns stock price information about the provided stock symbol."
      }



      def stock_info(response)
        symbol = response.matches[0][0]
        data = get_stock_data(symbol)

        response.reply format_response(data)

      rescue Exception => e
        Lita.logger.error("Stock information error: #{e.message}")
        response.reply "Sorry, but there was a problem retrieving stock information."
      end

      private 


      def get_stock_data(symbol)
        resp = http.get("https://www.google.com/finance/info?infotype=infoquoteall&q=#{symbol}")
        raise 'RequestFail' unless resp.status == 200
        MultiJson.load(resp.body.gsub(/\/\//, ''))[0]
      end

      def format_response(data)
        "#{data['name']} (#{data['e']}:#{data['t']}) - #{data['l']} (#{data['c']}, #{data['cp']}%) - 52week high/low: (#{data['hi52']}/#{data['lo52']}) - MktCap: #{data['mc']} - P/E: #{data['pe']}"
      end


    end

    Lita.register_handler(Stocks)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lita-stocks-0.0.3 lib/lita/handlers/stocks.rb
lita-stocks-0.0.2 lib/lita/handlers/stocks.rb