|
525
|
1 |
/* |
|
|
2 |
YUI 3.10.3 (build 2fb5187) |
|
|
3 |
Copyright 2013 Yahoo! Inc. All rights reserved. |
|
|
4 |
Licensed under the BSD License. |
|
|
5 |
http://yuilibrary.com/license/ |
|
|
6 |
*/ |
|
|
7 |
|
|
|
8 |
YUI.add('yql', function (Y, NAME) { |
|
|
9 |
|
|
|
10 |
/** |
|
|
11 |
* This class adds a sugar class to allow access to YQL (http://developer.yahoo.com/yql/). |
|
|
12 |
* @module yql |
|
|
13 |
*/ |
|
|
14 |
/** |
|
|
15 |
* Utility Class used under the hood my the YQL class |
|
|
16 |
* @class YQLRequest |
|
|
17 |
* @constructor |
|
|
18 |
* @param {String} sql The SQL statement to execute |
|
|
19 |
* @param {Function/Object} callback The callback to execute after the query (Falls through to JSONP). |
|
|
20 |
* @param {Object} params An object literal of extra parameters to pass along (optional). |
|
|
21 |
* @param {Object} opts An object literal of configuration options (optional): proto (http|https), base (url) |
|
|
22 |
*/ |
|
|
23 |
var YQLRequest = function (sql, callback, params, opts) { |
|
|
24 |
|
|
|
25 |
if (!params) { |
|
|
26 |
params = {}; |
|
|
27 |
} |
|
|
28 |
params.q = sql; |
|
|
29 |
//Allow format override.. JSON-P-X |
|
|
30 |
if (!params.format) { |
|
|
31 |
params.format = Y.YQLRequest.FORMAT; |
|
|
32 |
} |
|
|
33 |
if (!params.env) { |
|
|
34 |
params.env = Y.YQLRequest.ENV; |
|
|
35 |
} |
|
|
36 |
|
|
|
37 |
this._context = this; |
|
|
38 |
|
|
|
39 |
if (opts && opts.context) { |
|
|
40 |
this._context = opts.context; |
|
|
41 |
delete opts.context; |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
if (params && params.context) { |
|
|
45 |
this._context = params.context; |
|
|
46 |
delete params.context; |
|
|
47 |
} |
|
|
48 |
|
|
|
49 |
this._params = params; |
|
|
50 |
this._opts = opts; |
|
|
51 |
this._callback = callback; |
|
|
52 |
|
|
|
53 |
}; |
|
|
54 |
|
|
|
55 |
YQLRequest.prototype = { |
|
|
56 |
/** |
|
|
57 |
* @private |
|
|
58 |
* @property _jsonp |
|
|
59 |
* @description Reference to the JSONP instance used to make the queries |
|
|
60 |
*/ |
|
|
61 |
_jsonp: null, |
|
|
62 |
/** |
|
|
63 |
* @private |
|
|
64 |
* @property _opts |
|
|
65 |
* @description Holder for the opts argument |
|
|
66 |
*/ |
|
|
67 |
_opts: null, |
|
|
68 |
/** |
|
|
69 |
* @private |
|
|
70 |
* @property _callback |
|
|
71 |
* @description Holder for the callback argument |
|
|
72 |
*/ |
|
|
73 |
_callback: null, |
|
|
74 |
/** |
|
|
75 |
* @private |
|
|
76 |
* @property _params |
|
|
77 |
* @description Holder for the params argument |
|
|
78 |
*/ |
|
|
79 |
_params: null, |
|
|
80 |
/** |
|
|
81 |
* @private |
|
|
82 |
* @property _context |
|
|
83 |
* @description The context to execute the callback in |
|
|
84 |
*/ |
|
|
85 |
_context: null, |
|
|
86 |
/** |
|
|
87 |
* @private |
|
|
88 |
* @method _internal |
|
|
89 |
* @description Internal Callback Handler |
|
|
90 |
*/ |
|
|
91 |
_internal: function () { |
|
|
92 |
this._callback.apply(this._context, arguments); |
|
|
93 |
}, |
|
|
94 |
/** |
|
|
95 |
* @method send |
|
|
96 |
* @description The method that executes the YQL Request. |
|
|
97 |
* @chainable |
|
|
98 |
* @return {YQLRequest} |
|
|
99 |
*/ |
|
|
100 |
send: function () { |
|
|
101 |
var qs = [], url = ((this._opts && this._opts.proto) ? this._opts.proto : Y.YQLRequest.PROTO), o; |
|
|
102 |
|
|
|
103 |
Y.Object.each(this._params, function (v, k) { |
|
|
104 |
qs.push(k + '=' + encodeURIComponent(v)); |
|
|
105 |
}); |
|
|
106 |
|
|
|
107 |
qs = qs.join('&'); |
|
|
108 |
|
|
|
109 |
url += ((this._opts && this._opts.base) ? this._opts.base : Y.YQLRequest.BASE_URL) + qs; |
|
|
110 |
|
|
|
111 |
o = (!Y.Lang.isFunction(this._callback)) ? this._callback : { on: { success: this._callback } }; |
|
|
112 |
|
|
|
113 |
o.on = o.on || {}; |
|
|
114 |
this._callback = o.on.success; |
|
|
115 |
|
|
|
116 |
o.on.success = Y.bind(this._internal, this); |
|
|
117 |
|
|
|
118 |
Y.log('URL: ' + url, 'info', 'yql'); |
|
|
119 |
this._send(url, o); |
|
|
120 |
return this; |
|
|
121 |
}, |
|
|
122 |
/** |
|
|
123 |
* Private method to send the request, overwritten in plugins |
|
|
124 |
* @method _send |
|
|
125 |
* @private |
|
|
126 |
* @param {String} url The URL to request |
|
|
127 |
* @param {Object} o The config object |
|
|
128 |
*/ |
|
|
129 |
_send: function() { |
|
|
130 |
//Overwritten in plugins |
|
|
131 |
} |
|
|
132 |
}; |
|
|
133 |
|
|
|
134 |
/** |
|
|
135 |
* @static |
|
|
136 |
* @property FORMAT |
|
|
137 |
* @description Default format to use: json |
|
|
138 |
*/ |
|
|
139 |
YQLRequest.FORMAT = 'json'; |
|
|
140 |
/** |
|
|
141 |
* @static |
|
|
142 |
* @property PROTO |
|
|
143 |
* @description Default protocol to use: http |
|
|
144 |
*/ |
|
|
145 |
YQLRequest.PROTO = 'http'; |
|
|
146 |
/** |
|
|
147 |
* @static |
|
|
148 |
* @property BASE_URL |
|
|
149 |
* @description The base URL to query: query.yahooapis.com/v1/public/yql? |
|
|
150 |
*/ |
|
|
151 |
YQLRequest.BASE_URL = ':/' + '/query.yahooapis.com/v1/public/yql?'; |
|
|
152 |
/** |
|
|
153 |
* @static |
|
|
154 |
* @property ENV |
|
|
155 |
* @description The environment file to load: http://datatables.org/alltables.env |
|
|
156 |
*/ |
|
|
157 |
YQLRequest.ENV = 'http:/' + '/datatables.org/alltables.env'; |
|
|
158 |
|
|
|
159 |
Y.YQLRequest = YQLRequest; |
|
|
160 |
|
|
|
161 |
/** |
|
|
162 |
* This class adds a sugar class to allow access to YQL (http://developer.yahoo.com/yql/). |
|
|
163 |
* @class YQL |
|
|
164 |
* @constructor |
|
|
165 |
* @param {String} sql The SQL statement to execute |
|
|
166 |
* @param {Function} callback The callback to execute after the query (optional). |
|
|
167 |
* @param {Object} params An object literal of extra parameters to pass along (optional). |
|
|
168 |
* @param {Object} opts An object literal of configuration options (optional): proto (http|https), base (url) |
|
|
169 |
*/ |
|
|
170 |
Y.YQL = function (sql, callback, params, opts) { |
|
|
171 |
return new Y.YQLRequest(sql, callback, params, opts).send(); |
|
|
172 |
}; |
|
|
173 |
|
|
|
174 |
|
|
|
175 |
}, '3.10.3', {"requires": ["oop"]}); |