Sha256: 4dcf2389fc6d5c48ee098923017ef30f6916601405cb729f88556ef13ef3d9c7

Contents?: true

Size: 773 Bytes

Versions: 1

Compression:

Stored size: 773 Bytes

Contents

require "cgi"
require "json"
require "open-uri"

module Lita
  module Handlers
    class Wikipedia < Handler
      route(/^(?:wiki|wikipedia)\s+(.*)/i, :wikipedia, command: true,
            help: { t("help.wikipedia_key") => t("help.wikipedia_value")})

      def wikipedia(response)
        url = "http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&format=json&exintro=&explaintext=&inprop=url&titles=#{CGI.escape(response.matches.first.first)}&redirects="
        result = JSON.parse(open(URI.parse(URI.encode(url.strip))).read)
        page = result['query']['pages'].first[1]
        response.reply(page['extract'].split("\n").first)
        response.reply("Source: #{page['fullurl']}")
      end
    end

    Lita.register_handler(Wikipedia)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lita-wikipedia-0.0.1 lib/lita/handlers/wikipedia.rb