Class: Lita::Handlers::OnewheelWolframAlpha
- Inherits:
-
Handler
- Object
- Handler
- Lita::Handlers::OnewheelWolframAlpha
- Defined in:
- lib/lita/handlers/onewheel_wolfram_alpha.rb
Instance Method Summary collapse
- #build_uri(query) ⇒ Object
- #handle_wolfram_query(response) ⇒ Object
- #make_api_call(query) ⇒ Object
- #parse_response(noko_doc, query) ⇒ Object
- #rid_thee_of_extras(str) ⇒ Object
Instance Method Details
#build_uri(query) ⇒ Object
52 53 54 55 |
# File 'lib/lita/handlers/onewheel_wolfram_alpha.rb', line 52 def build_uri(query) uri = config.api_uri.sub '[query]', CGI::escape(query) uri.sub '[appid]', config.app_id end |
#handle_wolfram_query(response) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/lita/handlers/onewheel_wolfram_alpha.rb', line 12 def handle_wolfram_query(response) unless config.app_id and config.api_uri Lita.logger.error 'lita-onewheel-wolfram-alpha: Configuration error!' return end query = response.matches[0][0] api_response = make_api_call query reply = parse_response api_response, query Lita.logger.debug "lita-onewheel-wolfram-alpha: Replying with #{reply}" response.reply reply end |
#make_api_call(query) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/lita/handlers/onewheel_wolfram_alpha.rb', line 45 def make_api_call(query) Lita.logger.debug "lita-onewheel-wolfram-alpha: Making api call for #{query}" uri = build_uri query response = RestClient.get(uri) Nokogiri::XML response.to_s end |
#parse_response(noko_doc, query) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/lita/handlers/onewheel_wolfram_alpha.rb', line 24 def parse_response(noko_doc, query) success_node = noko_doc.xpath('queryresult').attribute('success') Lita.logger.debug "Success attr: #{success_node.to_s}" if success_node.to_s == 'true' pods = noko_doc.xpath('//pod') Lita.logger.debug "Pod title: #{pods[1].attribute('title').to_s}" if pods[1].attribute('title').to_s == 'Plot' pods[1].xpath('//img')[1].attribute('src').to_s else rid_thee_of_extras pods[1].xpath('//plaintext')[1].child.to_s end else "lita-onewheel-wolfram-alpha: Wolfram couldn't parse #{query}." end end |
#rid_thee_of_extras(str) ⇒ Object
41 42 43 |
# File 'lib/lita/handlers/onewheel_wolfram_alpha.rb', line 41 def rid_thee_of_extras(str) str.gsub /\s+\|\s/, ' | ' end |