Skip to content

Commit

Permalink
Logic update and testing improvment (#3)
Browse files Browse the repository at this point in the history
- Changed options for CLI call
- Finished basic and downloader tests
  • Loading branch information
Konstantin committed Aug 14, 2015
1 parent 8561c3a commit 76d2f43
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 51 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
gulpfile.js

25 changes: 11 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@ Just provide parse with link to website and arguments if needed. By default will
```bash
❯ html2json

Usage: html2json [options] [command]
Usage: index [options]

Parse provided website

Commands:
Options:

parse [website] Parse provided website
-h, --help output usage information
-V, --version output the version number
-o --output [filename] Set output file(output.json by default)
-v --verbose Add verbose logging
-q --quiet Do not display output to console

Options:
Examples:

-h, --help output usage information
-V, --version output the version number
-c --console Log result to console
-o --output [filename] Set output file(output.json by default)
-v --verbose Add verbose logging

Examples:

html2json https://google.com
html2json -o google.json https://google.com
html2json https://google.com #parse and display output
html2json -o google.json https://google.com #parse and save output to google.json
```
23 changes: 10 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,35 @@ var downloader = require('./lib/downloader'),
program = require('commander'),
fs = require('fs');

//Basic help
//Help topic
program
.version('0.1.0')
.option('-c --console', 'Log result to console')
.option('-o --output [filename]', 'Set output file(output.json by default)')
.option('-v --verbose', 'Add verbose logging')
.command('parse [website]')
.option('-q --quiet', 'Do not display output to console')
.description('Parse provided website')

//Main function
.action(function(website) {
var response = downloader.downloadPage(website);
var result = JSON.stringify(converter.convertToJSON(response, program.verbose), null, 2);
var filename = "output.json";

if(program.console) {
console.log(result);
return true;
}

if (program.output) {
filename = program.output;
var filename = (program.output) ? program.output : "output.json";
fs.writeFileSync(filename, result);
}

fs.writeFileSync(filename, result);
if(program.quiet === undefined) {
console.log(result);
}
});

// Examples
program.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' html2json https://google.com');
console.log(' html2json -o google.json https://google.com');
console.log(' html2json https://google.com #parse and display output ');
console.log(' html2json -o google.json https://google.com #parse and save output to google.json');
console.log('');
});

Expand Down
6 changes: 5 additions & 1 deletion lib/converter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var _ = require('underscore');

var verbose;

module.exports = {
Expand All @@ -16,6 +15,10 @@ module.exports = {
}
};

//------------------
// Private functions
//------------------

function filterHTML(html) {
if(verbose) {
console.log("\nFiltering HTML\n");
Expand Down Expand Up @@ -78,6 +81,7 @@ function parseTags (tags) {
parsedTag = _.object([pair]);
}

//Appending inner content
if(text) {
parsedTag.text = text;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ module.exports = {

return request.responseText;
}
}
};
62 changes: 58 additions & 4 deletions test/basicTests.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,63 @@
var converter = require('../lib/converter');
var downloader = require('../lib/downloader');
var assert = require('assert');
var html2json = require("../index");
var exec = require('child_process').exec;


describe('html2json command usage', function() {
describe('html2json cli', function () {
describe('html2json', function() {
describe('CLI', function () {
it('should display help topic', function(done) {
exec('./index.js', function(error, stdout, stderr) {
assert(stdout.indexOf('Usage:') > -1, "Don't found 'Usage' word in help topic");
done();
});
});

it('should display output in console', function(done) {
exec('./index.js https://google.com', function(error, stdout, stderr) {
assert(stdout.indexOf('{') > -1, "Output don't appear in console, recieved: " + stdout);
done();
});
});

it('should save response to file and display in console', function(done) {
exec('rm output.json', function(error, stdout, stderr) {
exec('./index.js -o output.json https://google.com', function(error, stdout, stderr) {
exec("ls -lAh | grep output.json | cut -d' ' -f6", function(error, stdout, stderr) {
assert.equal(stdout, '263\n', "Output file don't found or have incorrect size");

//Cleaning if assertion passed
exec('rm output.json', function(error, stdout, stderr) {
done();
});
});
assert(stdout.indexOf('{') > -1, "Output don't appear in console, recieved: " + stdout);
});
});
});

it('should save response to file', function(done) {
exec('rm silent.json', function(error, stdout, stderr) {
exec('./index.js -o silent.json -q https://google.com', function(error, stdout, stderr) {
exec("ls -lAh | grep silent.json | cut -d' ' -f6", function(error, stdout, stderr) {
assert.equal(stdout, '263\n', "Output file don't found or have incorrect size");

//Cleaning if assertion passed
exec('rm silent.json', function(error, stdout, stderr) {
done();
});
});
assert.equal(stdout, '', "Console output isn't empty");
});
});
});
});

describe('integration', function() {
it('should return parsed page', function() {

var response = html2json.convertPage('https://google.com');

assert(response.indexOf('{') > -1, "Output don't appear in console, recieved: " + response);
});
});
});
14 changes: 6 additions & 8 deletions test/converterTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ var converter = require('../lib/converter');
var assert = require('assert');


describe('Converter check', function() {
describe('Filter html', function () {
it('should return object with head element containing text property', function () {
assert.equal(converter.convertToJSON("<head>foo</head>").head.text, 'foo');
});
describe('Converter', function() {
it('should return object with head element containing text property', function () {
assert.equal(JSON.stringify(converter.convertToJSON("<head>foo</head>")), JSON.stringify({head: { text: "foo"}}));
});

it('should return second element of array', function() {
assert.equal(converter.convertToJSON("<a>0</a><a>1</a>").a[1].text, '1');
});
it('should return array of "a" objects', function() {
assert.equal(JSON.stringify(converter.convertToJSON("<a>0</a><a>1</a>")), JSON.stringify({a: [ { text: "0" }, { text: "1" } ] }));
});
});
22 changes: 12 additions & 10 deletions test/downloaderTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ var fs = require('fs');
var assert = require('assert');


describe('Downloader check', function() {
describe('Download pages', function () {
it('should return undefined becouse page don\'t exitst', function () {
assert.equal(downloader.downloadPage("http://www.gfhfdhdfsgdhtwgegdshbfnr.net/"), undefined);
});

it('should return tiny page', function () {
var tinyPage = fs.readFileSync('test/418.html');
assert.equal(downloader.downloadPage("http://httpbin.org/status/418"), tinyPage);
});
describe('Downloader', function() {
it('should return undefined becouse page don\'t exitst', function () {
assert.equal(downloader.downloadPage("http://www.gfhfdhdfsgdhtwgegdshbfnr.net/"), undefined);
});

it('should return tiny page', function () {
var tinyPage = fs.readFileSync('test/418.html');
assert.equal(downloader.downloadPage("http://httpbin.org/status/418"), tinyPage);
});

it('should return static page', function() {
assert(downloader.downloadPage("http://pygreen.neoname.eu/").indexOf('alt="Fork me on GitHub"') > -1, "Don't found one of static's page tags");
});
});

0 comments on commit 76d2f43

Please sign in to comment.