Add CSV download for results

This commit is contained in:
Rowan Manning
2013-09-26 14:10:04 +01:00
parent 8d224e160a
commit 8e1dde9ec0
3 changed files with 44 additions and 1 deletions

42
route/result/download.js Normal file
View File

@@ -0,0 +1,42 @@
'use strict';
var moment = require('moment');
module.exports = route;
// Route definition
function route (app) {
app.express.get('/:id/:rid.csv', function (req, res, next) {
app.webservice.task(req.params.id).get({}, function (err, task) {
if (err) {
return next();
}
app.webservice
.task(req.params.id)
.result(req.params.rid)
.get({full: true}, function (err, result) {
if (err) {
return next();
}
var rows = ['"code", "message", "type"'];
result.results.forEach(function (msg) {
rows.push([
JSON.stringify(msg.code),
JSON.stringify(msg.message),
JSON.stringify(msg.type)
].join(', '));
});
res.attachment([
'pa11y--',
task.url.replace(/^https?:\/\//i, '').replace(/\/$/, '').replace(/[^a-z0-9\.\-\_]+/gi, '-') + '--',
task.standard.toLowerCase() + '--',
moment(result.date).format('YYYY-MM-DD'),
'.csv'
].join(''));
res.send(rows.join('\n'));
});
});
});
}