# all files / lib/ util.js

97.92% Statements 94/96
80.49% Branches 33/41
100% Functions 19/19
97.89% Lines 93/95

    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
    164
    165
    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×
    4×

    1×
    1×

    1×
    2×
    2×

    1×
    2×
    2×

    1×
    2×
    2×

    1×
    1×

    1×
    1×

    1×
    1×

    1×
    1×

    1×
    1×

    1×
    3×
    3×
    3×

    1×

    1×

    1×
    3×
    1×

    2×
    1×

    1×

    1×

    1×
    6×

    1×
    1×

    1×
    1×

    1×
    4×

    1×

    3×

    3×
    8×
    3×
    3×
    1×
    1×

    2×

    3×

    3×
    3×
    3×
    3×
    3×
    3×
    3×
    3×
    3×
    3×
    3×
    3×
    3×

    3×
    3×
    1×
    3×
    1×

    2×
    2×
    2×
    2×

    3×
    3×
    3×

     // jscs:disable jsDoc
    var crypto = require('crypto'),
    	fs = require('fs'),
    	path = require('path'),
    	_ = require('lodash');

    /*
     Public API.
     */
    exports.md5 = md5;
    exports.redirect = redirect;
    exports.html = html;
    exports.css = css;
    exports.json = json;
    exports.nocontent = nocontent;
    exports.javascript = javascript;
    exports.gravatar = gravatar;
    exports.preferences = preferences;
    exports.etag = etag;
    exports.generateAPI = generateAPI;
    exports.generateAPIName = generateAPIName;
    exports.generateModelName = generateModelName;

    /*
     Implementation.
     */
    function md5(value) {
    	return crypto.createHash('md5').update(value).digest('hex');
    }

    function redirect(res, url) {
    	res.redirect(url);
    }

    function html(res, content) {
    	res.set('Content-Type', 'text/html');
    	res.send(content || '<html></html>');
    }

    function javascript(res, content) {
    	res.set('Content-Type', 'text/javascript');
    	res.send(content || '/* */');
    }

    function css(res, content) {
    	res.set('Content-Type', 'text/css');
    	res.send(content || '/* */');
    }

    function json(res, content) {
    	res.json(content);
    }

    function nocontent(res) {
    	res.sendStatus(204);
    }

    function gravatar(email) {
    	return 'http://www.gravatar.com/avatar/' + md5(email) + '.png';
    }

    function home() {
    	return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
    }

    function preferences() {
    	var _home = home(),
    		cfg = {};

    	['.appcelerator/appc-cli.json', '.acs', '.titanium/config.json'].forEach(function (name) {
    		var p = path.join(_home, name);
    		Eif (fs.existsSync(p)) {
    			_.defaults(cfg, JSON.parse(fs.readFileSync(p).toString()));
    		}
    	});

    	Iif (!cfg.username) {
    		cfg.username = process.env.APPC_SESSION_USERNAME || cfg.user && cfg.user.email;
    	}

    	return cfg;
    }

    function etag(stats) {
    	if (stats === undefined) {
    		throw new Error('stats is a required argument to etag!');
    	}
    	if (typeof stats === 'string') {
    		return md5(stats);
    	}
    	return md5(String(stats.ino) + String(stats.mtime));
    }

    const VERBMAP = {
    	'GET': 'read',
    	'PUT': 'update',
    	'POST': 'create',
    	'DELETE': 'delete'
    };

    function safeName(name) {
    	return name.replace(/^\//, '').split('/')[0].replace(/\s+/g, '-');
    }

    function generateAPIName(def) {
    	Iif (def.filename && fs.existsSync(def.filename)) {
    		return def.filename;
    	}
    	var name = safeName(def.group) + '-' + safeName(def.path) + '-' + VERBMAP[def.method] + '.js';
    	return path.join(process.cwd(), 'apis', name);
    }

    function generateModelName(config, name) {
    	return path.join(config && config.dir || process.cwd(), 'models', safeName(name) + '.js');
    }

    function generateAPI(def) {

    	var path = (def.path || '').substring(1).split('/'),
    		parameters = {};

    	path.forEach(function (token) {
    		if (token.charAt(0) === ':') {
    			var name = token.substring(1);
    			if (/\?$/.test(name)) {
    				name = name.substring(0, name.length - 1);
    				parameters[name] = {description: name + ' parameter', optional: true};
    			} else {
    				parameters[name] = {description: name + ' parameter'};
    			}
    		}
    	});

    	var count = Object.keys(parameters).length;

    	var code = 'var Arrow = require(\'arrow\');\n\n';
    	code += 'var API = Arrow.API.extend({\n';
    	code += '\tgroup: \'' + def.group + '\',\n';
    	code += '\tpath: \'' + def.path + '\',\n';
    	code += '\tmethod: \'' + def.method + '\',\n';
    	code += '\tdescription: \'' + def.description + '\',\n';
    	code += '\tmodel: ' + JSON.stringify(def.model) + ',\n';
    	code += '\tbefore: ' + JSON.stringify(def.before || []) + ',\n';
    	code += '\tafter: ' + JSON.stringify(def.after || []) + ',\n';
    	code += '\tparameters: {\n';
    	Object.keys(parameters).forEach(function (key, index) {
    		code += '\t\t\'' + key + '\': ' + JSON.stringify(parameters[key]);
    		code += ((index + 1 < count) ? ',' : '') + '\n';
    	});
    	code += '\t},\n';
    	if (def.action) {
    		code += '\t' + def.action.name + ': function ' + def.action.name + '(' + def.action.args.join(', ') + ') {\n';
    		code += def.action.body.split(/\n/).map(function (line) {return '\t\t' + line;}).join('\n') + '\n';
    		code += '\t}\n';
    	} else {
    		code += '\taction: function action (req, resp, next) {\n';
    		code += '\t\t//TODO implement\n';
    		code += '\t\tresp.nocontent(next);\n';
    		code += '\t}\n';
    	}
    	code += '});\n\n';
    	code += 'module.exports = API;';
    	return code;
    }