-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
45 lines (41 loc) · 1.01 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
var got = require('got'),
Promise = require('es6-promise').Promise,
stringFormat = require('string-format'),
oauth = require('./OAuth'),
baseUrl = 'https://query.yahooapis.com/v1/public/yql/',
format = 'json';
module.exports = {
options: function (options) {
var data = options || {};
if (data.OAuth) {
oauth.init(data.OAuth);
}
if (options.format) {
if (options.format === 'json' || options.format === 'xml') {
format = options.format;
}
}
return this;
},
query: function (query) {
return new Promise(function (fulfill, reject) {
if (oauth.valid) {
oauth.post(query, format).then(function (response) {
fulfill(response);
}).catch(function (error) {
reject(error);
});
} else {
got(stringFormat('{url}?q={query}&format={format}', { url: baseUrl, query: query, format: format })).then(function (response) {
try {
fulfill(response.body);
} catch (e) {
reject(e);
}
}).catch(function (error) {
reject(error);
})
}
});
}
}