Class: Lita::Handlers::Wolframalpha

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita-wolframalpha.rb

Instance Method Summary collapse

Instance Method Details

#wolframalpha(response) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lita-wolframalpha.rb', line 17

def wolframalpha(response)
  question = response.match_data.captures.join
  query = CGI.escape(question)
  url = "http://api.wolframalpha.com/v2/query?input=#{query}&appid=#{config.appid}"

  answer = nil

  begin
    # FIXME Use the version from the gemspec
    doc = Nokogiri::XML(open(url, "User-Agent" => "lita-wolframalpha/#{"1.0"} for Lita (https://lita.io)"))
    answer = doc.css("pod[@id='Result'] plaintext").first.text
  rescue => e
    log.debug e
    raise e
    answer = "Couldn't get a clear answer, see: http://www.wolframalpha.com/input/?i=#{query}"
  end
  response.reply(answer)
end