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
56 57 58 59 |
# File 'lib/lita/handlers/onewheel_wolfram_alpha.rb', line 56 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
48 49 50 51 52 53 54 |
# File 'lib/lita/handlers/onewheel_wolfram_alpha.rb', line 48 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) Lita.logger.debug 'lita-onewheel-wolfram-alpha: ' + response.to_s 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 40 41 42 |
# 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 "lita-onewheel-wolfram-alpha: Success attr: #{success_node.to_s}" if success_node.to_s == 'true' pods = noko_doc.xpath('//pod') Lita.logger.debug "lita-onewheel-wolfram-alpha: 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 ["Nope, no #{query} to see here.", "#{query}?", 'What\'s that, now?' ].sample end end |
#rid_thee_of_extras(str) ⇒ Object
44 45 46 |
# File 'lib/lita/handlers/onewheel_wolfram_alpha.rb', line 44 def rid_thee_of_extras(str) str.gsub /\s+\|\s/, ' | ' end |