-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
52 lines (50 loc) · 1.6 KB
/
index.js
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
var OAuth = require('oauth');
var needle = require('needle');
module.exports = {
oauth: null,
format: null,
parameters: {},
//yql-node used to output only xml, now the formatAsJSON chainloader should help.
withOAuth: function (consumer_key, consumer_secret) {
this.oauth = new OAuth.OAuth(
'https://query.yahooapis.com/v1/yql/',
'https://query.yahooapis.com/v1/yql/',
consumer_key, //consumer key
consumer_secret, //consumer secret
'1.0',
null,
'HMAC-SHA1'
);
this.oauth.setClientOptions({ requestTokenHttpMethod: 'POST' });
return this;
},
formatAsJSON: function () {
this.format = 'json';
return this;
},
formatAsXML: function () {
this.format = 'xml';
return this;
},
setQueryParameter: function(parameters) {
this.parameters = Object.assign(this.parameters, parameters);
return this;
},
execute: function (query, callback) {
if (this.oauth != null) {
this.oauth.post('https://query.yahooapis.com/v1/yql',
'',
'',
Object.assign({ q: query, format: this.format || 'xml' }, this.parameters),
callback);
} else {
needle
.post("https://query.yahooapis.com/v1/public/yql",
Object.assign({ q: query, format: this.format || 'xml' }, this.parameters),
{ multipart: false },
function (err, res) {
callback(err, res.body);
});
}
}
}