Skip to content

Commit

Permalink
updated grader.js to take in URL
Browse files Browse the repository at this point in the history
  • Loading branch information
prafullam committed Jul 8, 2013
1 parent 499235a commit e6709c6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
16 changes: 15 additions & 1 deletion checks.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
["h1",
".navigation"]
".navigation",
".logo",
".blank",
".about",
".heading",
".subheading",
".pitch",
".video",
".thermometer",
".order",
".social",
".section1",
".section2",
".faq",
".footer"]
49 changes: 43 additions & 6 deletions grader.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ References:
var fs = require('fs');
var program = require('commander');
var cheerio = require('cheerio');
var util = require('util'),
rest = require('restler');
var HTMLFILE_DEFAULT = "index.html";
var CHECKSFILE_DEFAULT = "checks.json";


var assertFileExists = function(infile) {
var instr = infile.toString();
if(!fs.existsSync(instr)) {
Expand All @@ -36,6 +39,12 @@ var assertFileExists = function(infile) {
return instr;
};

var assertURLExists = function(inURL) {
var instr = inURL.toString();
return instr;
};


var cheerioHtmlFile = function(htmlfile) {
return cheerio.load(fs.readFileSync(htmlfile));
};
Expand All @@ -44,13 +53,34 @@ var loadChecks = function(checksfile) {
return JSON.parse(fs.readFileSync(checksfile));
};

var checkHtmlFile = function(htmlfile, checksfile) {
$ = cheerioHtmlFile(htmlfile);
var checkHtmlFile = function(htmlfile, checksfile, myurl) {
var htmlContent = null;
var checks = loadChecks(checksfile).sort();
var out = {};
for(var ii in checks) {
if (checkURL(myurl)){
rest.get(myurl).on('complete', function(result, response) {
if (result instanceof Error) {
console.log('URL access Error: ' + util.format(result.message));
process.exit(1);
}else {
htmlContent = result;
$ = cheerio.load(htmlContent);
for(var ii in checks) {
var present = $(checks[ii]).length > 0;
out[checks[ii]] = present;
}
console.log(JSON.stringify(out,null, 4));
}
});
}
else
{
$ = cheerioHtmlFile(htmlfile);
for(var ii in checks) {
var present = $(checks[ii]).length > 0;
out[checks[ii]] = present;
}
console.log(JSON.stringify(out,null,4));
}
return out;
};
Expand All @@ -61,14 +91,21 @@ var clone = function(fn) {
return fn.bind({});
};

var checkURL = function(value) {
if (value.length > 0)
return (true);
return (false);
};

if(require.main == module) {
program
.option('-c, --checks <check_file>', 'Path to checks.json', clone(assertFileExists), CHECKSFILE_DEFAULT)
.option('-f, --file <html_file>', 'Path to index.html', clone(assertFileExists), HTMLFILE_DEFAULT)
.option('-u, --url <url_value>', 'Url', clone(assertURLExists), "")
.parse(process.argv);
var checkJson = checkHtmlFile(program.file, program.checks);
var outJson = JSON.stringify(checkJson, null, 4);
console.log(outJson);
var checkJson = checkHtmlFile(program.file, program.checks, program.url);
//var outJson = JSON.stringify(checkJson, null, 4);
//console.log(outJson);
} else {
exports.checkHtmlFile = checkHtmlFile;
}

0 comments on commit e6709c6

Please sign in to comment.