-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.coffee
More file actions
77 lines (63 loc) · 1.94 KB
/
index.coffee
File metadata and controls
77 lines (63 loc) · 1.94 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
xml2js = require("xml2js").parseString
request = require("restler")
async = require("async")
Wolfram = Wolfram = (key) ->
@key = key
return
Wolfram::ask = (term, callback) ->
return callback("Please set Wolfram App Key", null) unless @key
data =
input: term.query or ""
assumption: term.assumption or ""
translation: term.translation or false
reinterpret: term.reinterpret or false
primary: term.primary or true
appid: @key
@setQuery data
query_url = "http://api.wolframalpha.com/v2/query"
async.waterfall [
(next) ->
#Request query to Wolfram
request.get(query_url,
query: data
).on("complete", (results, response) ->
next null, results if response.statusCode is 200
return
).on "error", (err, response) ->
next err, null
return
(results, next) ->
#Convert the XML to JSON
xml2js results, (error, result) ->
next null, result
return
(result, next) ->
#Check for errors in result
if result.queryresult.$.error is "true"
err = result.queryresult.error[0].msg
next err, null
next null, result
], (err, result) ->
return callback(err, null) if err
Wolfram::setResults result.queryresult
callback null, result.queryresult
return
Wolfram::setResults = (result) ->
@query_results = result
return
Wolfram::getResults = (callback) ->
return callback(null, @query_results) if @query_results
callback "No Results Found. Check if you initialized Query", null
Wolfram::setQuery = (query) ->
@query = query
return
Wolfram::getQuery = (callback) ->
return callback(null, @query) if @query
callback "No Query set.", null
Wolfram::getPod = (callback) ->
return callback(null, @query_results.pod) if @query_results
callback "No Results Found. Check if you initialized Query", null
module.exports =
wolfram: Wolfram
init: (key) ->
new Wolfram(key)