/ajax/libs/yui/3.6.0pr4/yql/yql.js

https://gitlab.com/Mirros/cdnjs · JavaScript · 197 lines · 84 code · 24 blank · 89 comment · 23 complexity · abe112880187be70da2af96a9103094b MD5 · raw file

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