all files / arrow-docgen/lib/ apis.js

80.49% Statements 66/82
50% Branches 29/58
100% Functions 10/10
80.49% Lines 66/82
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163                                                                                                                                                                                                 
// jscs:disable jsDoc
 
var _ = require('lodash'),
	codegen = require('./code'),
	request = require('./request'),
	result = require('./result'),
	response = require('./response'),
	apitest = require('./apitest'),
	apicontrols = require('./apicontrols'),
	util = require('arrow-util').content;
 
function generate(object, baseurl, adminurl, context) {
	var pages = [];
 
	Object.keys(object.apis).forEach(function (name) {
		var api = object.apis[name],
			page = {
				url: util.makeAnchor(name),
				title: name
			},
			markdown = [],
			endpoints = api.endpoints.filter(function (endpoint) {
				if (endpoint.documented !== undefined && !endpoint.documented) {
					return false;
				}
				Iif (endpoint.enabled !== undefined && !endpoint.enabled) {
					return false;
				}
				return true;
			});
 
		if (!endpoints.length) {
			return;
		}
 
		markdown.push('# ' + name + '\n');
		markdown.push(api.description || 'The ' + name + ' API');
		markdown.push(apicontrols.generate(adminurl, name) + '\n');
 
		_.sortBy(endpoints, 'uiSort').forEach(function (endpoint) {
			var url = baseurl + endpoint.path.replace(/:(\w+)\??/g, function (value) {
						var orig_value = value;
						value = value.substring(1);
						Iif (/\?$/.test(value)) {
							value = value.substring(0, value.length - 1);
						}
						Iif (endpoint.result && endpoint.result.type === 'json') {
							var example = endpoint.result.example,
								key = _.without(_.keys(example), 'success', 'id')[0],
								exampleObj = example[key];
							if (exampleObj && value in exampleObj) {
								return exampleObj[value];
							}
						}
						return orig_value;
					}),
				templateurl = baseurl + endpoint.path.replace(/:(\w+)\??/g, function (value) {
						Iif (/\?$/.test(value)) {
							value = value.substring(0, value.length - 1);
						}
						return value;
					});
			var opts = {
				url: url,
				method: endpoint.method,
				pathParams: {},
				queryParams: {},
				bodyParams: {},
				templateurl: templateurl
			};
			var before_blocks = [],
				after_blocks = [];
 
			Eif (endpoint.before) {
				Iif (_.isString(endpoint.before)) {
					if (!(endpoint.before in context.blocks)) {
						context.blocks[endpoint.before] = [{api: endpoint.name, name: name}];
					} else if (!(endpoint.name in context.blocks[endpoint.before])) {
						context.blocks[endpoint.before].push({api: endpoint.name, name: name});
					}
					before_blocks.push(endpoint.before);
				} else {
					endpoint.before.forEach(function (before) {
						if (!(before in context.blocks)) {
							context.blocks[before] = [{api: endpoint.name, name: name}];
						} else Eif (!(endpoint.name in context.blocks[before])) {
							context.blocks[before].push({api: endpoint.name, name: name});
						}
						before_blocks.push(before);
					});
				}
			}
			Eif (endpoint.after) {
				Iif (_.isString(endpoint.after)) {
					if (!(endpoint.after in context.blocks)) {
						context.blocks[endpoint.after] = [{api: endpoint.name, name: name}];
					} else if (!(endpoint.name in context.blocks[endpoint.after])) {
						context.blocks[endpoint.after].push({api: endpoint.name, name: name});
					}
					after_blocks.push(endpoint.after);
				} else {
					endpoint.after.forEach(function (after) {
						if (!(after in context.blocks)) {
							context.blocks[after] = [{api: endpoint.name, name: name}];
						} else Eif (!(endpoint.name in context.blocks[after])) {
							context.blocks[after].push({api: endpoint.name, name: name});
						}
						after_blocks.push(after);
					});
				}
			}
 
			markdown.push('## ' + (endpoint.nickname || endpoint.name) + '\n');
 
			// generate code
			markdown.push(codegen.generate(object, api, endpoint, opts));
 
			// reset the example url
			opts.url = templateurl;
 
			markdown.push(endpoint.description);
 
			markdown.push(result.generate(object, api, endpoint, opts));
			markdown.push(request.generate(object, api, endpoint, opts));
 
			// print our before blocks
			Eif (before_blocks.length) {
				markdown.push('### Before Blocks');
				before_blocks.forEach(function (block, i) {
					var mark = i + 1 < before_blocks.length ? '├─' : '└─';
					markdown.push(mark + ' [' + block + '](docs.html?blocks/' + util.makeAnchor(block) + '.html)');
				});
 
				markdown.push();
			}
 
			markdown.push(response.generate(object, api, endpoint, opts));
 
			// print our after blocks
			Eif (after_blocks.length) {
				markdown.push('### After Blocks');
				after_blocks.forEach(function (block, i) {
					var mark = i + 1 < after_blocks.length ? '├─' : '└─';
					markdown.push(mark + ' [' + block + '](docs.html?blocks/' + util.makeAnchor(block) + '.html)');
				});
				markdown.push();
			}
 
			markdown.push(apitest.generate(object, api, endpoint, opts));
 
			markdown.push('');
		});
 
		page.markdown = markdown.join('\n');
 
		pages.push(page);
	});
 
	return pages;
}
 
exports.generate = generate;