Skip to content

Commit

Permalink
Initial commit of YQL Module
Browse files Browse the repository at this point in the history
  • Loading branch information
davglass committed Oct 15, 2009
1 parent e9495df commit 6e6ae6f
Show file tree
Hide file tree
Showing 8 changed files with 586 additions and 0 deletions.
133 changes: 133 additions & 0 deletions build/yql/yql-debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
YUI.add('yql', function(Y) {

//Global storage for the callbacks
if (!YUI.yql) {
YUI.yql = {};
}
/**
* This class adds a sugar class to allow access to YQL (http://developer.yahoo.com/yql/).
* @module yql
*/
/**
* This class adds a sugar class to allow access to YQL (http://developer.yahoo.com/yql/).
* @class yql
* @extends Event.Target
* @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).
* @param {Object} opts An object literal of extra options to pass along to the Get Utility (optional).
*/
var BASE_URL = 'http:/'+'/query.yahooapis.com/v1/public/yql?',
yql = function (sql, callback, params, opts) {
yql.superclass.constructor.apply(this);
this._query(sql, callback, params, opts);
};

Y.extend(yql, Y.EventTarget, {
/**
* @private
* @property _cb
* @description The callback method
*/
_cb: null,
/**
* @private
* @property _stamp
* @description The method name on the Global YUI object we use as the callback.
*/
_stamp: null,
/**
* @private
* @method _receiver
* @description The global callback that get's called from Get.
* @param {Object} q The JSON object from YQL.
*/
_receiver: function(q) {
if (q.query) {
this.fire('query', q.query);
}
if (q.error) {
this.fire('error', q.error);
}
if (this._cb) {
this._cb(q);
}
delete YUI.yql[this._stamp];
},
/**
* @private
* @method _query
* @description Builds the query and fire the Get call.
* @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).
* @param {Object} opts An object literal of extra options to pass along to the Get Utility (optional).
* @return Self
*/
_query: function(sql, callback, params, opts) {
var st = Y.stamp({}), qs = '', url;
//Must replace the dashes with underscrores
st = st.replace(/-/g, '_');

this._stamp = st;

this._cb = callback;

YUI.yql[st] = Y.bind(this._receiver, this);

if (!params) {
params = {};
}
params.q = sql;
params.format = 'json';
params.callback = "YUI.yql." + st;
if (!params.env) {
params.env = 'http:/'+'/datatables.org/alltables.env';
}

Y.each(params, function(v, k) {
qs += k + '=' + encodeURIComponent(v) + '&';
});

if (!opts) {
opts = {};
}
opts.autopurge = true;
opts.context = this;
opts.onTimeout = function(o){
this.fire('timeout', o);
if (this._cb) {
this._cb(o);
this._cb = null;
}
};

url = BASE_URL + qs;
Y.Get.script(url, opts);
return this;
}
});
/**
* @event query
* @description Fires when the Query returns.
* @type {Event.Custom}
*/

/**
* @event error
* @description Fires when an error occurs.
* @type {Event.Custom}
*/

/**
* @event timeout
* @description Fires when the request has timed-out.
* @type {Event.Custom}
*/

Y.yql = yql;



}, '@VERSION@' ,{requires:['get', 'event-custom'], supersedes:['get', 'event-custom']});
1 change: 1 addition & 0 deletions build/yql/yql-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

133 changes: 133 additions & 0 deletions build/yql/yql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
YUI.add('yql', function(Y) {

//Global storage for the callbacks
if (!YUI.yql) {
YUI.yql = {};
}
/**
* This class adds a sugar class to allow access to YQL (http://developer.yahoo.com/yql/).
* @module yql
*/
/**
* This class adds a sugar class to allow access to YQL (http://developer.yahoo.com/yql/).
* @class yql
* @extends Event.Target
* @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).
* @param {Object} opts An object literal of extra options to pass along to the Get Utility (optional).
*/
var BASE_URL = 'http:/'+'/query.yahooapis.com/v1/public/yql?',
yql = function (sql, callback, params, opts) {
yql.superclass.constructor.apply(this);
this._query(sql, callback, params, opts);
};

Y.extend(yql, Y.EventTarget, {
/**
* @private
* @property _cb
* @description The callback method
*/
_cb: null,
/**
* @private
* @property _stamp
* @description The method name on the Global YUI object we use as the callback.
*/
_stamp: null,
/**
* @private
* @method _receiver
* @description The global callback that get's called from Get.
* @param {Object} q The JSON object from YQL.
*/
_receiver: function(q) {
if (q.query) {
this.fire('query', q.query);
}
if (q.error) {
this.fire('error', q.error);
}
if (this._cb) {
this._cb(q);
}
delete YUI.yql[this._stamp];
},
/**
* @private
* @method _query
* @description Builds the query and fire the Get call.
* @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).
* @param {Object} opts An object literal of extra options to pass along to the Get Utility (optional).
* @return Self
*/
_query: function(sql, callback, params, opts) {
var st = Y.stamp({}), qs = '', url;
//Must replace the dashes with underscrores
st = st.replace(/-/g, '_');

this._stamp = st;

this._cb = callback;

YUI.yql[st] = Y.bind(this._receiver, this);

if (!params) {
params = {};
}
params.q = sql;
params.format = 'json';
params.callback = "YUI.yql." + st;
if (!params.env) {
params.env = 'http:/'+'/datatables.org/alltables.env';
}

Y.each(params, function(v, k) {
qs += k + '=' + encodeURIComponent(v) + '&';
});

if (!opts) {
opts = {};
}
opts.autopurge = true;
opts.context = this;
opts.onTimeout = function(o){
this.fire('timeout', o);
if (this._cb) {
this._cb(o);
this._cb = null;
}
};

url = BASE_URL + qs;
Y.Get.script(url, opts);
return this;
}
});
/**
* @event query
* @description Fires when the Query returns.
* @type {Event.Custom}
*/

/**
* @event error
* @description Fires when an error occurs.
* @type {Event.Custom}
*/

/**
* @event timeout
* @description Fires when the request has timed-out.
* @type {Event.Custom}
*/

Y.yql = yql;



}, '@VERSION@' ,{requires:['get', 'event-custom'], supersedes:['get', 'event-custom']});
30 changes: 30 additions & 0 deletions sandbox/davglass/yql/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Software License Agreement (BSD License)

Copyright (c) 2007, Dav Glass <dav.glass@yahoo.com>.
All rights reserved.

Redistribution and use of this software in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:

* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.

* The name of Dav Glass may not be used to endorse or promote products
derived from this software without specific prior
written permission of Dav Glass.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Loading

0 comments on commit 6e6ae6f

Please sign in to comment.