Skip to content

Commit

Permalink
Extract default results handler into its own module. Modify config ac…
Browse files Browse the repository at this point in the history
…cordingly.
  • Loading branch information
tobie committed Sep 16, 2010
1 parent 8fe0d0d commit 6c245c2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 27 deletions.
60 changes: 54 additions & 6 deletions lib/config.js
Expand Up @@ -72,6 +72,52 @@ function createConfig(options) {
_o.manifest = _o.manifest || path.join(self.outputDir, 'manifest.js');
return _o.manifest;
}
},

resultsHandler: {
get: function() {
_o.resultsHandler = _o.resultsHandler || require('./resultsHandler/console').handleResults;
return _o.resultsHandler;
},

set: function(handler) {

var initValue = handler;

if (typeof handler === 'function') {
_o.resultsHandler = handler
return;
}

// If it's a string, then it must be a reference to a module.
// Require the module's exports.
if (typeof handler === 'string') {
try {
// First, try loading the module from the resultHandler directory.
handler = require('./resultsHandler/' + initValue);
} catch(e) {
try {
// Might be an absolute path.
handler = require(initValue);
} catch (e) {
throw new Error('Cannot find module "' + initValue + '".');
}
}
}

// If it's an object, it must have a handleResults method.
if (typeof handler === 'object') {
if (typeof handler.handleResults === 'function') {
_o.resultsHandler = function() {
return handler.handleResults.apply(handler, arguments);
};
} else {
throw new Error('Cannot find "handleResults" method in "' + initValue + '".');
}
} else {
throw new Error('Invalid result handler "' + initValue + '".')
}
}
}
};

Expand All @@ -81,7 +127,13 @@ function createConfig(options) {
if (_keys.indexOf(option) < 0) {
throw new Error('Unknown option: ' + option);
}
_o[option] = options[option];

var desc = Object.getOwnPropertyDescriptor(self, option);
if (typeof desc.set === 'function') {
self[option] = options[option];
} else {
_o[option] = options[option];
}
}
}

Expand All @@ -104,11 +156,7 @@ function optionsFromEnv(callback) {
function optionsFromFile(f, callback) {
path.exists(f, function(exist) {
if (exist) {
fileutils.parseJSONFile(f, function(err, json) {
var msg = err ? 'Invalid JSON in config file ' : 'Loaded config file from: ';
console.log(msg + f);
callback(null, json);
});
fileutils.parseJSONFile(f, callback);
} else {
callback(null, {});
}
Expand Down
18 changes: 18 additions & 0 deletions lib/resultsHandler/console.js
@@ -0,0 +1,18 @@
exports.handleResults = handleResults;
function handleResults(r) {
var args = [];
args.push(r.isSuccess() ? '✔' : '✘');
args.push(_displayTime(new Date));
args.push('-');
args.push(r.userAgent + ':');
args.push(r);
console.log(args.join(' '));
}

function _pad(n) {
return (n > 9 ? '' : '0') + n;
}

function _displayTime(t) {
return [t.getHours(), t.getMinutes(), t.getSeconds()].map(_pad).join(':');
}
24 changes: 3 additions & 21 deletions lib/server.js
Expand Up @@ -7,10 +7,11 @@ function createServer(config) {
express.bodyDecoder(),
express.staticProvider(config.outputDir)
);
var resultHandler = config.resultHandler || _defaultResultHandler;

var resultsHandler = config.resultsHandler;
server.post('/results', function(req, res) {
res.redirect('back');
resultHandler(_createResults(req));
resultsHandler(_createResults(req));
});

server.listen(config.port);
Expand All @@ -37,23 +38,4 @@ function _createResults(req) {

self.userAgent = uaParser.parse(req.headers['user-agent']);
return self;
}

function _defaultResultHandler(r) {
var args = [];
args.push(r.isSuccess() ? '✔' : '✘');
args.push(_displayTime(new Date));
args.push('-');
args.push(r.userAgent + ':');
args.push(r);
console.log(args.join(' '));
}


function _pad(n) {
return (n > 9 ? '' : '0') + n;
}

function _displayTime(t) {
return [t.getHours(), t.getMinutes(), t.getSeconds()].map(_pad).join(':');
}

0 comments on commit 6c245c2

Please sign in to comment.