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