var xml = require('libxmljs'),
    request = require('request')

var Client = exports.Client = function Client(appKey) {
  this.appKey = appKey
}

Client.prototype.query = function(input, cb) {
  if(!this.appKey) {
    return cb("Application key not set", null)
  }

  var uri = 'http://api.wolframalpha.com/v2/query?input=' + encodeURIComponent(input) + '&primary=true&appid=' + this.appKey

  request(uri, function(error, response, body) {
    if(!error && response.statusCode == 200) {
      var doc = xml.parseXml(body), root = doc.root()

      if(root.attr('error').value() != 'false') {
        var message = root.get('//error/msg').text()
        return cb(message, null)
      } else {
        var pods = root.find('pod').map(function(pod) {
          var subpods = pod.find('subpod').map(function(node) {
            return {
              title: node.attr('title').value(),
              value: node.get('plaintext').text(),
              image: node.get('img').attr('src').value()
            }
          })
          var primary = (pod.attr('primary') && pod.attr('primary').value()) == 'true'
          return { subpods: subpods, primary: primary }
        })
       return cb(null, pods)
      }
    }
  })
}

exports.createClient = function(appKey) {
  return new Client(appKey)
}

@@ -1200,24 +1200,66 @@
 ds)%0A      %7D%0A
+    %7D else %7B%0A      return cb(error, null)%0A
     %7D%0A  %7D)%0A%7D

Handle network errors instead of failing silently