Skip to content

Commit

Permalink
preparation for pagetojson usage. usage of abstract class. reduced di…
Browse files Browse the repository at this point in the history
…rect dependencies to 1.
  • Loading branch information
maugenst committed Jun 15, 2018
1 parent 1453407 commit 2ae791a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
20 changes: 5 additions & 15 deletions lib/tabletojson.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

const AbstractToJson = require('pagetojson').AbstractToJson;
const cheerio = require('cheerio');
const request = require('request');

class tabletojson {
class tabletojson extends AbstractToJson {
/**
* Static conversion of a given HTML Page
* @param html {String} Html page content
Expand Down Expand Up @@ -177,36 +177,26 @@ class tabletojson {
callback = arg2;

// Use a callback (if passed)
const html = await tabletojson.fetchUrl(url, requestOptions);
const html = await AbstractToJson.fetchUrl(url, requestOptions);
return callback.call(this, tabletojson.convert(html, options));
} else if (typeof arg1 === 'function') {
// If only callback passed, invoke with no options
callback = arg1;

// Use a callback (if passed)
const html = await tabletojson.fetchUrl(url);
const html = await AbstractToJson.fetchUrl(url);
return callback.call(this, tabletojson.convert(html, options));
} else {
// If neither argument is callback, return a promise
options = arg1 || {};
// If you need to pass in options for request (proxy)
// add them to arg1.request
requestOptions = options.request || {};
const html = await tabletojson.fetchUrl(url, requestOptions);
const html = await AbstractToJson.fetchUrl(url, requestOptions);
return tabletojson.convert(html, options);
}
}

static fetchUrl(url, options) {
return new Promise((resolve, reject) => {
request(url, options, function(error, response, body) {
if (error) {
reject(error);
}
resolve(body);
});
});
}
}

module.exports = {
Expand Down
22 changes: 15 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"name": "tabletojson",
"description": "Converts HTML tables to JSON objects",
"version": "0.9.4",
"version": "0.9.5",
"main": "./lib/tabletojson.js",
"keywords": [
"table2json",
Expand All @@ -18,11 +18,11 @@
],
"repository": {
"type": "git",
"url": "git://github.com/iaincollins/tabletojson.git"
"url": "git://github.com/maugenst/tabletojson.git"
},
"dependencies": {
"cheerio": "^1.0.0-rc.2",
"request": "^2.85.0"
"pagetojson": "^0.0.2"
},
"engines": {
"node": "*"
Expand All @@ -36,8 +36,8 @@
"mocha": "^5.1.1",
"mocha-junit-reporter": "^1.17.0",
"mocha-lcov-reporter": "^1.3.0",
"nyc": "^11.7.1",
"prettier": "^1.12.1",
"nyc": "^11.9.0",
"prettier": "^1.13.5",
"should": "^13.2.1"
},
"scripts": {
Expand Down

0 comments on commit 2ae791a

Please sign in to comment.