Code coverage report for lib/echo.js

Statements: 98.31% (58 / 59)      Branches: 97.5% (39 / 40)      Functions: 100% (10 / 10)      Lines: 98.31% (58 / 59)     

All files » lib/ » echo.js
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          1   1 1 1 1                     1 8 8   8   8 2       1   1 1 1       229 224     229         229   3 2 2     226 672 223 223         229 114 114 114 112 112 58       229     111     111             111 108 52     52 13 39 6 6 6     6     33     56     3         3 6 6 2   6 3   6         1   1  
/*
Copyright (c) 2012, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://yuilibrary.com/license/
*/
"use strict";
 
var parse = require('url').parse;
var path  = require('path');
var fs    = require('fs');
var qs    = require('querystring');
 
/*
 
EchoEcho({
    paths: [] //Array of paths
    all: true //Turn on all paths with 'echo' in the url
})
 
*/
 
var EchoEcho = function(options) {
    this.BASE = {};
    this.options = options || {};
 
    this.scheme = require('./scheme');
 
    if (this.options.paths && !this.options.all) {
        this.paths(this.options.paths);
    }
};
 
EchoEcho.prototype = {
    load: function (obj) {
        var self = this;
        Object.keys(obj).forEach(function (key) {
            self.scheme[key] = obj[key];
        });
    },
    validate: function(req, lax) {
        if (typeof req === 'object') {
            req = req.url;
        }
 
        var url = req.split('?')[0],
            ret = false,
            route,
            base;
 
        if (this.options.all) {
            //Catch all (non-registered) echo urls.
            if (!ret && url.indexOf('/echo/') > -1) {
                route = url.split('/echo/')[1];
                ret = true;
            }
        } else {
            Object.keys(this.BASE).forEach(function (b) {
                if (url.indexOf(b) === 0) {
                    route = url.replace((b + '/echo/'), '');
                    ret = true;
                }
            });
        }
 
        if (route && !lax) {
            ret = false;
            base = route.split('/');
            if (this.scheme[base[0]]) {
                ret = base[0];
                if (base[0] === 'status') {
                    ret = base[1];
                }
            }
        }
        return ret;
    },
    handle: function (req) {
        return !!this.validate(req, true);
    },
    serve: function (req, res) {
        var self = this,
            base = this.validate(req),
            filepath,
            parsed,
            query,
            data;
 
        if (base && base !== true) {
            if (this.scheme[base]) {
                query  = parse(req.url).query,
                parsed = qs.parse(query);
 
                if (parsed.response) {
                    this.scheme[base](req, res, parsed.response);
                } else if (parsed.file) {
                    filepath = path.join(__dirname, parsed.file);
                    fs.readFile(filepath, function (err, data) {
                        Iif (err) {
                            self.scheme.status(404, res);
                        }
                        self.scheme[base](req, res, data);
                    });
                } else {
                    this.scheme[base](req, res, data);
                }
            } else {
                this.scheme.status(base, res);
            }
        } else {
            this.scheme.status(404, res);
        }
 
    },
    paths: function (p) {
        p.forEach(function (dir) {
            var base = dir;
            if (path.basename(dir).indexOf('.') > -1) {
                base = path.dirname(dir);
            }
            if (base.substr(-1) === '/') {
                base = base.substring(0, base.length - 1);
            }
            this.BASE[parse(base).path] = true;
        }, this);
    }
};
 
module.exports = new EchoEcho();
 
module.exports.EchoEcho = EchoEcho;