-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
114 lines (83 loc) · 2.26 KB
/
index.js
File metadata and controls
114 lines (83 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var xml2js = require('xml2js').parseString,
request = require('restler'),
async = require('async');
var Wolfram = function Wolfram(key) {
this.key = key;
}
Wolfram.prototype.ask = function(term, callback) {
if(!this.key) {
return callback('Please set Wolfram App Key', null);
}
var data = {
input : term.query || '' ,
assumption : term.assumption || '' ,
translation : term.translation || false,
reinterpret : term.reinterpret || false,
primary : term.primary || true,
appid : this.key
};
this.setQuery(data);
var query_url = 'http://api.wolframalpha.com/v2/query';
async.waterfall([
function(next) {
//Request query to Wolfram
request.get(query_url, { query: data })
.on('complete', function(results, response) {
if(response.statusCode == 200) {
next(null, results);
}
})
.on('error', function(err, response) {
next(err, null);
});
},
function(results, next) {
//Convert the XML to JSON
xml2js(results, function(error, result) {
next(null, result);
});
},
function(result, next) {
//Check for errors in result
if(result.queryresult.$.error == 'true') {
var err = result.queryresult.error[0].msg;
next(err, null);
}
next(null, result);
}
], function(err, result) {
if(err){
return callback(err, null);
}
Wolfram.prototype.setResults(result.queryresult);
return callback(null, result.queryresult);
});
};
Wolfram.prototype.setResults = function(result) {
this.query_results = result;
}
Wolfram.prototype.getResults = function(callback) {
if(this.query_results) {
return callback(null, this.query_results);
}
return callback('No Results Found. Check if you initialized Query', null);
};
Wolfram.prototype.setQuery = function(query) {
this.query = query;
}
Wolfram.prototype.getQuery = function(callback) {
if(this.query) {
return callback(null, this.query);
}
return callback('No Query set.', null);
};
Wolfram.prototype.getPod = function(callback) {
if(this.query_results) {
return callback(null, this.query_results.pod);
}
return callback('No Results Found. Check if you initialized Query', null)
};
module.exports = {
wolfram : Wolfram,
init : function(key) { return new Wolfram(key) }
};