Created
February 25, 2011 21:05
-
-
Save ericf/844490 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * This class adds a sugar class to allow access to YQL (http://developer.yahoo.com/yql/). | |
| * @module yql | |
| */ | |
| /** | |
| * Utility Class used under the hood my the YQL class | |
| * @class YQLRequest | |
| * @constructor | |
| * @param {String} sql The SQL statement to execute | |
| * @param {Function/Object} callback The callback to execute after the query (Falls through to JSONP). | |
| * @param {Object} params An object literal of extra parameters to pass along (optional). | |
| * @param {Object} opts An object literal of configuration options (optional): proto (http|https), base (url) | |
| */ | |
| var YQLRequest = function (sql, callback, params, opts) { | |
| if (!params) { | |
| params = {}; | |
| } | |
| params.q = sql; | |
| //Allow format override.. JSON-P-X | |
| if (!params.format) { | |
| params.format = Y.YQLRequest.FORMAT; | |
| } | |
| if (!params.env) { | |
| params.env = Y.YQLRequest.ENV; | |
| } | |
| this._params = params; | |
| this._opts = opts; | |
| this._callback = callback; | |
| }; | |
| YQLRequest.prototype = { | |
| /** | |
| * @private | |
| * @property _jsonp | |
| * @description Reference to the JSONP instance used to make the queries | |
| */ | |
| _jsonp: null, | |
| /** | |
| * @private | |
| * @property _opts | |
| * @description Holder for the opts argument | |
| */ | |
| _opts: null, | |
| /** | |
| * @private | |
| * @property _callback | |
| * @description Holder for the callback argument | |
| */ | |
| _callback: null, | |
| /** | |
| * @private | |
| * @property _params | |
| * @description Holder for the params argument | |
| */ | |
| _params: null, | |
| /** | |
| * @method send | |
| * @description The method that executes the YQL Request. | |
| * @chainable | |
| * @returns {YQLRequest} | |
| */ | |
| send: function() { | |
| var qs = '', url = ((this._opts && this._opts.proto) ? this._opts.proto : Y.YQLRequest.PROTO); | |
| Y.each(this._params, function(v, k) { | |
| qs += k + '=' + encodeURIComponent(v) + '&'; | |
| }); | |
| url += ((this._opts && this._opts.base) ? this._opts.base : Y.YQLRequest.BASE_URL) + qs; | |
| var o = (!Y.Lang.isFunction(this._callback)) ? this._callback : { on: { success: this._callback } }; | |
| if (o.allowCache !== false) { | |
| o.allowCache = true; | |
| } | |
| this._jsonp = Y.YQLRequest._JSONPRequests[url]; | |
| if (this._jsonp && o.allowCache) { | |
| if (o.on && o.on.success) { | |
| this._jsonp._config.on.success = o.on.success; | |
| } | |
| this._jsonp.send(); | |
| } else { | |
| this._jsonp = Y.YQLRequest._JSONPRequests[url] = Y.jsonp(url, o); | |
| } | |
| return this; | |
| } | |
| }; | |
| /** | |
| * @static | |
| * @property FORMAT | |
| * @description Default format to use: json | |
| */ | |
| YQLRequest.FORMAT = 'json'; | |
| /** | |
| * @static | |
| * @property PROTO | |
| * @description Default protocol to use: http | |
| */ | |
| YQLRequest.PROTO = 'http'; | |
| /** | |
| * @static | |
| * @property BASE_URL | |
| * @description The base URL to query: query.yahooapis.com/v1/public/yql? | |
| */ | |
| YQLRequest.BASE_URL = ':/'+'/query.yahooapis.com/v1/public/yql?'; | |
| /** | |
| * @static | |
| * @property ENV | |
| * @description The environment file to load: http://datatables.org/alltables.env | |
| */ | |
| YQLRequest.ENV = 'http:/'+'/datatables.org/alltables.env'; | |
| /** | |
| * @static | |
| * @private | |
| * @property _JSONPRequests | |
| * @description A simple cache of query -> JSONPReqeust instance to help YQL request caching | |
| */ | |
| YQLRequest._JSONPRequests = {}; | |
| Y.YQLRequest = YQLRequest; | |
| /** | |
| * This class adds a sugar class to allow access to YQL (http://developer.yahoo.com/yql/). | |
| * @class YQL | |
| * @constructor | |
| * @param {String} sql The SQL statement to execute | |
| * @param {Function} callback The callback to execute after the query (optional). | |
| * @param {Object} params An object literal of extra parameters to pass along (optional). | |
| */ | |
| Y.YQL = function(sql, callback, params) { | |
| return new Y.YQLRequest(sql, callback, params).send(); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment