swagger-ui_rails 0.1.7 → 2.1.0.alpha.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -2
  3. data/Rakefile +45 -0
  4. data/app/assets/images/explorer_icons.png +0 -0
  5. data/app/assets/javascripts/swagger-oauth.js +211 -0
  6. data/app/assets/javascripts/swagger-ui/index.js +13 -0
  7. data/app/assets/javascripts/{lib → swagger-ui/lib}/backbone-min.js +0 -0
  8. data/app/assets/javascripts/{lib → swagger-ui/lib}/handlebars-1.0.0.js +0 -0
  9. data/app/assets/javascripts/{lib → swagger-ui/lib}/highlight.7.3.pack.js +0 -0
  10. data/app/assets/javascripts/{lib → swagger-ui/lib}/jquery-1.8.0.min.js +0 -0
  11. data/app/assets/javascripts/{lib → swagger-ui/lib}/jquery.ba-bbq.min.js +0 -0
  12. data/app/assets/javascripts/{lib → swagger-ui/lib}/jquery.slideto.min.js +0 -0
  13. data/app/assets/javascripts/{lib → swagger-ui/lib}/jquery.wiggle.min.js +0 -0
  14. data/app/assets/javascripts/{lib → swagger-ui/lib}/shred.bundle.js +3 -3
  15. data/app/assets/javascripts/swagger-ui/lib/swagger-client.js +1606 -0
  16. data/app/assets/javascripts/swagger-ui/lib/swagger.js +1725 -0
  17. data/app/assets/javascripts/{lib → swagger-ui/lib}/underscore-min.js +0 -0
  18. data/app/assets/javascripts/swagger-ui/swagger-oauth.js +237 -0
  19. data/app/assets/javascripts/{swagger-ui.js.erb → swagger-ui/swagger-ui.js.erb} +1076 -520
  20. data/app/assets/stylesheets/swagger-ui/index.css +5 -0
  21. data/app/assets/stylesheets/swagger-ui/reset.css +125 -0
  22. data/app/assets/stylesheets/swagger-ui/screen.css +1224 -0
  23. data/app/views/swagger_ui/_swagger_ui.html.erb +1 -1
  24. data/lib/swagger-ui_rails/engine.rb +12 -2
  25. data/lib/swagger-ui_rails/version.rb +1 -1
  26. metadata +29 -28
  27. data/.gitignore +0 -17
  28. data/Gemfile +0 -4
  29. data/app/assets/javascripts/lib/shred/content.js +0 -193
  30. data/app/assets/javascripts/lib/swagger.js +0 -1480
  31. data/app/assets/stylesheets/hightlight.default.css +0 -135
  32. data/app/assets/stylesheets/screen.css +0 -1070
  33. data/app/assets/stylesheets/swagger-ui.css +0 -4
  34. data/swagger-ui_rails.gemspec +0 -23
