You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

509 lines
17 KiB

  1. // Generated by CoffeeScript 1.3.1
  2. (function() {
  3. var SwaggerApi, SwaggerOperation, SwaggerRequest, SwaggerResource,
  4. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  5. SwaggerApi = (function() {
  6. SwaggerApi.name = 'SwaggerApi';
  7. SwaggerApi.prototype.discoveryUrl = "http://api.wordnik.com/v4/resources.json";
  8. SwaggerApi.prototype.debug = false;
  9. SwaggerApi.prototype.api_key = null;
  10. SwaggerApi.prototype.basePath = null;
  11. function SwaggerApi(options) {
  12. if (options == null) {
  13. options = {};
  14. }
  15. if (options.discoveryUrl != null) {
  16. this.discoveryUrl = options.discoveryUrl;
  17. }
  18. if (options.debug != null) {
  19. this.debug = options.debug;
  20. }
  21. if (options.apiKey != null) {
  22. this.api_key = options.apiKey;
  23. }
  24. if (options.api_key != null) {
  25. this.api_key = options.api_key;
  26. }
  27. if (options.verbose != null) {
  28. this.verbose = options.verbose;
  29. }
  30. this.supportHeaderParams = options.supportHeaderParams != null ? options.supportHeaderParams : false;
  31. if (options.success != null) {
  32. this.success = options.success;
  33. }
  34. this.failure = options.failure != null ? options.failure : function() {};
  35. this.progress = options.progress != null ? options.progress : function() {};
  36. this.discoveryUrl = this.suffixApiKey(this.discoveryUrl);
  37. if (options.success != null) {
  38. this.build();
  39. }
  40. }
  41. SwaggerApi.prototype.build = function() {
  42. var _this = this;
  43. this.progress('fetching resource list: ' + this.discoveryUrl);
  44. return jQuery.getJSON(this.discoveryUrl, function(response) {
  45. var res, resource, _i, _j, _len, _len1, _ref, _ref1;
  46. if ((response.basePath != null) && jQuery.trim(response.basePath).length > 0) {
  47. _this.basePath = response.basePath;
  48. if (_this.basePath.match(/^HTTP/i) == null) {
  49. _this.fail("discoveryUrl basePath must be a URL.");
  50. }
  51. _this.basePath = _this.basePath.replace(/\/$/, '');
  52. } else {
  53. _this.basePath = _this.discoveryUrl.substring(0, _this.discoveryUrl.lastIndexOf('/'));
  54. log('derived basepath from discoveryUrl as ' + _this.basePath);
  55. }
  56. _this.resources = {};
  57. _this.resourcesArray = [];
  58. if (response.resourcePath != null) {
  59. _this.resourcePath = response.resourcePath;
  60. res = null;
  61. _ref = response.apis;
  62. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  63. resource = _ref[_i];
  64. if (res === null) {
  65. res = new SwaggerResource(resource, _this);
  66. } else {
  67. res.addOperations(resource.path, resource.operations);
  68. }
  69. }
  70. if (res != null) {
  71. _this.resources[res.name] = res;
  72. _this.resourcesArray.push(res);
  73. res.ready = true;
  74. _this.selfReflect();
  75. }
  76. } else {
  77. _ref1 = response.apis;
  78. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  79. resource = _ref1[_j];
  80. res = new SwaggerResource(resource, _this);
  81. _this.resources[res.name] = res;
  82. _this.resourcesArray.push(res);
  83. }
  84. }
  85. return _this;
  86. }).error(function(error) {
  87. return _this.fail(error.status + ' : ' + error.statusText + ' ' + _this.discoveryUrl);
  88. });
  89. };
  90. SwaggerApi.prototype.selfReflect = function() {
  91. var resource, resource_name, _ref;
  92. if (this.resources == null) {
  93. return false;
  94. }
  95. _ref = this.resources;
  96. for (resource_name in _ref) {
  97. resource = _ref[resource_name];
  98. if (resource.ready == null) {
  99. return false;
  100. }
  101. }
  102. this.ready = true;
  103. if (this.success != null) {
  104. return this.success();
  105. }
  106. };
  107. SwaggerApi.prototype.fail = function(message) {
  108. this.failure(message);
  109. throw message;
  110. };
  111. SwaggerApi.prototype.suffixApiKey = function(url) {
  112. var sep;
  113. if ((this.api_key != null) && jQuery.trim(this.api_key).length > 0 && (url != null)) {
  114. sep = url.indexOf('?') > 0 ? '&' : '?';
  115. return url + sep + 'api_key=' + this.api_key;
  116. } else {
  117. return url;
  118. }
  119. };
  120. SwaggerApi.prototype.help = function() {
  121. var operation, operation_name, parameter, resource, resource_name, _i, _len, _ref, _ref1, _ref2;
  122. _ref = this.resources;
  123. for (resource_name in _ref) {
  124. resource = _ref[resource_name];
  125. console.log(resource_name);
  126. _ref1 = resource.operations;
  127. for (operation_name in _ref1) {
  128. operation = _ref1[operation_name];
  129. console.log(" " + operation.nickname);
  130. _ref2 = operation.parameters;
  131. for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
  132. parameter = _ref2[_i];
  133. console.log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description);
  134. }
  135. }
  136. }
  137. return this;
  138. };
  139. return SwaggerApi;
  140. })();
  141. SwaggerResource = (function() {
  142. SwaggerResource.name = 'SwaggerResource';
  143. function SwaggerResource(resourceObj, api) {
  144. var parts,
  145. _this = this;
  146. this.api = api;
  147. this.path = this.api.resourcePath != null ? this.api.resourcePath : resourceObj.path;
  148. this.description = resourceObj.description;
  149. parts = this.path.split("/");
  150. this.name = parts[parts.length - 1].replace('.{format}', '');
  151. this.basePath = this.api.basePath;
  152. this.operations = {};
  153. this.operationsArray = [];
  154. if ((resourceObj.operations != null) && (this.api.resourcePath != null)) {
  155. this.api.progress('reading resource ' + this.name + ' operations');
  156. this.addOperations(resourceObj.path, resourceObj.operations);
  157. this.api[this.name] = this;
  158. } else {
  159. if (this.path == null) {
  160. this.api.fail("SwaggerResources must have a path.");
  161. }
  162. this.url = this.api.suffixApiKey(this.api.basePath + this.path.replace('{format}', 'json'));
  163. this.api.progress('fetching resource ' + this.name + ': ' + this.url);
  164. jQuery.getJSON(this.url, function(response) {
  165. var endpoint, _i, _len, _ref;
  166. if ((response.basePath != null) && jQuery.trim(response.basePath).length > 0) {
  167. _this.basePath = response.basePath;
  168. _this.basePath = _this.basePath.replace(/\/$/, '');
  169. }
  170. if (response.apis) {
  171. _ref = response.apis;
  172. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  173. endpoint = _ref[_i];
  174. _this.addOperations(endpoint.path, endpoint.operations);
  175. }
  176. }
  177. _this.api[_this.name] = _this;
  178. _this.ready = true;
  179. return _this.api.selfReflect();
  180. }).error(function(error) {
  181. return _this.fail(error.status + ' : ' + error.statusText + ' ' + _this.url);
  182. });
  183. }
  184. }
  185. SwaggerResource.prototype.addOperations = function(resource_path, ops) {
  186. var o, op, _i, _len, _results;
  187. if (ops) {
  188. _results = [];
  189. for (_i = 0, _len = ops.length; _i < _len; _i++) {
  190. o = ops[_i];
  191. op = new SwaggerOperation(o.nickname, resource_path, o.httpMethod, o.parameters, o.summary, this);
  192. this.operations[op.nickname] = op;
  193. _results.push(this.operationsArray.push(op));
  194. }
  195. return _results;
  196. }
  197. };
  198. SwaggerResource.prototype.help = function() {
  199. var operation, operation_name, parameter, _i, _len, _ref, _ref1;
  200. _ref = this.operations;
  201. for (operation_name in _ref) {
  202. operation = _ref[operation_name];
  203. console.log(" " + operation.nickname);
  204. _ref1 = operation.parameters;
  205. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  206. parameter = _ref1[_i];
  207. console.log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description);
  208. }
  209. }
  210. return this;
  211. };
  212. return SwaggerResource;
  213. })();
  214. SwaggerOperation = (function() {
  215. SwaggerOperation.name = 'SwaggerOperation';
  216. function SwaggerOperation(nickname, path, httpMethod, parameters, summary, resource) {
  217. var parameter, v, _i, _j, _len, _len1, _ref, _ref1,
  218. _this = this;
  219. this.nickname = nickname;
  220. this.path = path;
  221. this.httpMethod = httpMethod;
  222. this.parameters = parameters != null ? parameters : [];
  223. this.summary = summary;
  224. this.resource = resource;
  225. this["do"] = __bind(this["do"], this);
  226. if (this.nickname == null) {
  227. this.resource.api.fail("SwaggerOperations must have a nickname.");
  228. }
  229. if (this.path == null) {
  230. this.resource.api.fail("SwaggerOperation " + nickname + " is missing path.");
  231. }
  232. if (this.httpMethod == null) {
  233. this.resource.api.fail("SwaggerOperation " + nickname + " is missing httpMethod.");
  234. }
  235. this.path = this.path.replace('{format}', 'json');
  236. this.httpMethod = this.httpMethod.toLowerCase();
  237. this.isGetMethod = this.httpMethod === "get";
  238. this.resourceName = this.resource.name;
  239. _ref = this.parameters;
  240. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  241. parameter = _ref[_i];
  242. parameter.name = parameter.name || parameter.dataType;
  243. if (parameter.allowableValues != null) {
  244. if (parameter.allowableValues.valueType === "RANGE") {
  245. parameter.isRange = true;
  246. } else {
  247. parameter.isList = true;
  248. }
  249. if (parameter.allowableValues.values != null) {
  250. parameter.allowableValues.descriptiveValues = [];
  251. _ref1 = parameter.allowableValues.values;
  252. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  253. v = _ref1[_j];
  254. if ((parameter.defaultValue != null) && parameter.defaultValue === v) {
  255. parameter.allowableValues.descriptiveValues.push({
  256. value: v,
  257. isDefault: true
  258. });
  259. } else {
  260. parameter.allowableValues.descriptiveValues.push({
  261. value: v,
  262. isDefault: false
  263. });
  264. }
  265. }
  266. }
  267. }
  268. }
  269. this.resource[this.nickname] = function(args, callback, error) {
  270. return _this["do"](args, callback, error);
  271. };
  272. }
  273. SwaggerOperation.prototype["do"] = function(args, callback, error) {
  274. var body, headers;
  275. if (args == null) {
  276. args = {};
  277. }
  278. if ((typeof args) === "function") {
  279. error = callback;
  280. callback = args;
  281. args = {};
  282. }
  283. if (error == null) {
  284. error = function(xhr, textStatus, error) {
  285. return console.log(xhr, textStatus, error);
  286. };
  287. }
  288. if (callback == null) {
  289. callback = function(data) {
  290. return console.log(data);
  291. };
  292. }
  293. if (args.headers != null) {
  294. headers = args.headers;
  295. delete args.headers;
  296. }
  297. if (args.body != null) {
  298. body = args.body;
  299. delete args.body;
  300. }
  301. return new SwaggerRequest(this.httpMethod, this.urlify(args), headers, body, callback, error, this);
  302. };
  303. SwaggerOperation.prototype.pathJson = function() {
  304. return this.path.replace("{format}", "json");
  305. };
  306. SwaggerOperation.prototype.pathXml = function() {
  307. return this.path.replace("{format}", "xml");
  308. };
  309. SwaggerOperation.prototype.urlify = function(args, includeApiKey) {
  310. var param, queryParams, url, _i, _len, _ref;
  311. if (includeApiKey == null) {
  312. includeApiKey = true;
  313. }
  314. url = this.resource.basePath + this.pathJson();
  315. _ref = this.parameters;
  316. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  317. param = _ref[_i];
  318. if (param.paramType === 'path') {
  319. if (args[param.name]) {
  320. url = url.replace("{" + param.name + "}", encodeURIComponent(args[param.name]));
  321. delete args[param.name];
  322. } else {
  323. throw "" + param.name + " is a required path param.";
  324. }
  325. }
  326. }
  327. if (includeApiKey && (this.resource.api.api_key != null) && this.resource.api.api_key.length > 0) {
  328. args['api_key'] = this.resource.api.api_key;
  329. }
  330. if (this.supportHeaderParams()) {
  331. queryParams = jQuery.param(this.getQueryParams(args));
  332. } else {
  333. queryParams = jQuery.param(this.getQueryAndHeaderParams(args));
  334. }
  335. if ((queryParams != null) && queryParams.length > 0) {
  336. url += "?" + queryParams;
  337. }
  338. return url;
  339. };
  340. SwaggerOperation.prototype.supportHeaderParams = function() {
  341. return this.resource.api.supportHeaderParams;
  342. };
  343. SwaggerOperation.prototype.getQueryAndHeaderParams = function(args, includeApiKey) {
  344. if (includeApiKey == null) {
  345. includeApiKey = true;
  346. }
  347. return this.getMatchingParams(['query', 'header'], args, includeApiKey);
  348. };
  349. SwaggerOperation.prototype.getQueryParams = function(args, includeApiKey) {
  350. if (includeApiKey == null) {
  351. includeApiKey = true;
  352. }
  353. return this.getMatchingParams(['query'], args, includeApiKey);
  354. };
  355. SwaggerOperation.prototype.getHeaderParams = function(args, includeApiKey) {
  356. if (includeApiKey == null) {
  357. includeApiKey = true;
  358. }
  359. return this.getMatchingParams(['header'], args, includeApiKey);
  360. };
  361. SwaggerOperation.prototype.getMatchingParams = function(paramTypes, args, includeApiKey) {
  362. var matchingParams, param, _i, _len, _ref;
  363. matchingParams = {};
  364. _ref = this.parameters;
  365. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  366. param = _ref[_i];
  367. if ((jQuery.inArray(param.paramType, paramTypes) >= 0) && args[param.name]) {
  368. matchingParams[param.name] = args[param.name];
  369. }
  370. }
  371. if (includeApiKey && (this.resource.api.api_key != null) && this.resource.api.api_key.length > 0) {
  372. matchingParams['api_key'] = this.resource.api.api_key;
  373. }
  374. return matchingParams;
  375. };
  376. SwaggerOperation.prototype.help = function() {
  377. var parameter, _i, _len, _ref;
  378. _ref = this.parameters;
  379. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  380. parameter = _ref[_i];
  381. console.log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description);
  382. }
  383. return this;
  384. };
  385. return SwaggerOperation;
  386. })();
  387. SwaggerRequest = (function() {
  388. SwaggerRequest.name = 'SwaggerRequest';
  389. function SwaggerRequest(type, url, headers, body, successCallback, errorCallback, operation) {
  390. var obj,
  391. _this = this;
  392. this.type = type;
  393. this.url = url;
  394. this.headers = headers;
  395. this.body = body;
  396. this.successCallback = successCallback;
  397. this.errorCallback = errorCallback;
  398. this.operation = operation;
  399. if (this.type == null) {
  400. throw "SwaggerRequest type is required (get/post/put/delete).";
  401. }
  402. if (this.url == null) {
  403. throw "SwaggerRequest url is required.";
  404. }
  405. if (this.successCallback == null) {
  406. throw "SwaggerRequest successCallback is required.";
  407. }
  408. if (this.errorCallback == null) {
  409. throw "SwaggerRequest error callback is required.";
  410. }
  411. if (this.operation == null) {
  412. throw "SwaggerRequest operation is required.";
  413. }
  414. if (this.operation.resource.api.verbose) {
  415. console.log(this.asCurl());
  416. }
  417. this.headers || (this.headers = {});
  418. if (this.operation.resource.api.api_key != null) {
  419. this.headers.api_key = this.operation.resource.api.api_key;
  420. }
  421. if (this.headers.mock == null) {
  422. obj = {
  423. type: this.type,
  424. url: this.url,
  425. data: JSON.stringify(this.body),
  426. dataType: 'json',
  427. error: function(xhr, textStatus, error) {
  428. return _this.errorCallback(xhr, textStatus, error);
  429. },
  430. success: function(data) {
  431. return _this.successCallback(data);
  432. }
  433. };
  434. if (obj.type.toLowerCase() === "post" || obj.type.toLowerCase() === "put") {
  435. obj.contentType = "application/json";
  436. }
  437. jQuery.ajax(obj);
  438. }
  439. }
  440. SwaggerRequest.prototype.asCurl = function() {
  441. var header_args, k, v;
  442. header_args = (function() {
  443. var _ref, _results;
  444. _ref = this.headers;
  445. _results = [];
  446. for (k in _ref) {
  447. v = _ref[k];
  448. _results.push("--header \"" + k + ": " + v + "\"");
  449. }
  450. return _results;
  451. }).call(this);
  452. return "curl " + (header_args.join(" ")) + " " + this.url;
  453. };
  454. return SwaggerRequest;
  455. })();
  456. window.SwaggerApi = SwaggerApi;
  457. window.SwaggerResource = SwaggerResource;
  458. window.SwaggerOperation = SwaggerOperation;
  459. window.SwaggerRequest = SwaggerRequest;
  460. }).call(this);