No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

766 líneas
27 KiB

  1. // Generated by CoffeeScript 1.4.0
  2. (function() {
  3. var SwaggerApi, SwaggerModel, SwaggerModelProperty, SwaggerOperation, SwaggerRequest, SwaggerResource,
  4. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  5. SwaggerApi = (function() {
  6. SwaggerApi.prototype.discoveryUrl = "http://api.wordnik.com/v4/resources.json";
  7. SwaggerApi.prototype.debug = false;
  8. SwaggerApi.prototype.api_key = null;
  9. SwaggerApi.prototype.basePath = null;
  10. function SwaggerApi(options) {
  11. if (options == null) {
  12. options = {};
  13. }
  14. if (options.discoveryUrl != null) {
  15. this.discoveryUrl = options.discoveryUrl;
  16. }
  17. if (options.debug != null) {
  18. this.debug = options.debug;
  19. }
  20. this.apiKeyName = options.apiKeyName != null ? options.apiKeyName : 'api_key';
  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. this.supportedSubmitMethods = options.supportedSubmitMethods != null ? options.supportedSubmitMethods : ['get'];
  32. if (options.success != null) {
  33. this.success = options.success;
  34. }
  35. this.failure = options.failure != null ? options.failure : function() {};
  36. this.progress = options.progress != null ? options.progress : function() {};
  37. this.headers = options.headers != null ? options.headers : {};
  38. this.booleanValues = options.booleanValues != null ? options.booleanValues : new Array('true', 'false');
  39. this.discoveryUrl = this.suffixApiKey(this.discoveryUrl);
  40. if (options.success != null) {
  41. this.build();
  42. }
  43. }
  44. SwaggerApi.prototype.build = function() {
  45. var _this = this;
  46. this.progress('fetching resource list: ' + this.discoveryUrl);
  47. return jQuery.getJSON(this.discoveryUrl, function(response) {
  48. var res, resource, _i, _j, _len, _len1, _ref, _ref1;
  49. if (response.apiVersion != null) {
  50. _this.apiVersion = response.apiVersion;
  51. }
  52. if ((response.basePath != null) && jQuery.trim(response.basePath).length > 0) {
  53. _this.basePath = response.basePath;
  54. if (_this.basePath.match(/^HTTP/i) == null) {
  55. _this.fail("discoveryUrl basePath must be a URL.");
  56. }
  57. _this.basePath = _this.basePath.replace(/\/$/, '');
  58. } else {
  59. _this.basePath = _this.discoveryUrl.substring(0, _this.discoveryUrl.lastIndexOf('/'));
  60. log('derived basepath from discoveryUrl as ' + _this.basePath);
  61. }
  62. _this.apis = {};
  63. _this.apisArray = [];
  64. if (response.resourcePath != null) {
  65. _this.resourcePath = response.resourcePath;
  66. res = null;
  67. _ref = response.apis;
  68. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  69. resource = _ref[_i];
  70. if (res === null) {
  71. res = new SwaggerResource(resource, _this);
  72. } else {
  73. res.addOperations(resource.path, resource.operations);
  74. }
  75. }
  76. if (res != null) {
  77. _this.apis[res.name] = res;
  78. _this.apisArray.push(res);
  79. res.ready = true;
  80. _this.selfReflect();
  81. }
  82. } else {
  83. _ref1 = response.apis;
  84. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  85. resource = _ref1[_j];
  86. res = new SwaggerResource(resource, _this);
  87. _this.apis[res.name] = res;
  88. _this.apisArray.push(res);
  89. }
  90. }
  91. return _this;
  92. }).error(function(error) {
  93. if (_this.discoveryUrl.substring(0, 4) !== 'http') {
  94. return _this.fail('Please specify the protocol for ' + _this.discoveryUrl);
  95. } else if (error.status === 0) {
  96. return _this.fail('Can\'t read from server. It may not have the appropriate access-control-origin settings.');
  97. } else if (error.status === 404) {
  98. return _this.fail('Can\'t read swagger JSON from ' + _this.discoveryUrl);
  99. } else {
  100. return _this.fail(error.status + ' : ' + error.statusText + ' ' + _this.discoveryUrl);
  101. }
  102. });
  103. };
  104. SwaggerApi.prototype.selfReflect = function() {
  105. var resource, resource_name, _ref;
  106. if (this.apis == null) {
  107. return false;
  108. }
  109. _ref = this.apis;
  110. for (resource_name in _ref) {
  111. resource = _ref[resource_name];
  112. if (resource.ready == null) {
  113. return false;
  114. }
  115. }
  116. this.setConsolidatedModels();
  117. this.ready = true;
  118. if (this.success != null) {
  119. return this.success();
  120. }
  121. };
  122. SwaggerApi.prototype.fail = function(message) {
  123. this.failure(message);
  124. throw message;
  125. };
  126. SwaggerApi.prototype.setConsolidatedModels = function() {
  127. var model, modelName, resource, resource_name, _i, _len, _ref, _ref1, _results;
  128. this.modelsArray = [];
  129. this.models = {};
  130. _ref = this.apis;
  131. for (resource_name in _ref) {
  132. resource = _ref[resource_name];
  133. for (modelName in resource.models) {
  134. if (!(this.models[modelName] != null)) {
  135. this.models[modelName] = resource.models[modelName];
  136. this.modelsArray.push(resource.models[modelName]);
  137. }
  138. }
  139. }
  140. _ref1 = this.modelsArray;
  141. _results = [];
  142. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  143. model = _ref1[_i];
  144. _results.push(model.setReferencedModels(this.models));
  145. }
  146. return _results;
  147. };
  148. SwaggerApi.prototype.suffixApiKey = function(url) {
  149. var sep;
  150. if ((this.api_key != null) && jQuery.trim(this.api_key).length > 0 && (url != null)) {
  151. sep = url.indexOf('?') > 0 ? '&' : '?';
  152. return url + sep + this.apiKeyName + '=' + this.api_key;
  153. } else {
  154. return url;
  155. }
  156. };
  157. SwaggerApi.prototype.help = function() {
  158. var operation, operation_name, parameter, resource, resource_name, _i, _len, _ref, _ref1, _ref2;
  159. _ref = this.apis;
  160. for (resource_name in _ref) {
  161. resource = _ref[resource_name];
  162. console.log(resource_name);
  163. _ref1 = resource.operations;
  164. for (operation_name in _ref1) {
  165. operation = _ref1[operation_name];
  166. console.log(" " + operation.nickname);
  167. _ref2 = operation.parameters;
  168. for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
  169. parameter = _ref2[_i];
  170. console.log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description);
  171. }
  172. }
  173. }
  174. return this;
  175. };
  176. return SwaggerApi;
  177. })();
  178. SwaggerResource = (function() {
  179. function SwaggerResource(resourceObj, api) {
  180. var parts,
  181. _this = this;
  182. this.api = api;
  183. this.path = this.api.resourcePath != null ? this.api.resourcePath : resourceObj.path;
  184. this.description = resourceObj.description;
  185. parts = this.path.split("/");
  186. this.name = parts[parts.length - 1].replace('.{format}', '');
  187. this.basePath = this.api.basePath;
  188. this.operations = {};
  189. this.operationsArray = [];
  190. this.modelsArray = [];
  191. this.models = {};
  192. if ((resourceObj.operations != null) && (this.api.resourcePath != null)) {
  193. this.api.progress('reading resource ' + this.name + ' models and operations');
  194. this.addModels(resourceObj.models);
  195. this.addOperations(resourceObj.path, resourceObj.operations);
  196. this.api[this.name] = this;
  197. } else {
  198. if (this.path == null) {
  199. this.api.fail("SwaggerResources must have a path.");
  200. }
  201. this.url = this.api.suffixApiKey(this.api.basePath + this.path.replace('{format}', 'json'));
  202. this.api.progress('fetching resource ' + this.name + ': ' + this.url);
  203. jQuery.getJSON(this.url, function(response) {
  204. var endpoint, _i, _len, _ref;
  205. if ((response.basePath != null) && jQuery.trim(response.basePath).length > 0) {
  206. _this.basePath = response.basePath;
  207. _this.basePath = _this.basePath.replace(/\/$/, '');
  208. }
  209. _this.addModels(response.models);
  210. if (response.apis) {
  211. _ref = response.apis;
  212. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  213. endpoint = _ref[_i];
  214. _this.addOperations(endpoint.path, endpoint.operations);
  215. }
  216. }
  217. _this.api[_this.name] = _this;
  218. _this.ready = true;
  219. return _this.api.selfReflect();
  220. }).error(function(error) {
  221. return _this.api.fail("Unable to read api '" + _this.name + "' from path " + _this.url + " (server returned " + error.statusText + ")");
  222. });
  223. }
  224. }
  225. SwaggerResource.prototype.addModels = function(models) {
  226. var model, modelName, swaggerModel, _i, _len, _ref, _results;
  227. if (models != null) {
  228. for (modelName in models) {
  229. if (!(this.models[modelName] != null)) {
  230. swaggerModel = new SwaggerModel(modelName, models[modelName]);
  231. this.modelsArray.push(swaggerModel);
  232. this.models[modelName] = swaggerModel;
  233. }
  234. }
  235. _ref = this.modelsArray;
  236. _results = [];
  237. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  238. model = _ref[_i];
  239. _results.push(model.setReferencedModels(this.models));
  240. }
  241. return _results;
  242. }
  243. };
  244. SwaggerResource.prototype.addOperations = function(resource_path, ops) {
  245. var consumes, o, op, _i, _len, _results;
  246. if (ops) {
  247. _results = [];
  248. for (_i = 0, _len = ops.length; _i < _len; _i++) {
  249. o = ops[_i];
  250. consumes = o.consumes;
  251. if (o.supportedContentTypes) {
  252. consumes = o.supportedContentTypes;
  253. }
  254. op = new SwaggerOperation(o.nickname, resource_path, o.httpMethod, o.parameters, o.summary, o.notes, o.responseClass, o.errorResponses, this, o.consumes, o.produces);
  255. this.operations[op.nickname] = op;
  256. _results.push(this.operationsArray.push(op));
  257. }
  258. return _results;
  259. }
  260. };
  261. SwaggerResource.prototype.help = function() {
  262. var operation, operation_name, parameter, _i, _len, _ref, _ref1;
  263. _ref = this.operations;
  264. for (operation_name in _ref) {
  265. operation = _ref[operation_name];
  266. console.log(" " + operation.nickname);
  267. _ref1 = operation.parameters;
  268. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  269. parameter = _ref1[_i];
  270. console.log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description);
  271. }
  272. }
  273. return this;
  274. };
  275. return SwaggerResource;
  276. })();
  277. SwaggerModel = (function() {
  278. function SwaggerModel(modelName, obj) {
  279. var propertyName;
  280. this.name = obj.id != null ? obj.id : modelName;
  281. this.properties = [];
  282. for (propertyName in obj.properties) {
  283. this.properties.push(new SwaggerModelProperty(propertyName, obj.properties[propertyName]));
  284. }
  285. }
  286. SwaggerModel.prototype.setReferencedModels = function(allModels) {
  287. var prop, _i, _len, _ref, _results;
  288. _ref = this.properties;
  289. _results = [];
  290. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  291. prop = _ref[_i];
  292. if (allModels[prop.dataType] != null) {
  293. _results.push(prop.refModel = allModels[prop.dataType]);
  294. } else if ((prop.refDataType != null) && (allModels[prop.refDataType] != null)) {
  295. _results.push(prop.refModel = allModels[prop.refDataType]);
  296. } else {
  297. _results.push(void 0);
  298. }
  299. }
  300. return _results;
  301. };
  302. SwaggerModel.prototype.getMockSignature = function(prefix, modelsToIgnore) {
  303. var classClose, classOpen, prop, propertiesStr, returnVal, strong, strongClose, stronger, _i, _j, _len, _len1, _ref, _ref1;
  304. propertiesStr = [];
  305. _ref = this.properties;
  306. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  307. prop = _ref[_i];
  308. propertiesStr.push(prop.toString());
  309. }
  310. strong = '<span style="font-weight: bold; color: #000; font-size: 1.0em">';
  311. stronger = '<span style="font-weight: bold; color: #000; font-size: 1.1em">';
  312. strongClose = '</span>';
  313. classOpen = strong + 'class ' + this.name + '(' + strongClose;
  314. classClose = strong + ')' + strongClose;
  315. returnVal = classOpen + '<span>' + propertiesStr.join('</span>, <span>') + '</span>' + classClose;
  316. if (prefix != null) {
  317. returnVal = stronger + prefix + strongClose + '<br/>' + returnVal;
  318. }
  319. if (!modelsToIgnore) {
  320. modelsToIgnore = [];
  321. }
  322. modelsToIgnore.push(this);
  323. _ref1 = this.properties;
  324. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  325. prop = _ref1[_j];
  326. if ((prop.refModel != null) && (modelsToIgnore.indexOf(prop.refModel)) === -1) {
  327. returnVal = returnVal + ('<br>' + prop.refModel.getMockSignature(void 0, modelsToIgnore));
  328. }
  329. }
  330. return returnVal;
  331. };
  332. SwaggerModel.prototype.createJSONSample = function(modelToIgnore) {
  333. var prop, result, _i, _len, _ref;
  334. result = {};
  335. _ref = this.properties;
  336. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  337. prop = _ref[_i];
  338. result[prop.name] = prop.getSampleValue(modelToIgnore);
  339. }
  340. return result;
  341. };
  342. return SwaggerModel;
  343. })();
  344. SwaggerModelProperty = (function() {
  345. function SwaggerModelProperty(name, obj) {
  346. this.name = name;
  347. this.dataType = obj.type;
  348. this.isArray = this.dataType.toLowerCase() === 'array';
  349. this.descr = obj.description;
  350. if (obj.items != null) {
  351. if (obj.items.type != null) {
  352. this.refDataType = obj.items.type;
  353. }
  354. if (obj.items.$ref != null) {
  355. this.refDataType = obj.items.$ref;
  356. }
  357. }
  358. this.dataTypeWithRef = this.refDataType != null ? this.dataType + '[' + this.refDataType + ']' : this.dataType;
  359. if (obj.allowableValues != null) {
  360. this.valueType = obj.allowableValues.valueType;
  361. this.values = obj.allowableValues.values;
  362. if (this.values != null) {
  363. this.valuesString = "'" + this.values.join("' or '") + "'";
  364. }
  365. }
  366. }
  367. SwaggerModelProperty.prototype.getSampleValue = function(modelToIgnore) {
  368. var result;
  369. if ((this.refModel != null) && (!(this.refModel === modelToIgnore))) {
  370. result = this.refModel.createJSONSample(this.refModel);
  371. } else {
  372. if (this.isArray) {
  373. result = this.refDataType;
  374. } else {
  375. result = this.dataType;
  376. }
  377. }
  378. if (this.isArray) {
  379. return [result];
  380. } else {
  381. return result;
  382. }
  383. };
  384. SwaggerModelProperty.prototype.toString = function() {
  385. var str;
  386. str = this.name + ': ' + this.dataTypeWithRef;
  387. if (this.values != null) {
  388. str += " = ['" + this.values.join("' or '") + "']";
  389. }
  390. if (this.descr != null) {
  391. str += ' {' + this.descr + '}';
  392. }
  393. return str;
  394. };
  395. return SwaggerModelProperty;
  396. })();
  397. SwaggerOperation = (function() {
  398. function SwaggerOperation(nickname, path, httpMethod, parameters, summary, notes, responseClass, errorResponses, resource, consumes, produces) {
  399. var parameter, v, _i, _j, _len, _len1, _ref, _ref1, _ref2,
  400. _this = this;
  401. this.nickname = nickname;
  402. this.path = path;
  403. this.httpMethod = httpMethod;
  404. this.parameters = parameters != null ? parameters : [];
  405. this.summary = summary;
  406. this.notes = notes;
  407. this.responseClass = responseClass;
  408. this.errorResponses = errorResponses;
  409. this.resource = resource;
  410. this.consumes = consumes;
  411. this.produces = produces;
  412. this["do"] = __bind(this["do"], this);
  413. if (this.nickname == null) {
  414. this.resource.api.fail("SwaggerOperations must have a nickname.");
  415. }
  416. if (this.path == null) {
  417. this.resource.api.fail("SwaggerOperation " + nickname + " is missing path.");
  418. }
  419. if (this.httpMethod == null) {
  420. this.resource.api.fail("SwaggerOperation " + nickname + " is missing httpMethod.");
  421. }
  422. this.path = this.path.replace('{format}', 'json');
  423. this.httpMethod = this.httpMethod.toLowerCase();
  424. this.isGetMethod = this.httpMethod === "get";
  425. this.resourceName = this.resource.name;
  426. if (((_ref = this.responseClass) != null ? _ref.toLowerCase() : void 0) === 'void') {
  427. this.responseClass = void 0;
  428. }
  429. if (this.responseClass != null) {
  430. this.responseClassSignature = this.getSignature(this.responseClass, this.resource.models);
  431. this.responseSampleJSON = this.getSampleJSON(this.responseClass, this.resource.models);
  432. }
  433. this.errorResponses = this.errorResponses || [];
  434. _ref1 = this.parameters;
  435. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  436. parameter = _ref1[_i];
  437. parameter.name = parameter.name || parameter.dataType;
  438. if (parameter.dataType.toLowerCase() === 'boolean') {
  439. parameter.allowableValues = {};
  440. parameter.allowableValues.values = this.resource.api.booleanValues;
  441. }
  442. parameter.signature = this.getSignature(parameter.dataType, this.resource.models);
  443. parameter.sampleJSON = this.getSampleJSON(parameter.dataType, this.resource.models);
  444. if (parameter.allowableValues != null) {
  445. if (parameter.allowableValues.valueType === "RANGE") {
  446. parameter.isRange = true;
  447. } else {
  448. parameter.isList = true;
  449. }
  450. if (parameter.allowableValues.values != null) {
  451. parameter.allowableValues.descriptiveValues = [];
  452. _ref2 = parameter.allowableValues.values;
  453. for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
  454. v = _ref2[_j];
  455. if ((parameter.defaultValue != null) && parameter.defaultValue === v) {
  456. parameter.allowableValues.descriptiveValues.push({
  457. value: v,
  458. isDefault: true
  459. });
  460. } else {
  461. parameter.allowableValues.descriptiveValues.push({
  462. value: v,
  463. isDefault: false
  464. });
  465. }
  466. }
  467. }
  468. }
  469. }
  470. this.resource[this.nickname] = function(args, callback, error) {
  471. return _this["do"](args, callback, error);
  472. };
  473. }
  474. SwaggerOperation.prototype.isListType = function(dataType) {
  475. if (dataType.indexOf('[') >= 0) {
  476. return dataType.substring(dataType.indexOf('[') + 1, dataType.indexOf(']'));
  477. } else {
  478. return void 0;
  479. }
  480. };
  481. SwaggerOperation.prototype.getSignature = function(dataType, models) {
  482. var isPrimitive, listType;
  483. listType = this.isListType(dataType);
  484. isPrimitive = ((listType != null) && models[listType]) || (models[dataType] != null) ? false : true;
  485. if (isPrimitive) {
  486. return dataType;
  487. } else {
  488. if (listType != null) {
  489. return models[listType].getMockSignature(dataType);
  490. } else {
  491. return models[dataType].getMockSignature(dataType);
  492. }
  493. }
  494. };
  495. SwaggerOperation.prototype.getSampleJSON = function(dataType, models) {
  496. var isPrimitive, listType, val;
  497. listType = this.isListType(dataType);
  498. isPrimitive = ((listType != null) && models[listType]) || (models[dataType] != null) ? false : true;
  499. val = isPrimitive ? void 0 : (listType != null ? models[listType].createJSONSample() : models[dataType].createJSONSample());
  500. if (val) {
  501. val = listType ? [val] : val;
  502. return JSON.stringify(val, null, 2);
  503. }
  504. };
  505. SwaggerOperation.prototype["do"] = function(args, callback, error) {
  506. var body, headers;
  507. if (args == null) {
  508. args = {};
  509. }
  510. if ((typeof args) === "function") {
  511. error = callback;
  512. callback = args;
  513. args = {};
  514. }
  515. if (error == null) {
  516. error = function(xhr, textStatus, error) {
  517. return console.log(xhr, textStatus, error);
  518. };
  519. }
  520. if (callback == null) {
  521. callback = function(data) {
  522. return console.log(data);
  523. };
  524. }
  525. if (args.headers != null) {
  526. headers = args.headers;
  527. delete args.headers;
  528. }
  529. if (args.body != null) {
  530. body = args.body;
  531. delete args.body;
  532. }
  533. return new SwaggerRequest(this.httpMethod, this.urlify(args), headers, body, callback, error, this);
  534. };
  535. SwaggerOperation.prototype.pathJson = function() {
  536. return this.path.replace("{format}", "json");
  537. };
  538. SwaggerOperation.prototype.pathXml = function() {
  539. return this.path.replace("{format}", "xml");
  540. };
  541. SwaggerOperation.prototype.urlify = function(args, includeApiKey) {
  542. var param, queryParams, reg, url, _i, _len, _ref;
  543. if (includeApiKey == null) {
  544. includeApiKey = true;
  545. }
  546. url = this.resource.basePath + this.pathJson();
  547. _ref = this.parameters;
  548. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  549. param = _ref[_i];
  550. if (param.paramType === 'path') {
  551. if (args[param.name]) {
  552. reg = new RegExp('\{' + param.name + '[^\}]*\}', 'gi');
  553. url = url.replace(reg, encodeURIComponent(args[param.name]));
  554. delete args[param.name];
  555. } else {
  556. throw "" + param.name + " is a required path param.";
  557. }
  558. }
  559. }
  560. if (includeApiKey && (this.resource.api.api_key != null) && this.resource.api.api_key.length > 0) {
  561. args[this.apiKeyName] = this.resource.api.api_key;
  562. }
  563. if (this.supportHeaderParams()) {
  564. queryParams = jQuery.param(this.getQueryParams(args, includeApiKey));
  565. } else {
  566. queryParams = jQuery.param(this.getQueryAndHeaderParams(args, includeApiKey));
  567. }
  568. if ((queryParams != null) && queryParams.length > 0) {
  569. url += "?" + queryParams;
  570. }
  571. return url;
  572. };
  573. SwaggerOperation.prototype.supportHeaderParams = function() {
  574. return this.resource.api.supportHeaderParams;
  575. };
  576. SwaggerOperation.prototype.supportedSubmitMethods = function() {
  577. return this.resource.api.supportedSubmitMethods;
  578. };
  579. SwaggerOperation.prototype.getQueryAndHeaderParams = function(args, includeApiKey) {
  580. if (includeApiKey == null) {
  581. includeApiKey = true;
  582. }
  583. return this.getMatchingParams(['query', 'header'], args, includeApiKey);
  584. };
  585. SwaggerOperation.prototype.getQueryParams = function(args, includeApiKey) {
  586. if (includeApiKey == null) {
  587. includeApiKey = true;
  588. }
  589. return this.getMatchingParams(['query'], args, includeApiKey);
  590. };
  591. SwaggerOperation.prototype.getHeaderParams = function(args, includeApiKey) {
  592. if (includeApiKey == null) {
  593. includeApiKey = true;
  594. }
  595. return this.getMatchingParams(['header'], args, includeApiKey);
  596. };
  597. SwaggerOperation.prototype.getMatchingParams = function(paramTypes, args, includeApiKey) {
  598. var matchingParams, name, param, value, _i, _len, _ref, _ref1;
  599. matchingParams = {};
  600. _ref = this.parameters;
  601. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  602. param = _ref[_i];
  603. if ((jQuery.inArray(param.paramType, paramTypes) >= 0) && args[param.name]) {
  604. matchingParams[param.name] = args[param.name];
  605. }
  606. }
  607. if (includeApiKey && (this.resource.api.api_key != null) && this.resource.api.api_key.length > 0) {
  608. matchingParams[this.resource.api.apiKeyName] = this.resource.api.api_key;
  609. }
  610. if (jQuery.inArray('header', paramTypes) >= 0) {
  611. _ref1 = this.resource.api.headers;
  612. for (name in _ref1) {
  613. value = _ref1[name];
  614. matchingParams[name] = value;
  615. }
  616. }
  617. return matchingParams;
  618. };
  619. SwaggerOperation.prototype.help = function() {
  620. var parameter, _i, _len, _ref;
  621. _ref = this.parameters;
  622. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  623. parameter = _ref[_i];
  624. console.log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description);
  625. }
  626. return this;
  627. };
  628. return SwaggerOperation;
  629. })();
  630. SwaggerRequest = (function() {
  631. function SwaggerRequest(type, url, headers, body, successCallback, errorCallback, operation) {
  632. var obj,
  633. _this = this;
  634. this.type = type;
  635. this.url = url;
  636. this.headers = headers;
  637. this.body = body;
  638. this.successCallback = successCallback;
  639. this.errorCallback = errorCallback;
  640. this.operation = operation;
  641. if (this.type == null) {
  642. throw "SwaggerRequest type is required (get/post/put/delete).";
  643. }
  644. if (this.url == null) {
  645. throw "SwaggerRequest url is required.";
  646. }
  647. if (this.successCallback == null) {
  648. throw "SwaggerRequest successCallback is required.";
  649. }
  650. if (this.errorCallback == null) {
  651. throw "SwaggerRequest error callback is required.";
  652. }
  653. if (this.operation == null) {
  654. throw "SwaggerRequest operation is required.";
  655. }
  656. if (this.operation.resource.api.verbose) {
  657. console.log(this.asCurl());
  658. }
  659. this.headers || (this.headers = {});
  660. if (this.operation.resource.api.api_key != null) {
  661. this.headers[this.apiKeyName] = this.operation.resource.api.api_key;
  662. }
  663. if (this.headers.mock == null) {
  664. obj = {
  665. type: this.type,
  666. url: this.url,
  667. data: JSON.stringify(this.body),
  668. dataType: 'json',
  669. error: function(xhr, textStatus, error) {
  670. return _this.errorCallback(xhr, textStatus, error);
  671. },
  672. success: function(data) {
  673. return _this.successCallback(data);
  674. }
  675. };
  676. if (obj.type.toLowerCase() === "post" || obj.type.toLowerCase() === "put") {
  677. obj.contentType = "application/json";
  678. }
  679. jQuery.ajax(obj);
  680. }
  681. }
  682. SwaggerRequest.prototype.asCurl = function() {
  683. var header_args, k, v;
  684. header_args = (function() {
  685. var _ref, _results;
  686. _ref = this.headers;
  687. _results = [];
  688. for (k in _ref) {
  689. v = _ref[k];
  690. _results.push("--header \"" + k + ": " + v + "\"");
  691. }
  692. return _results;
  693. }).call(this);
  694. return "curl " + (header_args.join(" ")) + " " + this.url;
  695. };
  696. return SwaggerRequest;
  697. })();
  698. window.SwaggerApi = SwaggerApi;
  699. window.SwaggerResource = SwaggerResource;
  700. window.SwaggerOperation = SwaggerOperation;
  701. window.SwaggerRequest = SwaggerRequest;
  702. }).call(this);