Sha256: 4db282728d7947431436a7c2e96d28e3a4dcc97fe84eb92a18d695b9453ba91b

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require "lita"

module Lita
  module Handlers
    class Stock < Handler
      route %r{action octo}i, :stock_info, command: true, help: {
        "action octo" => "Returns stock price information of OCTO Technology."
      }



      def stock_info(response)
        symbol = "EPA:ALOCT"
        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 octo 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(clean_response(resp.body))[0]
      end

      # Clean up the body and fix any hex formatting that Google does
      def clean_response(body)
        body.gsub!(/\/\//, '')

        # Google sends hex encoded data, we need to fix that
        (33..47).each do |char|
          body.gsub!(/\\x#{char.to_s(16)}/, char.chr)
        end

        body
      end

      def format_response(data)
        line = []
        #line << "#{data['name']}"
        line << "Action OCTO"
        line << "#{data['l']} (#{data['c']}, #{data['cp']}%)"

        line.join ' - '
      end


    end

    Lita.register_handler(Stock)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lita-octo-0.0.1 lib/lita/handlers/stock.rb