-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathyql.js
336 lines (296 loc) · 6.41 KB
/
yql.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
Software Copyright License Agreement (BSD License)
Copyright (c) 2010, Yahoo! Inc.
All rights reserved.
*/
var request = require('request');
var url = require('url');
var _ = require('underscore');
/**
* YQL constructor
*
* @example
* `var query = new YQL('SHOW TABLES');`
*
* @module YQL
* @class YQL
* @param {String} query A YQL query
* @param {Object} [config] A hash of configuration options
* @public
* @constructor
*/
function YQL (query, config) {
config = config || {};
if (!query) {
throw new Error(YQL.ERROR.missingQuery);
}
if (!(this instanceof YQL)) {
return new YQL(query, config);
}
/**
* The YQL query string
*
* @property query
* @public
* @type {String}
*/
this.query = query;
/**
* A hash of values to replace in the query
*
* @property params
* @public
* @type {Object}
*/
this.params = {};
/**
* A hash of configuration options
*
* @property config
* @public
* @type {Object}
*/
this.config = _.defaults(config, YQL.DEFAULT_CONFIG);
}
/**
* Sets a variable value for use in the YQL query
*
* @example
* `query.setParam('name', 'derek');`
*
* @method setParam
* @public
* @chainable
* @param key {String}
* @param value {String}
* @return {Object} This instance
*/
YQL.prototype.setParam = function (key, value) {
this.params[key] = value;
return this;
};
/**
* Sets an array of variable values for use in the YQL query
*
* @example
* ```
* query.setParams({
* city: 'San Francisco',
* state: 'CA'
* });
* ```
*
* @method setParams
* @public
* @chainable
* @param params {Object}
* @return {Object} This instance
*/
YQL.prototype.setParams = function (params) {
this.params = _.extend(this.params, params);
return this;
};
/**
* Sets a YQL option
*
* @example
* `query.setOption('ssl', true);`
*
* @method setOption
* @public
* @chainable
* @param key {String}
* @param value {String}
* @return {Object} This instance
*/
YQL.prototype.setConfig = function (key, value) {
this.config[key] = value;
return this;
};
/**
* Sets a batch of YQL options
*
* @example
* ```
* query.setOptions({
* env: 'http://example.com/path/to/my.env',
* headers: {'User-Agent': 'request'},
* ssl: true,
* timout: 500
* });
* ```
*
* @method setOptions
* @public
* @chainable
* @param options {Object}
* @return {Object} This instance
*/
YQL.prototype.setConfigs = function (config) {
this.config = _.extend(this.config, config);
return this;
};
/**
* Gets the URL object of the HTTP request
*
* @example
* ```
* var url = query.getURL()
* ```
* @method getURL
* @public
* @return {String} URL
*/
YQL.prototype.getURL = function () {
var baseURL = this.config.baseURL[this.config.ssl ? 'https' : 'http'];
// Create base url object
var urlObj = url.parse(baseURL, true);
// delete urlObj.search
_.extend(urlObj.query, {
format: 'json',
env: this.config.env,
q: this.query
});
// Extend query with params object
urlObj.query = _.extend({}, urlObj.query, this.params);
this._urlObj = urlObj;
return urlObj.format();
};
/**
* Executes the query
*
* @example
* ```
* var query = new YQL(yqlQuery);
* query.exec(callbackFn)
* ```
* @method exec
* @param {Function} [callback] A function to be executed with arguments `(error, response)`.
* @public
*/
YQL.prototype.exec = function (callback) {
var url = this.getURL();
var config = {
headers: this.config.headers,
method: 'GET',
timeout: parseInt(this.config.timeout, 10),
url: url
};
var handler = this._handleResponse.bind(this, callback);
// Execute the YQL request
this._httpRequest(config, handler);
};
/**
* Executes an HTTP request (via Request).
*
* @method _httpRequest
* @private
*/
YQL.prototype._httpRequest = function () {
request.apply(null, arguments)
};
/**
* The YQL#exec response handler
*
* @method _handleResponse
* @private
*/
YQL.prototype._handleResponse = function (callback, error, response, body) {
if (error) {
return callback(error);
}
if (body) {
parseJSON(body, function (error, body) {
if (error) {
return callback(error, null);
}
error = null;
// Request returned an error, create an error object
if (body.error) {
error = new Error(body.error.description);
}
if (callback) {
callback(error, body);
}
});
}
else {
throw new Error(YQL.ERROR.missingBody, null);
}
};
/**
* The original API, for backwards compatibility
*
* @method exec
* @public
* @static
* @deprecated
* @param {Object} query
* @param {Object} params
* @param {Object} options
* @param {Object} callback Arguments: `(response)`
*/
YQL.exec = function (query, callback, params, config) {
if (typeof query !== 'string') {
throw new Error(YQL.ERROR.invalidParameter + ': query')
}
return YQL(query).setParams(params).setConfigs(config).exec(function (error, results) {
if (error) {
results = results || {};
results.error = results.error || {};
results.error.description = error.code;
}
callback(results)
});
};
/**
* A default configuration object
*
* @property DEFAULT_CONFIG
* @public
* @static
* @type {Object}
*/
YQL.DEFAULT_CONFIG = {
baseURL: {
http: 'http://query.yahooapis.com/v1/public/yql',
https: 'https://query.yahooapis.com/v1/public/yql'
},
env: 'http://datatables.org/alltables.env',
headers: {},
ssl: false,
timeout: 0 // 0 = No timeout
};
/**
* A hash of error messages
*
* @property ERROR
* @private
* @static
* @type {Object}
*/
YQL.ERROR = {
missingQuery: 'Missing YQL query',
missingBody: 'Missing response body',
invalidParameter: 'Missing or invalid parameter'
};
module.exports = YQL;
/* Utilities */
/**
* Gracefully parse JSON
*
* @method parseJSON
* @param {String} string
* @param {Function} callback
* @private
*/
function parseJSON(string, callback) {
var result = null,
error = null;
try {
result = JSON.parse(string);
} catch (e) {
error = e;
}
callback(error, result);
}