Sha256: 8674e5f6f745ae4ba4a3624b6578ad9c82c2ce72bbd9b1e4a42939d8f91430e8

Contents?: true

Size: 908 Bytes

Versions: 2

Compression:

Stored size: 908 Bytes

Contents

require 'rest-client'

module Lita
  module Handlers
    class OnewheelDuckDuckGo < Handler
      route /^duck\s+(.*)$/, :search, command: true
      route /^quack\s+(.*)$/, :search, command: true
      route /^ddg\s+(.*)$/, :search, command: true
      route /^fuck\s+(.*)$/, :search, command: true

      def search(response)
        query = response.matches[0][0]
        Lita.logger.debug "Querying for #{query}"
        result = get_result(query)
        Lita.logger.debug "Result: #{result}"
        if result['Abstract'].empty?
          reply = result['AbstractURL']
        else
          reply = result['Abstract'][0..250]
        end
        Lita.logger.debug "Reply: #{reply}"
        response.reply reply
      end

      def get_result(query)
        JSON.parse(RestClient.get("http://api.duckduckgo.com/?q=#{query}&format=json"))
      end

      Lita.register_handler(self)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lita-onewheel-duckduckgo-0.1.0 lib/lita/handlers/onewheel_duckduckgo.rb
lita-onewheel-duckduckgo-0.0.5 lib/lita/handlers/onewheel_duckduckgo.rb