2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('yql', function(Y) {
11 * This class adds a sugar class to allow access to YQL (http://developer.yahoo.com/yql/).
15 * Utility Class used under the hood my the YQL class
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)
23 var YQLRequest = function (sql, callback, params, opts) {
29 //Allow format override.. JSON-P-X
31 params.format = Y.YQLRequest.FORMAT;
34 params.env = Y.YQLRequest.ENV;
37 this._params = params;
39 this._callback = callback;
43 YQLRequest.prototype = {
47 * @description Reference to the JSONP instance used to make the queries
53 * @description Holder for the opts argument
59 * @description Holder for the callback argument
65 * @description Holder for the params argument
70 * @description The method that executes the YQL Request.
72 * @returns {YQLRequest}
75 var qs = '', url = ((this._opts && this._opts.proto) ? this._opts.proto : Y.YQLRequest.PROTO);
77 Y.each(this._params, function(v, k) {
78 qs += k + '=' + encodeURIComponent(v) + '&';
81 url += ((this._opts && this._opts.base) ? this._opts.base : Y.YQLRequest.BASE_URL) + qs;
83 var o = (!Y.Lang.isFunction(this._callback)) ? this._callback : { on: { success: this._callback } };
84 if (o.allowCache !== false) {
89 this._jsonp = Y.jsonp(url, o);
91 this._jsonp.url = url;
92 if (o.on && o.on.success) {
93 this._jsonp._config.on.success = o.on.success;
104 * @description Default format to use: json
106 YQLRequest.FORMAT = 'json';
110 * @description Default protocol to use: http
112 YQLRequest.PROTO = 'http';
116 * @description The base URL to query: query.yahooapis.com/v1/public/yql?
118 YQLRequest.BASE_URL = ':/'+'/query.yahooapis.com/v1/public/yql?';
122 * @description The environment file to load: http://datatables.org/alltables.env
124 YQLRequest.ENV = 'http:/'+'/datatables.org/alltables.env';
126 Y.YQLRequest = YQLRequest;
129 * This class adds a sugar class to allow access to YQL (http://developer.yahoo.com/yql/).
132 * @param {String} sql The SQL statement to execute
133 * @param {Function} callback The callback to execute after the query (optional).
134 * @param {Object} params An object literal of extra parameters to pass along (optional).
136 Y.YQL = function(sql, callback, params) {
137 return new Y.YQLRequest(sql, callback, params).send();
142 }, '3.3.0' ,{requires:['jsonp']});