<commit_before>module Lita
  module Handlers
    class WolframAlpha < Handler
    end

    Lita.register_handler(WolframAlpha)
  end
end<commit_msg>lib: config, route, and #query<commit_after>require "nokogiri"

module Lita
  module Handlers
    class WolframAlpha < Handler

      API_URL = "http://api.wolframalpha.com/v2/query"

      config :app_id, type: String, required: true

      route(/^wa\s+(.+)/i, :query, command: true,
                    help: {t("help.query_key") => t("help.query_value")})

      def query(response)
        input = response.matches.first.first
        http_response = http.get(
          API_URL,
          input: input,
          format: "plaintext",
          appid: config.app_id
        )
        return if http_response.status != 200
        xml = Nokogiri::XML(http_response.body)
        result = xml.at_xpath("//pod[@primary='true']//plaintext")
        return response.reply result.content if result
        result = xml.at_xpath("//pod[not(@title='Input interpretation')]//plaintext[string-length(text()) > 0]")
        response.reply result ? result.content : "¯\\_(ツ)_/¯"
      end

    end

    Lita.register_handler(WolframAlpha)
  end
end
{'fraction_non_alphanumeric': 0.12699747687132043, 'fraction_numerical': 0.004205214465937763, 'mean_word_length': 2.5843373493975905, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4649774', 'n_tokens_mistral': 392, 'n_tokens_neox': 376, 'n_words': 77}