Class: Lita::Handlers::WolframAlpha

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/wolfram_alpha.rb

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#query(response) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lita/handlers/wolfram_alpha.rb', line 16

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
  link = "http://www.wolframalpha.com/input/?i=#{CGI.escape(input)}"
  xml = Nokogiri::XML(http_response.body)
  primary = xml.at_xpath("//pod[@primary='true']//plaintext[string-length(text()) > 0]")
  if primary
    response.reply format_reply(primary)
    response.reply link unless config.hide_link
  else
    secondary = xml.at_xpath("//pod[not(@id='Input')]//plaintext[string-length(text()) > 0]")
    if secondary
      response.reply format_reply(secondary)
      response.reply link unless config.hide_link
    else
      response.reply "¯\\_(ツ)_/¯"
    end
  end
end