@@ -0,0 +1,1725 @@
1
+ // swagger.js
2
+ // version 2.0.48
3
+
4
+ (function () {
5
+ var __bind = function (fn, me) {
6
+ return function () {
7
+ return fn.apply(me, arguments);
8
+ };
9
+ };
10
+
11
+ var log = function () {
12
+ log.history = log.history || [];
13
+ log.history.push(arguments);
14
+ if (this.console) {
15
+ console.log(Array.prototype.slice.call(arguments)[0]);
16
+ }
17
+ };
18
+
19
+ /**
20
+ * allows override of the default value based on the parameter being
21
+ * supplied
22
+ **/
23
+ var applyParameterMacro = function (model, parameter) {
24
+ var e = (typeof window !== 'undefined' ? window : exports);
25
+ if(e.parameterMacro)
26
+ return e.parameterMacro(model, parameter);
27
+ else
28
+ return parameter.defaultValue;
29
+ }
30
+
31
+ /**
32
+ * allows overriding the default value of an operation
33
+ **/
34
+ var applyModelPropertyMacro = function (operation, property) {
35
+ var e = (typeof window !== 'undefined' ? window : exports);
36
+ if(e.modelPropertyMacro)
37
+ return e.modelPropertyMacro(operation, property);
38
+ else
39
+ return property.defaultValue;
40
+ }
41
+
42
+ if (!Array.prototype.indexOf) {
43
+ Array.prototype.indexOf = function (obj, start) {
44
+ for (var i = (start || 0), j = this.length; i < j; i++) {
45
+ if (this[i] === obj) { return i; }
46
+ }
47
+ return -1;
48
+ }
49
+ }
50
+
51
+ if (!('filter' in Array.prototype)) {
52
+ Array.prototype.filter = function (filter, that /*opt*/) {
53
+ var other = [], v;
54
+ for (var i = 0, n = this.length; i < n; i++)
55
+ if (i in this && filter.call(that, v = this[i], i, this))
56
+ other.push(v);
57
+ return other;
58
+ };
59
+ }
60
+
61
+ if (!('map' in Array.prototype)) {
62
+ Array.prototype.map = function (mapper, that /*opt*/) {
63
+ var other = new Array(this.length);
64
+ for (var i = 0, n = this.length; i < n; i++)
65
+ if (i in this)
66
+ other[i] = mapper.call(that, this[i], i, this);
67
+ return other;
68
+ };
69
+ }
70
+
71
+ Object.keys = Object.keys || (function () {
72
+ var hasOwnProperty = Object.prototype.hasOwnProperty,
73
+ hasDontEnumBug = !{ toString: null }.propertyIsEnumerable('toString'),
74
+ DontEnums = [
75
+ 'toString',
76
+ 'toLocaleString',
77
+ 'valueOf',
78
+ 'hasOwnProperty',
79
+ 'isPrototypeOf',
80
+ 'propertyIsEnumerable',
81
+ 'constructor'
82
+ ],
83
+ DontEnumsLength = DontEnums.length;
84
+
85
+ return function (o) {
86
+ if (typeof o != 'object' && typeof o != 'function' || o === null)
87
+ throw new TypeError('Object.keys called on a non-object');
88
+
89
+ var result = [];
90
+ for (var name in o) {
91
+ if (hasOwnProperty.call(o, name))
92
+ result.push(name);
93
+ }
94
+
95
+ if (hasDontEnumBug) {
96
+ for (var i = 0; i < DontEnumsLength; i++) {
97
+ if (hasOwnProperty.call(o, DontEnums[i]))
98
+ result.push(DontEnums[i]);
99
+ }
100
+ }
101
+
102
+ return result;
103
+ };
104
+ })();
105
+
106
+ var SwaggerApi = function (url, options) {
107
+ this.isBuilt = false;
108
+ this.url = null;
109
+ this.debug = false;
110
+ this.basePath = null;
111
+ this.authorizations = null;
112
+ this.authorizationScheme = null;
113
+ this.info = null;
114
+ this.useJQuery = false;
115
+ this.modelsArray = [];
116
+ this.isValid;
117
+
118
+ options = (options || {});
119
+ if (url)
120
+ if (url.url)
121
+ options = url;
122
+ else
123
+ this.url = url;
124
+ else
125
+ options = url;
126
+
127
+ if (options.url != null)
128
+ this.url = options.url;
129
+
130
+ this.swaggerRequstHeaders = options.swaggerRequstHeaders || 'application/json,application/json;charset=utf-8,*/*';
131
+ this.defaultSuccessCallback = options.defaultSuccessCallback || null;
132
+ this.defaultErrorCallback = options.defaultErrorCallback || null;
133
+
134
+ if (options.success != null)
135
+ this.success = options.success;
136
+
137
+ if (typeof options.useJQuery === 'boolean')
138
+ this.useJQuery = options.useJQuery;
139
+
140
+ if (options.authorizations) {
141
+ this.clientAuthorizations = options.authorizations;
142
+ } else {
143
+ var e = (typeof window !== 'undefined' ? window : exports);
144
+ this.clientAuthorizations = e.authorizations;
145
+ }
146
+
147
+ this.failure = options.failure != null ? options.failure : function () { };
148
+ this.progress = options.progress != null ? options.progress : function () { };
149
+ if (options.success != null) {
150
+ this.build();
151
+ this.isBuilt = true;
152
+ }
153
+ };
154
+
155
+ SwaggerApi.prototype.build = function (mock) {
156
+ if (this.isBuilt)
157
+ return this;
158
+ var _this = this;
159
+ this.progress('fetching resource list: ' + this.url);
160
+ var obj = {
161
+ useJQuery: this.useJQuery,
162
+ url: this.url,
163
+ method: 'GET',
164
+ headers: {
165
+ accept: _this.swaggerRequstHeaders
166
+ },
167
+ on: {
168
+ error: function (response) {
169
+ if (_this.url.substring(0, 4) !== 'http') {
170
+ return _this.fail('Please specify the protocol for ' + _this.url);
171
+ } else if (response.status === 0) {
172
+ return _this.fail('Can\'t read from server. It may not have the appropriate access-control-origin settings.');
173
+ } else if (response.status === 404) {
174
+ return _this.fail('Can\'t read swagger JSON from ' + _this.url);
175
+ } else {
176
+ return _this.fail(response.status + ' : ' + response.statusText + ' ' + _this.url);
177
+ }
178
+ },
179
+ response: function (resp) {
180
+ var responseObj = resp.obj || JSON.parse(resp.data);
181
+ _this.swaggerVersion = responseObj.swaggerVersion;
182
+ if (_this.swaggerVersion === '1.2') {
183
+ return _this.buildFromSpec(responseObj);
184
+ } else {
185
+ return _this.buildFrom1_1Spec(responseObj);
186
+ }
187
+ }
188
+ }
189
+ };
190
+ var e = (typeof window !== 'undefined' ? window : exports);
191
+ e.authorizations.apply(obj);
192
+ if (mock === true)
193
+ return obj;
194
+
195
+ new SwaggerHttp().execute(obj);
196
+ return this;
197
+ };
198
+
199
+ SwaggerApi.prototype.buildFromSpec = function (response) {
200
+ if (response.apiVersion != null) {
201
+ this.apiVersion = response.apiVersion;
202
+ }
203
+ this.apis = {};
204
+ this.apisArray = [];
205
+ this.consumes = response.consumes;
206
+ this.produces = response.produces;
207
+ this.authSchemes = response.authorizations;
208
+ if (response.info != null) {
209
+ this.info = response.info;
210
+ }
211
+ var isApi = false;
212
+ var i;
213
+ for (i = 0; i < response.apis.length; i++) {
214
+ var api = response.apis[i];
215
+ if (api.operations) {
216
+ var j;
217
+ for (j = 0; j < api.operations.length; j++) {
218
+ operation = api.operations[j];
219
+ isApi = true;
220
+ }
221
+ }
222
+ }
223
+ if (response.basePath)
224
+ this.basePath = response.basePath;
225
+ else if (this.url.indexOf('?') > 0)
226
+ this.basePath = this.url.substring(0, this.url.lastIndexOf('?'));
227
+ else
228
+ this.basePath = this.url;
229
+
230
+ if (isApi) {
231
+ var newName = response.resourcePath.replace(/\//g, '');
232
+ this.resourcePath = response.resourcePath;
233
+ var res = new SwaggerResource(response, this);
234
+ this.apis[newName] = res;
235
+ this.apisArray.push(res);
236
+ } else {
237
+ var k;
238
+ for (k = 0; k < response.apis.length; k++) {
239
+ var resource = response.apis[k];
240
+ var res = new SwaggerResource(resource, this);
241
+ this.apis[res.name] = res;
242
+ this.apisArray.push(res);
243
+ }
244
+ }
245
+ this.isValid = true;
246
+ if (this.success) {
247
+ this.success();
248
+ }
249
+ return this;
250
+ };
251
+
252
+ SwaggerApi.prototype.buildFrom1_1Spec = function (response) {
253
+ log('This API is using a deprecated version of Swagger! Please see http://github.com/wordnik/swagger-core/wiki for more info');
254
+ if (response.apiVersion != null)
255
+ this.apiVersion = response.apiVersion;
256
+ this.apis = {};
257
+ this.apisArray = [];
258
+ this.produces = response.produces;
259
+ if (response.info != null) {
260
+ this.info = response.info;
261
+ }
262
+ var isApi = false;
263
+ for (var i = 0; i < response.apis.length; i++) {
264
+ var api = response.apis[i];
265
+ if (api.operations) {
266
+ for (var j = 0; j < api.operations.length; j++) {
267
+ operation = api.operations[j];
268
+ isApi = true;
269
+ }
270
+ }
271
+ }
272
+ if (response.basePath) {
273
+ this.basePath = response.basePath;
274
+ } else if (this.url.indexOf('?') > 0) {
275
+ this.basePath = this.url.substring(0, this.url.lastIndexOf('?'));
276
+ } else {
277
+ this.basePath = this.url;
278
+ }
279
+ if (isApi) {
280
+ var newName = response.resourcePath.replace(/\//g, '');
281
+ this.resourcePath = response.resourcePath;
282
+ var res = new SwaggerResource(response, this);
283
+ this.apis[newName] = res;
284
+ this.apisArray.push(res);
285
+ } else {
286
+ for (k = 0; k < response.apis.length; k++) {
287
+ resource = response.apis[k];
288
+ var res = new SwaggerResource(resource, this);
289
+ this.apis[res.name] = res;
290
+ this.apisArray.push(res);
291
+ }
292
+ }
293
+ this.isValid = true;
294
+ if (this.success) {
295
+ this.success();
296
+ }
297
+ return this;
298
+ };
299
+
300
+ SwaggerApi.prototype.selfReflect = function () {
301
+ var resource, resource_name, ref;
302
+ if (this.apis == null) {
303
+ return false;
304
+ }
305
+ ref = this.apis;
306
+ for (resource_name in ref) {
307
+ resource = ref[resource_name];
308
+ if (resource.ready == null) {
309
+ return false;
310
+ }
311
+ }
312
+ this.setConsolidatedModels();
313
+ this.ready = true;
314
+ if (this.success != null) {
315
+ return this.success();
316
+ }
317
+ };
318
+
319
+ SwaggerApi.prototype.fail = function (message) {
320
+ this.failure(message);
321
+ throw message;
322
+ };
323
+
324
+ SwaggerApi.prototype.setConsolidatedModels = function () {
325
+ var model, modelName, resource, resource_name, _i, _len, _ref, _ref1, _results;
326
+ this.models = {};
327
+ _ref = this.apis;
328
+ for (resource_name in _ref) {
329
+ resource = _ref[resource_name];
330
+ for (modelName in resource.models) {
331
+ if (this.models[modelName] == null) {
332
+ this.models[modelName] = resource.models[modelName];
333
+ this.modelsArray.push(resource.models[modelName]);
334
+ }
335
+ }
336
+ }
337
+ _ref1 = this.modelsArray;
338
+ _results = [];
339
+ for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
340
+ model = _ref1[_i];
341
+ _results.push(model.setReferencedModels(this.models));
342
+ }
343
+ return _results;
344
+ };
345
+
346
+ SwaggerApi.prototype.help = function () {
347
+ var operation, operation_name, parameter, resource, resource_name, _i, _len, _ref, _ref1, _ref2;
348
+ _ref = this.apis;
349
+ for (resource_name in _ref) {
350
+ resource = _ref[resource_name];
351
+ log(resource_name);
352
+ _ref1 = resource.operations;
353
+ for (operation_name in _ref1) {
354
+ operation = _ref1[operation_name];
355
+ log(' ' + operation.nickname);
356
+ _ref2 = operation.parameters;
357
+ for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
358
+ parameter = _ref2[_i];
359
+ log(' ' + parameter.name + (parameter.required ? ' (required)' : '') + ' - ' + parameter.description);
360
+ }
361
+ }
362
+ }
363
+ return this;
364
+ };
365
+
366
+ var SwaggerResource = function (resourceObj, api) {
367
+ var _this = this;
368
+ this.api = api;
369
+ this.swaggerRequstHeaders = api.swaggerRequstHeaders;
370
+ this.path = this.api.resourcePath != null ? this.api.resourcePath : resourceObj.path;
371
+ this.description = resourceObj.description;
372
+ this.authorizations = (resourceObj.authorizations || {});
373
+
374
+ var parts = this.path.split('/');
375
+ this.name = parts[parts.length - 1].replace('.{format}', '');
376
+ this.basePath = this.api.basePath;
377
+ this.operations = {};
378
+ this.operationsArray = [];
379
+ this.modelsArray = [];
380
+ this.models = {};
381
+ this.rawModels = {};
382
+ this.useJQuery = (typeof api.useJQuery !== 'undefined' ? api.useJQuery : null);
383
+
384
+ if ((resourceObj.apis != null) && (this.api.resourcePath != null)) {
385
+ this.addApiDeclaration(resourceObj);
386
+ } else {
387
+ if (this.path == null) {
388
+ this.api.fail('SwaggerResources must have a path.');
389
+ }
390
+ if (this.path.substring(0, 4) === 'http') {
391
+ this.url = this.path.replace('{format}', 'json');
392
+ } else {
393
+ this.url = this.api.basePath + this.path.replace('{format}', 'json');
394
+ }
395
+ this.api.progress('fetching resource ' + this.name + ': ' + this.url);
396
+ var obj = {
397
+ url: this.url,
398
+ method: 'GET',
399
+ useJQuery: this.useJQuery,
400
+ headers: {
401
+ accept: this.swaggerRequstHeaders
402
+ },
403
+ on: {
404
+ response: function (resp) {
405
+ var responseObj = resp.obj || JSON.parse(resp.data);
406
+ return _this.addApiDeclaration(responseObj);
407
+ },
408
+ error: function (response) {
409
+ return _this.api.fail('Unable to read api \'' +
410
+ _this.name + '\' from path ' + _this.url + ' (server returned ' + response.statusText + ')');
411
+ }
412
+ }
413
+ };
414
+ var e = typeof window !== 'undefined' ? window : exports;
415
+ e.authorizations.apply(obj);
416
+ new SwaggerHttp().execute(obj);
417
+ }
418
+ }
419
+
420
+ SwaggerResource.prototype.getAbsoluteBasePath = function (relativeBasePath) {
421
+ var pos, url;
422
+ url = this.api.basePath;
423
+ pos = url.lastIndexOf(relativeBasePath);
424
+ var parts = url.split('/');
425
+ var rootUrl = parts[0] + '//' + parts[2];
426
+
427
+ if (relativeBasePath.indexOf('http') === 0)
428
+ return relativeBasePath;
429
+ if (relativeBasePath === '/')
430
+ return rootUrl;
431
+ if (relativeBasePath.substring(0, 1) == '/') {
432
+ // use root + relative
433
+ return rootUrl + relativeBasePath;
434
+ }
435
+ else {
436
+ var pos = this.basePath.lastIndexOf('/');
437
+ var base = this.basePath.substring(0, pos);
438
+ if (base.substring(base.length - 1) == '/')
439
+ return base + relativeBasePath;
440
+ else
441
+ return base + '/' + relativeBasePath;
442
+ }
443
+ };
444
+
445
+ SwaggerResource.prototype.addApiDeclaration = function (response) {
446
+ if (response.produces != null)
447
+ this.produces = response.produces;
448
+ if (response.consumes != null)
449
+ this.consumes = response.consumes;
450
+ if ((response.basePath != null) && response.basePath.replace(/\s/g, '').length > 0)
451
+ this.basePath = response.basePath.indexOf('http') === -1 ? this.getAbsoluteBasePath(response.basePath) : response.basePath;
452
+
453
+ this.addModels(response.models);
454
+ if (response.apis) {
455
+ for (var i = 0 ; i < response.apis.length; i++) {
456
+ var endpoint = response.apis[i];
457
+ this.addOperations(endpoint.path, endpoint.operations, response.consumes, response.produces);
458
+ }
459
+ }
460
+ this.api[this.name] = this;
461
+ this.ready = true;
462
+ return this.api.selfReflect();
463
+ };
464
+
465
+ SwaggerResource.prototype.addModels = function (models) {
466
+ if (models != null) {
467
+ var modelName;
468
+ for (modelName in models) {
469
+ if (this.models[modelName] == null) {
470
+ var swaggerModel = new SwaggerModel(modelName, models[modelName]);
471
+ this.modelsArray.push(swaggerModel);
472
+ this.models[modelName] = swaggerModel;
473
+ this.rawModels[modelName] = models[modelName];
474
+ }
475
+ }
476
+ var output = [];
477
+ for (var i = 0; i < this.modelsArray.length; i++) {
478
+ var model = this.modelsArray[i];
479
+ output.push(model.setReferencedModels(this.models));
480
+ }
481
+ return output;
482
+ }
483
+ };
484
+
485
+ SwaggerResource.prototype.addOperations = function (resource_path, ops, consumes, produces) {
486
+ if (ops) {
487
+ var output = [];
488
+ for (var i = 0; i < ops.length; i++) {
489
+ var o = ops[i];
490
+ consumes = this.consumes;
491
+ produces = this.produces;
492
+ if (o.consumes != null)
493
+ consumes = o.consumes;
494
+ else
495
+ consumes = this.consumes;
496
+
497
+ if (o.produces != null)
498
+ produces = o.produces;
499
+ else
500
+ produces = this.produces;
501
+ var type = (o.type || o.responseClass);
502
+
503
+ if (type === 'array') {
504
+ ref = null;
505
+ if (o.items)
506
+ ref = o.items['type'] || o.items['$ref'];
507
+ type = 'array[' + ref + ']';
508
+ }
509
+ var responseMessages = o.responseMessages;
510
+ var method = o.method;
511
+ if (o.httpMethod) {
512
+ method = o.httpMethod;
513
+ }
514
+ if (o.supportedContentTypes) {
515
+ consumes = o.supportedContentTypes;
516
+ }
517
+ if (o.errorResponses) {
518
+ responseMessages = o.errorResponses;
519
+ for (var j = 0; j < responseMessages.length; j++) {
520
+ r = responseMessages[j];
521
+ r.message = r.reason;
522
+ r.reason = null;
523
+ }
524
+ }
525
+ o.nickname = this.sanitize(o.nickname);
526
+ var op = new SwaggerOperation(o.nickname, resource_path, method, o.parameters, o.summary, o.notes, type, responseMessages, this, consumes, produces, o.authorizations, o.deprecated);
527
+ this.operations[op.nickname] = op;
528
+ output.push(this.operationsArray.push(op));
529
+ }
530
+ return output;
531
+ }
532
+ };
533
+
534
+ SwaggerResource.prototype.sanitize = function (nickname) {
535
+ var op;
536
+ op = nickname.replace(/[\s!@#$%^&*()_+=\[{\]};:<>|.\/?,\\'""-]/g, '_');
537
+ op = op.replace(/((_){2,})/g, '_');
538
+ op = op.replace(/^(_)*/g, '');
539
+ op = op.replace(/([_])*$/g, '');
540
+ return op;
541
+ };
542
+
543
+ var SwaggerModel = function (modelName, obj) {
544
+ this.name = obj.id != null ? obj.id : modelName;
545
+ this.properties = [];
546
+ var propertyName;
547
+ for (propertyName in obj.properties) {
548
+ if (obj.required != null) {
549
+ var value;
550
+ for (value in obj.required) {
551
+ if (propertyName === obj.required[value]) {
552
+ obj.properties[propertyName].required = true;
553
+ }
554
+ }
555
+ }
556
+ var prop = new SwaggerModelProperty(propertyName, obj.properties[propertyName], this);
557
+ this.properties.push(prop);
558
+ }
559
+ }
560
+
561
+ SwaggerModel.prototype.setReferencedModels = function (allModels) {
562
+ var results = [];
563
+ for (var i = 0; i < this.properties.length; i++) {
564
+ var property = this.properties[i];
565
+ var type = property.type || property.dataType;
566
+ if (allModels[type] != null)
567
+ results.push(property.refModel = allModels[type]);
568
+ else if ((property.refDataType != null) && (allModels[property.refDataType] != null))
569
+ results.push(property.refModel = allModels[property.refDataType]);
570
+ else
571
+ results.push(void 0);
572
+ }
573
+ return results;
574
+ };
575
+
576
+ SwaggerModel.prototype.getMockSignature = function (modelsToIgnore) {
577
+ var propertiesStr = [];
578
+ for (var i = 0; i < this.properties.length; i++) {
579
+ var prop = this.properties[i];
580
+ propertiesStr.push(prop.toString());
581
+ }
582
+
583
+ var strong = '<span class="strong">';
584
+ var strongClose = '</span>';
585
+ var classOpen = strong + this.name + ' {' + strongClose;
586
+ var classClose = strong + '}' + strongClose;
587
+ var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
588
+ if (!modelsToIgnore)
589
+ modelsToIgnore = [];
590
+ modelsToIgnore.push(this.name);
591
+
592
+ for (var i = 0; i < this.properties.length; i++) {
593
+ var prop = this.properties[i];
594
+ if ((prop.refModel != null) && modelsToIgnore.indexOf(prop.refModel.name) === -1) {
595
+ returnVal = returnVal + ('<br>' + prop.refModel.getMockSignature(modelsToIgnore));
596
+ }
597
+ }
598
+ return returnVal;
599
+ };
600
+
601
+ SwaggerModel.prototype.createJSONSample = function (modelsToIgnore) {
602
+ if (sampleModels[this.name]) {
603
+ return sampleModels[this.name];
604
+ }
605
+ else {
606
+ var result = {};
607
+ var modelsToIgnore = (modelsToIgnore || [])
608
+ modelsToIgnore.push(this.name);
609
+ for (var i = 0; i < this.properties.length; i++) {
610
+ var prop = this.properties[i];
611
+ result[prop.name] = prop.getSampleValue(modelsToIgnore);
612
+ }
613
+ modelsToIgnore.pop(this.name);
614
+ return result;
615
+ }
616
+ };
617
+
618
+ var SwaggerModelProperty = function (name, obj, model) {
619
+ this.name = name;
620
+ this.dataType = obj.type || obj.dataType || obj['$ref'];
621
+ this.isCollection = this.dataType && (this.dataType.toLowerCase() === 'array' || this.dataType.toLowerCase() === 'list' || this.dataType.toLowerCase() === 'set');
622
+ this.descr = obj.description;
623
+ this.required = obj.required;
624
+ this.defaultValue = applyModelPropertyMacro(obj, model);
625
+ if (obj.items != null) {
626
+ if (obj.items.type != null) {
627
+ this.refDataType = obj.items.type;
628
+ }
629
+ if (obj.items.$ref != null) {
630
+ this.refDataType = obj.items.$ref;
631
+ }
632
+ }
633
+ this.dataTypeWithRef = this.refDataType != null ? (this.dataType + '[' + this.refDataType + ']') : this.dataType;
634
+ if (obj.allowableValues != null) {
635
+ this.valueType = obj.allowableValues.valueType;
636
+ this.values = obj.allowableValues.values;
637
+ if (this.values != null) {
638
+ this.valuesString = '\'' + this.values.join('\' or \'') + '\'';
639
+ }
640
+ }
641
+ if (obj['enum'] != null) {
642
+ this.valueType = 'string';
643
+ this.values = obj['enum'];
644
+ if (this.values != null) {
645
+ this.valueString = '\'' + this.values.join('\' or \'') + '\'';
646
+ }
647
+ }
648
+ }
649
+
650
+ SwaggerModelProperty.prototype.getSampleValue = function (modelsToIgnore) {
651
+ var result;
652
+ if ((this.refModel != null) && (modelsToIgnore.indexOf(this.refModel.name) === -1)) {
653
+ result = this.refModel.createJSONSample(modelsToIgnore);
654
+ } else {
655
+ if (this.isCollection) {
656
+ result = this.toSampleValue(this.refDataType);
657
+ } else {
658
+ result = this.toSampleValue(this.dataType);
659
+ }
660
+ }
661
+ if (this.isCollection) {
662
+ return [result];
663
+ } else {
664
+ return result;
665
+ }
666
+ };
667
+
668
+ SwaggerModelProperty.prototype.toSampleValue = function (value) {
669
+ var result;
670
+ if ((typeof this.defaultValue !== 'undefined') && this.defaultValue !== null) {
671
+ result = this.defaultValue;
672
+ } else if (value === 'integer') {
673
+ result = 0;
674
+ } else if (value === 'boolean') {
675
+ result = false;
676
+ } else if (value === 'double' || value === 'number') {
677
+ result = 0.0;
678
+ } else if (value === 'string') {
679
+ result = '';
680
+ } else {
681
+ result = value;
682
+ }
683
+ return result;
684
+ };
685
+
686
+ SwaggerModelProperty.prototype.toString = function () {
687
+ var req = this.required ? 'propReq' : 'propOpt';
688
+ var str = '<span class="propName ' + req + '">' + this.name + '</span> (<span class="propType">' + this.dataTypeWithRef + '</span>';
689
+ if (!this.required) {
690
+ str += ', <span class="propOptKey">optional</span>';
691
+ }
692
+ str += ')';
693
+ if (this.values != null) {
694
+ str += ' = <span class="propVals">["' + this.values.join('\' or \'') + '\']</span>';
695
+ }
696
+ if (this.descr != null) {
697
+ str += ': <span class="propDesc">' + this.descr + '</span>';
698
+ }
699
+ return str;
700
+ };
701
+
702
+ var SwaggerOperation = function (nickname, path, method, parameters, summary, notes, type, responseMessages, resource, consumes, produces, authorizations, deprecated) {
703
+ var _this = this;
704
+
705
+ var errors = [];
706
+ this.nickname = (nickname || errors.push('SwaggerOperations must have a nickname.'));
707
+ this.path = (path || errors.push('SwaggerOperation ' + nickname + ' is missing path.'));
708
+ this.method = (method || errors.push('SwaggerOperation ' + nickname + ' is missing method.'));
709
+ this.parameters = parameters != null ? parameters : [];
710
+ this.summary = summary;
711
+ this.notes = notes;
712
+ this.type = type;
713
+ this.responseMessages = (responseMessages || []);
714
+ this.resource = (resource || errors.push('Resource is required'));
715
+ this.consumes = consumes;
716
+ this.produces = produces;
717
+ this.authorizations = typeof authorizations !== 'undefined' ? authorizations : resource.authorizations;
718
+ this.deprecated = deprecated;
719
+ this['do'] = __bind(this['do'], this);
720
+
721
+ if (errors.length > 0) {
722
+ console.error('SwaggerOperation errors', errors, arguments);
723
+ this.resource.api.fail(errors);
724
+ }
725
+
726
+ this.path = this.path.replace('{format}', 'json');
727
+ this.method = this.method.toLowerCase();
728
+ this.isGetMethod = this.method === 'GET';
729
+
730
+ this.resourceName = this.resource.name;
731
+ if (typeof this.type !== 'undefined' && this.type === 'void')
732
+ this.type = null;
733
+ else {
734
+ this.responseClassSignature = this.getSignature(this.type, this.resource.models);
735
+ this.responseSampleJSON = this.getSampleJSON(this.type, this.resource.models);
736
+ }
737
+
738
+ for (var i = 0; i < this.parameters.length; i++) {
739
+ var param = this.parameters[i];
740
+ // might take this away
741
+ param.name = param.name || param.type || param.dataType;
742
+
743
+ // for 1.1 compatibility
744
+ var type = param.type || param.dataType;
745
+ if (type === 'array') {
746
+ type = 'array[' + (param.items.$ref ? param.items.$ref : param.items.type) + ']';
747
+ }
748
+ param.type = type;
749
+
750
+ if (type && type.toLowerCase() === 'boolean') {
751
+ param.allowableValues = {};
752
+ param.allowableValues.values = ['true', 'false'];
753
+ }
754
+ param.signature = this.getSignature(type, this.resource.models);
755
+ param.sampleJSON = this.getSampleJSON(type, this.resource.models);
756
+
757
+ var enumValue = param['enum'];
758
+ if (enumValue != null) {
759
+ param.isList = true;
760
+ param.allowableValues = {};
761
+ param.allowableValues.descriptiveValues = [];
762
+
763
+ for (var j = 0; j < enumValue.length; j++) {
764
+ var v = enumValue[j];
765
+ if (param.defaultValue != null) {
766
+ param.allowableValues.descriptiveValues.push({
767
+ value: String(v),
768
+ isDefault: (v === param.defaultValue)
769
+ });
770
+ }
771
+ else {
772
+ param.allowableValues.descriptiveValues.push({
773
+ value: String(v),
774
+ isDefault: false
775
+ });
776
+ }
777
+ }
778
+ }
779
+ else if (param.allowableValues != null) {
780
+ if (param.allowableValues.valueType === 'RANGE')
781
+ param.isRange = true;
782
+ else
783
+ param.isList = true;
784
+ if (param.allowableValues != null) {
785
+ param.allowableValues.descriptiveValues = [];
786
+ if (param.allowableValues.values) {
787
+ for (var j = 0; j < param.allowableValues.values.length; j++) {
788
+ var v = param.allowableValues.values[j];
789
+ if (param.defaultValue != null) {
790
+ param.allowableValues.descriptiveValues.push({
791
+ value: String(v),
792
+ isDefault: (v === param.defaultValue)
793
+ });
794
+ }
795
+ else {
796
+ param.allowableValues.descriptiveValues.push({
797
+ value: String(v),
798
+ isDefault: false
799
+ });
800
+ }
801
+ }
802
+ }
803
+ }
804
+ }
805
+ param.defaultValue = applyParameterMacro(param, this);
806
+ }
807
+ var defaultSuccessCallback = this.resource.api.defaultSuccessCallback || null;
808
+ var defaultErrorCallback = this.resource.api.defaultErrorCallback || null;
809
+
810
+ this.resource[this.nickname] = function (args, opts, callback, error) {
811
+ var arg1 = args,
812
+ arg2 = opts,
813
+ arg3 = callback || defaultSuccessCallback,
814
+ arg4 = error || defaultErrorCallback;
815
+ if(typeof opts === 'function') {
816
+ arg2 = {};
817
+ arg3 = opts;
818
+ arg4 = error;
819
+ }
820
+ return _this['do'](arg1, arg2, arg3, arg4);
821
+ };
822
+ this.resource[this.nickname].help = function () {
823
+ return _this.help();
824
+ };
825
+ this.resource[this.nickname].asCurl = function (args) {
826
+ return _this.asCurl(args);
827
+ };
828
+ }
829
+
830
+ SwaggerOperation.prototype.isListType = function (type) {
831
+ if (type && type.indexOf('[') >= 0) {
832
+ return type.substring(type.indexOf('[') + 1, type.indexOf(']'));
833
+ } else {
834
+ return void 0;
835
+ }
836
+ };
837
+
838
+ SwaggerOperation.prototype.getSignature = function (type, models) {
839
+ var isPrimitive, listType;
840
+ listType = this.isListType(type);
841
+ isPrimitive = ((listType != null) && models[listType]) || (models[type] != null) ? false : true;
842
+ if (isPrimitive) {
843
+ return type;
844
+ } else {
845
+ if (listType != null) {
846
+ return models[listType].getMockSignature();
847
+ } else {
848
+ return models[type].getMockSignature();
849
+ }
850
+ }
851
+ };
852
+
853
+ SwaggerOperation.prototype.getSampleJSON = function (type, models) {
854
+ var isPrimitive, listType, val;
855
+ listType = this.isListType(type);
856
+ isPrimitive = ((listType != null) && models[listType]) || (models[type] != null) ? false : true;
857
+ val = isPrimitive ? void 0 : (listType != null ? models[listType].createJSONSample() : models[type].createJSONSample());
858
+ if (val) {
859
+ val = listType ? [val] : val;
860
+ if (typeof val == 'string')
861
+ return val;
862
+ else if (typeof val === 'object') {
863
+ var t = val;
864
+ if (val instanceof Array && val.length > 0) {
865
+ t = val[0];
866
+ }
867
+ if (t.nodeName) {
868
+ var xmlString = new XMLSerializer().serializeToString(t);
869
+ return this.formatXml(xmlString);
870
+ }
871
+ else
872
+ return JSON.stringify(val, null, 2);
873
+ }
874
+ else
875
+ return val;
876
+ }
877
+ };
878
+
879
+ SwaggerOperation.prototype['do'] = function (args, opts, callback, error) {
880
+ var key, param, params, possibleParams, req, value;
881
+ args = args || {};
882
+ opts = opts || {};
883
+
884
+ if ((typeof args) === 'function') {
885
+ error = opts;
886
+ callback = args;
887
+ args = {};
888
+ }
889
+ if ((typeof opts) === 'function') {
890
+ error = callback;
891
+ callback = opts;
892
+ }
893
+ if (error == null) {
894
+ error = function (xhr, textStatus, error) {
895
+ return log(xhr, textStatus, error);
896
+ };
897
+ }
898
+ if (callback == null) {
899
+ callback = function (response) {
900
+ var content;
901
+ content = null;
902
+ if (response != null) {
903
+ content = response.data;
904
+ } else {
905
+ content = 'no data';
906
+ }
907
+ return log('default callback: ' + content);
908
+ };
909
+ }
910
+ params = {};
911
+ params.headers = [];
912
+ if (args.headers != null) {
913
+ params.headers = args.headers;
914
+ delete args.headers;
915
+ }
916
+ // allow override from the opts
917
+ if(opts && opts.responseContentType) {
918
+ params.headers['Content-Type'] = opts.responseContentType;
919
+ }
920
+ if(opts && opts.requestContentType) {
921
+ params.headers['Accept'] = opts.requestContentType;
922
+ }
923
+
924
+ var possibleParams = [];
925
+ for (var i = 0; i < this.parameters.length; i++) {
926
+ var param = this.parameters[i];
927
+ if (param.paramType === 'header') {
928
+ if (typeof args[param.name] !== 'undefined')
929
+ params.headers[param.name] = args[param.name];
930
+ }
931
+ else if (param.paramType === 'form' || param.paramType.toLowerCase() === 'file')
932
+ possibleParams.push(param);
933
+ else if (param.paramType === 'body' && param.name !== 'body' && typeof args[param.name] !== 'undefined') {
934
+ if (args.body) {
935
+ throw new Error('Saw two body params in an API listing; expecting a max of one.');
936
+ }
937
+ args.body = args[param.name];
938
+ }
939
+ }
940
+
941
+ if (args.body != null) {
942
+ params.body = args.body;
943
+ delete args.body;
944
+ }
945
+
946
+ if (possibleParams) {
947
+ var key;
948
+ for (key in possibleParams) {
949
+ var value = possibleParams[key];
950
+ if (args[value.name]) {
951
+ params[value.name] = args[value.name];
952
+ }
953
+ }
954
+ }
955
+ req = new SwaggerRequest(this.method, this.urlify(args), params, opts, callback, error, this);
956
+ if (opts.mock != null) {
957
+ return req;
958
+ } else {
959
+ return true;
960
+ }
961
+ };
962
+
963
+ SwaggerOperation.prototype.pathJson = function () {
964
+ return this.path.replace('{format}', 'json');
965
+ };
966
+
967
+ SwaggerOperation.prototype.pathXml = function () {
968
+ return this.path.replace('{format}', 'xml');
969
+ };
970
+
971
+ SwaggerOperation.prototype.encodePathParam = function (pathParam) {
972
+ var encParts, part, parts, _i, _len;
973
+ pathParam = pathParam.toString();
974
+ if (pathParam.indexOf('/') === -1) {
975
+ return encodeURIComponent(pathParam);
976
+ } else {
977
+ parts = pathParam.split('/');
978
+ encParts = [];
979
+ for (_i = 0, _len = parts.length; _i < _len; _i++) {
980
+ part = parts[_i];
981
+ encParts.push(encodeURIComponent(part));
982
+ }
983
+ return encParts.join('/');
984
+ }
985
+ };
986
+
987
+ SwaggerOperation.prototype.urlify = function (args) {
988
+ var url = this.resource.basePath + this.pathJson();
989
+ var params = this.parameters;
990
+ for (var i = 0; i < params.length; i++) {
991
+ var param = params[i];
992
+ if (param.paramType === 'path') {
993
+ if (typeof args[param.name] !== 'undefined') {
994
+ // apply path params and remove from args
995
+ var reg = new RegExp('\\{\\s*?' + param.name + '.*?\\}(?=\\s*?(\\/?|$))', 'gi');
996
+ url = url.replace(reg, this.encodePathParam(args[param.name]));
997
+ delete args[param.name];
998
+ }
999
+ else
1000
+ throw '' + param.name + ' is a required path param.';
1001
+ }
1002
+ }
1003
+
1004
+ var queryParams = '';
1005
+ for (var i = 0; i < params.length; i++) {
1006
+ var param = params[i];
1007
+ if(param.paramType === 'query') {
1008
+ if (queryParams !== '')
1009
+ queryParams += '&';
1010
+ if (Array.isArray(param)) {
1011
+ var j;
1012
+ var output = '';
1013
+ for(j = 0; j < param.length; j++) {
1014
+ if(j > 0)
1015
+ output += ',';
1016
+ output += encodeURIComponent(param[j]);
1017
+ }
1018
+ queryParams += encodeURIComponent(param.name) + '=' + output;
1019
+ }
1020
+ else {
1021
+ if (typeof args[param.name] !== 'undefined') {
1022
+ queryParams += encodeURIComponent(param.name) + '=' + encodeURIComponent(args[param.name]);
1023
+ } else {
1024
+ if (param.required)
1025
+ throw '' + param.name + ' is a required query param.';
1026
+ }
1027
+ }
1028
+ }
1029
+ }
1030
+ if ((queryParams != null) && queryParams.length > 0)
1031
+ url += '?' + queryParams;
1032
+ return url;
1033
+ };
1034
+
1035
+ SwaggerOperation.prototype.supportHeaderParams = function () {
1036
+ return this.resource.api.supportHeaderParams;
1037
+ };
1038
+
1039
+ SwaggerOperation.prototype.supportedSubmitMethods = function () {
1040
+ return this.resource.api.supportedSubmitMethods;
1041
+ };
1042
+
1043
+ SwaggerOperation.prototype.getQueryParams = function (args) {
1044
+ return this.getMatchingParams(['query'], args);
1045
+ };
1046
+
1047
+ SwaggerOperation.prototype.getHeaderParams = function (args) {
1048
+ return this.getMatchingParams(['header'], args);
1049
+ };
1050
+
1051
+ SwaggerOperation.prototype.getMatchingParams = function (paramTypes, args) {
1052
+ var matchingParams = {};
1053
+ var params = this.parameters;
1054
+ for (var i = 0; i < params.length; i++) {
1055
+ param = params[i];
1056
+ if (args && args[param.name])
1057
+ matchingParams[param.name] = args[param.name];
1058
+ }
1059
+ var headers = this.resource.api.headers;
1060
+ var name;
1061
+ for (name in headers) {
1062
+ var value = headers[name];
1063
+ matchingParams[name] = value;
1064
+ }
1065
+ return matchingParams;
1066
+ };
1067
+
1068
+ SwaggerOperation.prototype.help = function () {
1069
+ var msg = '';
1070
+ var params = this.parameters;
1071
+ for (var i = 0; i < params.length; i++) {
1072
+ var param = params[i];
1073
+ if (msg !== '')
1074
+ msg += '\n';
1075
+ msg += '* ' + param.name + (param.required ? ' (required)' : '') + " - " + param.description;
1076
+ }
1077
+ return msg;
1078
+ };
1079
+
1080
+ SwaggerOperation.prototype.asCurl = function (args) {
1081
+ var results = [];
1082
+ var i;
1083
+
1084
+ var headers = SwaggerRequest.prototype.setHeaders(args, {}, this);
1085
+ for(i = 0; i < this.parameters.length; i++) {
1086
+ var param = this.parameters[i];
1087
+ if(param.paramType && param.paramType === 'header' && args[param.name]) {
1088
+ headers[param.name] = args[param.name];
1089
+ }
1090
+ }
1091
+
1092
+ var key;
1093
+ for (key in headers) {
1094
+ results.push('--header "' + key + ': ' + headers[key] + '"');
1095
+ }
1096
+ return 'curl ' + (results.join(' ')) + ' ' + this.urlify(args);
1097
+ };
1098
+
1099
+ SwaggerOperation.prototype.formatXml = function (xml) {
1100
+ var contexp, formatted, indent, lastType, lines, ln, pad, reg, transitions, wsexp, _fn, _i, _len;
1101
+ reg = /(>)(<)(\/*)/g;
1102
+ wsexp = /[ ]*(.*)[ ]+\n/g;
1103
+ contexp = /(<.+>)(.+\n)/g;
1104
+ xml = xml.replace(reg, '$1\n$2$3').replace(wsexp, '$1\n').replace(contexp, '$1\n$2');
1105
+ pad = 0;
1106
+ formatted = '';
1107
+ lines = xml.split('\n');
1108
+ indent = 0;
1109
+ lastType = 'other';
1110
+ transitions = {
1111
+ 'single->single': 0,
1112
+ 'single->closing': -1,
1113
+ 'single->opening': 0,
1114
+ 'single->other': 0,
1115
+ 'closing->single': 0,
1116
+ 'closing->closing': -1,
1117
+ 'closing->opening': 0,
1118
+ 'closing->other': 0,
1119
+ 'opening->single': 1,
1120
+ 'opening->closing': 0,
1121
+ 'opening->opening': 1,
1122
+ 'opening->other': 1,
1123
+ 'other->single': 0,
1124
+ 'other->closing': -1,
1125
+ 'other->opening': 0,
1126
+ 'other->other': 0
1127
+ };
1128
+ _fn = function (ln) {
1129
+ var fromTo, j, key, padding, type, types, value;
1130
+ types = {
1131
+ single: Boolean(ln.match(/<.+\/>/)),
1132
+ closing: Boolean(ln.match(/<\/.+>/)),
1133
+ opening: Boolean(ln.match(/<[^!?].*>/))
1134
+ };
1135
+ type = ((function () {
1136
+ var _results;
1137
+ _results = [];
1138
+ for (key in types) {
1139
+ value = types[key];
1140
+ if (value) {
1141
+ _results.push(key);
1142
+ }
1143
+ }
1144
+ return _results;
1145
+ })())[0];
1146
+ type = type === void 0 ? 'other' : type;
1147
+ fromTo = lastType + '->' + type;
1148
+ lastType = type;
1149
+ padding = '';
1150
+ indent += transitions[fromTo];
1151
+ padding = ((function () {
1152
+ var _j, _ref5, _results;
1153
+ _results = [];
1154
+ for (j = _j = 0, _ref5 = indent; 0 <= _ref5 ? _j < _ref5 : _j > _ref5; j = 0 <= _ref5 ? ++_j : --_j) {
1155
+ _results.push(' ');
1156
+ }
1157
+ return _results;
1158
+ })()).join('');
1159
+ if (fromTo === 'opening->closing') {
1160
+ return formatted = formatted.substr(0, formatted.length - 1) + ln + '\n';
1161
+ } else {
1162
+ return formatted += padding + ln + '\n';
1163
+ }
1164
+ };
1165
+ for (_i = 0, _len = lines.length; _i < _len; _i++) {
1166
+ ln = lines[_i];
1167
+ _fn(ln);
1168
+ }
1169
+ return formatted;
1170
+ };
1171
+
1172
+ var SwaggerRequest = function (type, url, params, opts, successCallback, errorCallback, operation, execution) {
1173
+ var _this = this;
1174
+ var errors = [];
1175
+ this.useJQuery = (typeof operation.resource.useJQuery !== 'undefined' ? operation.resource.useJQuery : null);
1176
+ this.type = (type || errors.push('SwaggerRequest type is required (get/post/put/delete/patch/options).'));
1177
+ this.url = (url || errors.push('SwaggerRequest url is required.'));
1178
+ this.params = params;
1179
+ this.opts = opts;
1180
+ this.successCallback = (successCallback || errors.push('SwaggerRequest successCallback is required.'));
1181
+ this.errorCallback = (errorCallback || errors.push('SwaggerRequest error callback is required.'));
1182
+ this.operation = (operation || errors.push('SwaggerRequest operation is required.'));
1183
+ this.execution = execution;
1184
+ this.headers = (params.headers || {});
1185
+
1186
+ if (errors.length > 0) {
1187
+ throw errors;
1188
+ }
1189
+
1190
+ this.type = this.type.toUpperCase();
1191
+
1192
+ // set request, response content type headers
1193
+ var headers = this.setHeaders(params, opts, this.operation);
1194
+ var body = params.body;
1195
+
1196
+ // encode the body for form submits
1197
+ if (headers['Content-Type']) {
1198
+ var values = {};
1199
+ var i;
1200
+ var operationParams = this.operation.parameters;
1201
+ for (i = 0; i < operationParams.length; i++) {
1202
+ var param = operationParams[i];
1203
+ if (param.paramType === 'form')
1204
+ values[param.name] = param;
1205
+ }
1206
+
1207
+ if (headers['Content-Type'].indexOf('application/x-www-form-urlencoded') === 0) {
1208
+ var encoded = '';
1209
+ var key, value;
1210
+ for (key in values) {
1211
+ value = this.params[key];
1212
+ if (typeof value !== 'undefined') {
1213
+ if (encoded !== '')
1214
+ encoded += '&';
1215
+ encoded += encodeURIComponent(key) + '=' + encodeURIComponent(value);
1216
+ }
1217
+ }
1218
+ body = encoded;
1219
+ }
1220
+ else if (headers['Content-Type'].indexOf('multipart/form-data') === 0) {
1221
+ // encode the body for form submits
1222
+ var data = '';
1223
+ var boundary = '----SwaggerFormBoundary' + Date.now();
1224
+ var key, value;
1225
+ for (key in values) {
1226
+ value = this.params[key];
1227
+ if (typeof value !== 'undefined') {
1228
+ data += '--' + boundary + '\n';
1229
+ data += 'Content-Disposition: form-data; name="' + key + '"';
1230
+ data += '\n\n';
1231
+ data += value + '\n';
1232
+ }
1233
+ }
1234
+ data += '--' + boundary + '--\n';
1235
+ headers['Content-Type'] = 'multipart/form-data; boundary=' + boundary;
1236
+ body = data;
1237
+ }
1238
+ }
1239
+
1240
+ var obj;
1241
+ if (!((this.headers != null) && (this.headers.mock != null))) {
1242
+ obj = {
1243
+ url: this.url,
1244
+ method: this.type,
1245
+ headers: headers,
1246
+ body: body,
1247
+ useJQuery: this.useJQuery,
1248
+ on: {
1249
+ error: function (response) {
1250
+ return _this.errorCallback(response, _this.opts.parent);
1251
+ },
1252
+ redirect: function (response) {
1253
+ return _this.successCallback(response, _this.opts.parent);
1254
+ },
1255
+ 307: function (response) {
1256
+ return _this.successCallback(response, _this.opts.parent);
1257
+ },
1258
+ response: function (response) {
1259
+ return _this.successCallback(response, _this.opts.parent);
1260
+ }
1261
+ }
1262
+ };
1263
+
1264
+ var status = false;
1265
+ if (this.operation.resource && this.operation.resource.api && this.operation.resource.api.clientAuthorizations) {
1266
+ // Get the client authorizations from the resource declaration
1267
+ status = this.operation.resource.api.clientAuthorizations.apply(obj, this.operation.authorizations);
1268
+ } else {
1269
+ // Get the client authorization from the default authorization declaration
1270
+ var e;
1271
+ if (typeof window !== 'undefined') {
1272
+ e = window;
1273
+ } else {
1274
+ e = exports;
1275
+ }
1276
+ status = e.authorizations.apply(obj, this.operation.authorizations);
1277
+ }
1278
+
1279
+ if (opts.mock == null) {
1280
+ if (status !== false) {
1281
+ new SwaggerHttp().execute(obj);
1282
+ } else {
1283
+ obj.canceled = true;
1284
+ }
1285
+ } else {
1286
+ return obj;
1287
+ }
1288
+ }
1289
+ return obj;
1290
+ };
1291
+
1292
+ SwaggerRequest.prototype.setHeaders = function (params, opts, operation) {
1293
+ // default type
1294
+ var accepts = opts.responseContentType || 'application/json';
1295
+ var consumes = opts.requestContentType || 'application/json';
1296
+
1297
+ var allDefinedParams = operation.parameters;
1298
+ var definedFormParams = [];
1299
+ var definedFileParams = [];
1300
+ var body = params.body;
1301
+ var headers = {};
1302
+
1303
+ // get params from the operation and set them in definedFileParams, definedFormParams, headers
1304
+ var i;
1305
+ for (i = 0; i < allDefinedParams.length; i++) {
1306
+ var param = allDefinedParams[i];
1307
+ if (param.paramType === 'form')
1308
+ definedFormParams.push(param);
1309
+ else if (param.paramType === 'file')
1310
+ definedFileParams.push(param);
1311
+ else if (param.paramType === 'header' && this.params.headers) {
1312
+ var key = param.name;
1313
+ var headerValue = this.params.headers[param.name];
1314
+ if (typeof this.params.headers[param.name] !== 'undefined')
1315
+ headers[key] = headerValue;
1316
+ }
1317
+ }
1318
+
1319
+ // if there's a body, need to set the accepts header via requestContentType
1320
+ if (body && (this.type === 'POST' || this.type === 'PUT' || this.type === 'PATCH' || this.type === 'DELETE')) {
1321
+ if (this.opts.requestContentType)
1322
+ consumes = this.opts.requestContentType;
1323
+ } else {
1324
+ // if any form params, content type must be set
1325
+ if (definedFormParams.length > 0) {
1326
+ if (definedFileParams.length > 0)
1327
+ consumes = 'multipart/form-data';
1328
+ else
1329
+ consumes = 'application/x-www-form-urlencoded';
1330
+ }
1331
+ else if (this.type === 'DELETE')
1332
+ body = '{}';
1333
+ else if (this.type != 'DELETE')
1334
+ consumes = null;
1335
+ }
1336
+
1337
+ if (consumes && this.operation.consumes) {
1338
+ if (this.operation.consumes.indexOf(consumes) === -1) {
1339
+ log('server doesn\'t consume ' + consumes + ', try ' + JSON.stringify(this.operation.consumes));
1340
+ }
1341
+ }
1342
+
1343
+ if (this.opts && this.opts.responseContentType) {
1344
+ accepts = this.opts.responseContentType;
1345
+ } else {
1346
+ accepts = 'application/json';
1347
+ }
1348
+ if (accepts && operation.produces) {
1349
+ if (operation.produces.indexOf(accepts) === -1) {
1350
+ log('server can\'t produce ' + accepts);
1351
+ }
1352
+ }
1353
+
1354
+ if ((consumes && body !== '') || (consumes === 'application/x-www-form-urlencoded'))
1355
+ headers['Content-Type'] = consumes;
1356
+ if (accepts)
1357
+ headers['Accept'] = accepts;
1358
+ return headers;
1359
+ }
1360
+
1361
+ /**
1362
+ * SwaggerHttp is a wrapper for executing requests
1363
+ */
1364
+ var SwaggerHttp = function () { };
1365
+
1366
+ SwaggerHttp.prototype.execute = function (obj) {
1367
+ if (obj && (typeof obj.useJQuery === 'boolean'))
1368
+ this.useJQuery = obj.useJQuery;
1369
+ else
1370
+ this.useJQuery = this.isIE8();
1371
+
1372
+ if (this.useJQuery)
1373
+ return new JQueryHttpClient().execute(obj);
1374
+ else
1375
+ return new ShredHttpClient().execute(obj);
1376
+ }
1377
+
1378
+ SwaggerHttp.prototype.isIE8 = function () {
1379
+ var detectedIE = false;
1380
+ if (typeof navigator !== 'undefined' && navigator.userAgent) {
1381
+ nav = navigator.userAgent.toLowerCase();
1382
+ if (nav.indexOf('msie') !== -1) {
1383
+ var version = parseInt(nav.split('msie')[1]);
1384
+ if (version <= 8) {
1385
+ detectedIE = true;
1386
+ }
1387
+ }
1388
+ }
1389
+ return detectedIE;
1390
+ };
1391
+
1392
+ /*
1393
+ * JQueryHttpClient lets a browser take advantage of JQuery's cross-browser magic.
1394
+ * NOTE: when jQuery is available it will export both '$' and 'jQuery' to the global space.
1395
+ * Since we are using closures here we need to alias it for internal use.
1396
+ */
1397
+ var JQueryHttpClient = function (options) {
1398
+ this.options = options || {};
1399
+ if (!jQuery) {
1400
+ var jQuery = window.jQuery;
1401
+ }
1402
+ }
1403
+
1404
+ JQueryHttpClient.prototype.execute = function (obj) {
1405
+ var cb = obj.on;
1406
+ var request = obj;
1407
+
1408
+ obj.type = obj.method;
1409
+ obj.cache = false;
1410
+
1411
+ obj.beforeSend = function (xhr) {
1412
+ var key, results;
1413
+ if (obj.headers) {
1414
+ results = [];
1415
+ var key;
1416
+ for (key in obj.headers) {
1417
+ if (key.toLowerCase() === 'content-type') {
1418
+ results.push(obj.contentType = obj.headers[key]);
1419
+ } else if (key.toLowerCase() === 'accept') {
1420
+ results.push(obj.accepts = obj.headers[key]);
1421
+ } else {
1422
+ results.push(xhr.setRequestHeader(key, obj.headers[key]));
1423
+ }
1424
+ }
1425
+ return results;
1426
+ }
1427
+ };
1428
+
1429
+ obj.data = obj.body;
1430
+ obj.complete = function (response) {
1431
+ var headers = {},
1432
+ headerArray = response.getAllResponseHeaders().split('\n');
1433
+
1434
+ for (var i = 0; i < headerArray.length; i++) {
1435
+ var toSplit = headerArray[i].trim();
1436
+ if (toSplit.length === 0)
1437
+ continue;
1438
+ var separator = toSplit.indexOf(':');
1439
+ if (separator === -1) {
1440
+ // Name but no value in the header
1441
+ headers[toSplit] = null;
1442
+ continue;
1443
+ }
1444
+ var name = toSplit.substring(0, separator).trim(),
1445
+ value = toSplit.substring(separator + 1).trim();
1446
+ headers[name] = value;
1447
+ }
1448
+
1449
+ var out = {
1450
+ url: request.url,
1451
+ method: request.method,
1452
+ status: response.status,
1453
+ data: response.responseText,
1454
+ headers: headers
1455
+ };
1456
+
1457
+ var contentType = (headers['content-type'] || headers['Content-Type'] || null)
1458
+
1459
+ if (contentType != null) {
1460
+ if (contentType.indexOf('application/json') == 0 || contentType.indexOf('+json') > 0) {
1461
+ if (response.responseText && response.responseText !== '')
1462
+ out.obj = JSON.parse(response.responseText);
1463
+ else
1464
+ out.obj = {}
1465
+ }
1466
+ }
1467
+
1468
+ if (response.status >= 200 && response.status < 300)
1469
+ cb.response(out);
1470
+ else if (response.status === 0 || (response.status >= 400 && response.status < 599))
1471
+ cb.error(out);
1472
+ else
1473
+ return cb.response(out);
1474
+ };
1475
+
1476
+ jQuery.support.cors = true;
1477
+ return jQuery.ajax(obj);
1478
+ }
1479
+
1480
+ /*
1481
+ * ShredHttpClient is a light-weight, node or browser HTTP client
1482
+ */
1483
+ var ShredHttpClient = function (options) {
1484
+ this.options = (options || {});
1485
+ this.isInitialized = false;
1486
+
1487
+ if (typeof window !== 'undefined') {
1488
+ this.Shred = require('./shred');
1489
+ this.content = require('./shred/content');
1490
+ }
1491
+ else
1492
+ this.Shred = require('shred');
1493
+ this.shred = new this.Shred();
1494
+ }
1495
+
1496
+ ShredHttpClient.prototype.initShred = function () {
1497
+ this.isInitialized = true;
1498
+ this.registerProcessors(this.shred);
1499
+ }
1500
+
1501
+ ShredHttpClient.prototype.registerProcessors = function () {
1502
+ var identity = function (x) {
1503
+ return x;
1504
+ };
1505
+ var toString = function (x) {
1506
+ return x.toString();
1507
+ };
1508
+
1509
+ if (typeof window !== 'undefined') {
1510
+ this.content.registerProcessor(['application/json; charset=utf-8', 'application/json', 'json'], {
1511
+ parser: identity,
1512
+ stringify: toString
1513
+ });
1514
+ } else {
1515
+ this.Shred.registerProcessor(['application/json; charset=utf-8', 'application/json', 'json'], {
1516
+ parser: identity,
1517
+ stringify: toString
1518
+ });
1519
+ }
1520
+ }
1521
+
1522
+ ShredHttpClient.prototype.execute = function (obj) {
1523
+ if (!this.isInitialized)
1524
+ this.initShred();
1525
+
1526
+ var cb = obj.on, res;
1527
+
1528
+ var transform = function (response) {
1529
+ var out = {
1530
+ headers: response._headers,
1531
+ url: response.request.url,
1532
+ method: response.request.method,
1533
+ status: response.status,
1534
+ data: response.content.data
1535
+ };
1536
+
1537
+ var headers = response._headers.normalized || response._headers;
1538
+ var contentType = (headers['content-type'] || headers['Content-Type'] || null)
1539
+ if (contentType != null) {
1540
+ if (contentType.indexOf('application/json') == 0 || contentType.indexOf('+json') > 0) {
1541
+ if (response.content.data && response.content.data !== '')
1542
+ try {
1543
+ out.obj = JSON.parse(response.content.data);
1544
+ }
1545
+ catch (ex) {
1546
+ // do not set out.obj
1547
+ log('unable to parse JSON content');
1548
+ }
1549
+ else
1550
+ out.obj = {}
1551
+ }
1552
+ }
1553
+ return out;
1554
+ };
1555
+
1556
+ // Transform an error into a usable response-like object
1557
+ var transformError = function (error) {
1558
+ var out = {
1559
+ // Default to a status of 0 - The client will treat this as a generic permissions sort of error
1560
+ status: 0,
1561
+ data: error.message || error
1562
+ };
1563
+
1564
+ if (error.code) {
1565
+ out.obj = error;
1566
+
1567
+ if (error.code === 'ENOTFOUND' || error.code === 'ECONNREFUSED') {
1568
+ // We can tell the client that this should be treated as a missing resource and not as a permissions thing
1569
+ out.status = 404;
1570
+ }
1571
+ }
1572
+
1573
+ return out;
1574
+ };
1575
+
1576
+ var res = {
1577
+ error: function (response) {
1578
+ if (obj)
1579
+ return cb.error(transform(response));
1580
+ },
1581
+ // Catch the Shred error raised when the request errors as it is made (i.e. No Response is coming)
1582
+ request_error: function (err) {
1583
+ if (obj)
1584
+ return cb.error(transformError(err));
1585
+ },
1586
+ response: function (response) {
1587
+ if (obj)
1588
+ return cb.response(transform(response));
1589
+ }
1590
+ };
1591
+ if (obj) {
1592
+ obj.on = res;
1593
+ }
1594
+ return this.shred.request(obj);
1595
+ };
1596
+
1597
+ /**
1598
+ * SwaggerAuthorizations applys the correct authorization to an operation being executed
1599
+ */
1600
+ var SwaggerAuthorizations = function (name, auth) {
1601
+ this.authz = {};
1602
+ if(name && auth) {
1603
+ this.authz[name] = auth;
1604
+ }
1605
+ };
1606
+
1607
+ SwaggerAuthorizations.prototype.add = function (name, auth) {
1608
+ this.authz[name] = auth;
1609
+ return auth;
1610
+ };
1611
+
1612
+ SwaggerAuthorizations.prototype.remove = function (name) {
1613
+ return delete this.authz[name];
1614
+ };
1615
+
1616
+ SwaggerAuthorizations.prototype.apply = function (obj, authorizations) {
1617
+ var status = null;
1618
+ var key, value, result;
1619
+
1620
+ // if the "authorizations" key is undefined, or has an empty array, add all keys
1621
+ if (typeof authorizations === 'undefined' || Object.keys(authorizations).length == 0) {
1622
+ for (key in this.authz) {
1623
+ value = this.authz[key];
1624
+ result = value.apply(obj, authorizations);
1625
+ if (result === true)
1626
+ status = true;
1627
+ }
1628
+ }
1629
+ else {
1630
+ for (name in authorizations) {
1631
+ for (key in this.authz) {
1632
+ if (key == name) {
1633
+ value = this.authz[key];
1634
+ result = value.apply(obj, authorizations);
1635
+ if (result === true)
1636
+ status = true;
1637
+ }
1638
+ }
1639
+ }
1640
+ }
1641
+
1642
+ return status;
1643
+ };
1644
+
1645
+ /**
1646
+ * ApiKeyAuthorization allows a query param or header to be injected
1647
+ */
1648
+ var ApiKeyAuthorization = function (name, value, type, delimiter) {
1649
+ this.name = name;
1650
+ this.value = value;
1651
+ this.type = type;
1652
+ this.delimiter = delimiter;
1653
+ };
1654
+
1655
+ ApiKeyAuthorization.prototype.apply = function (obj) {
1656
+ if (this.type === 'query') {
1657
+ if (obj.url.indexOf('?') > 0)
1658
+ obj.url = obj.url + '&' + this.name + '=' + this.value;
1659
+ else
1660
+ obj.url = obj.url + '?' + this.name + '=' + this.value;
1661
+ return true;
1662
+ } else if (this.type === 'header') {
1663
+ if (typeof obj.headers[this.name] !== 'undefined') {
1664
+ if (typeof this.delimiter !== 'undefined')
1665
+ obj.headers[this.name] = obj.headers[this.name] + this.delimiter + this.value;
1666
+ }
1667
+ else
1668
+ obj.headers[this.name] = this.value;
1669
+ return true;
1670
+ }
1671
+ };
1672
+
1673
+ var CookieAuthorization = function (cookie) {
1674
+ this.cookie = cookie;
1675
+ }
1676
+
1677
+ CookieAuthorization.prototype.apply = function (obj) {
1678
+ obj.cookieJar = obj.cookieJar || CookieJar();
1679
+ obj.cookieJar.setCookie(this.cookie);
1680
+ return true;
1681
+ }
1682
+
1683
+ /**
1684
+ * Password Authorization is a basic auth implementation
1685
+ */
1686
+ var PasswordAuthorization = function (name, username, password) {
1687
+ this.name = name;
1688
+ this.username = username;
1689
+ this.password = password;
1690
+ this._btoa = null;
1691
+ if (typeof window !== 'undefined')
1692
+ this._btoa = btoa;
1693
+ else
1694
+ this._btoa = require('btoa');
1695
+ };
1696
+
1697
+ PasswordAuthorization.prototype.apply = function (obj) {
1698
+ var base64encoder = this._btoa;
1699
+ obj.headers['Authorization'] = 'Basic ' + base64encoder(this.username + ':' + this.password);
1700
+ return true;
1701
+ };
1702
+
1703
+ var e = (typeof window !== 'undefined' ? window : exports);
1704
+
1705
+ var sampleModels = {};
1706
+
1707
+ e.SampleModels = sampleModels;
1708
+ e.SwaggerHttp = SwaggerHttp;
1709
+ e.SwaggerRequest = SwaggerRequest;
1710
+ e.SwaggerAuthorizations = SwaggerAuthorizations;
1711
+ e.authorizations = new SwaggerAuthorizations();
1712
+ e.ApiKeyAuthorization = ApiKeyAuthorization;
1713
+ e.PasswordAuthorization = PasswordAuthorization;
1714
+ e.CookieAuthorization = CookieAuthorization;
1715
+ e.JQueryHttpClient = JQueryHttpClient;
1716
+ e.ShredHttpClient = ShredHttpClient;
1717
+ e.SwaggerOperation = SwaggerOperation;
1718
+ e.SwaggerModel = SwaggerModel;
1719
+ e.SwaggerModelProperty = SwaggerModelProperty;
1720
+ e.SwaggerResource = SwaggerResource;
1721
+ e.SwaggerApi = SwaggerApi;
1722
+
1723
+ e.log = log;
1724
+
1725
+ })();