alexarank

A simple node package to get Alexa traffic rank for a domain or URL
Log | Files | Refs | README | LICENSE

commit 01c0d0b69dc86cd8738a7c441a685470c15bcce8
parent 627c42ba657e36111c40f62b522809e873362099
Author: Frederic Cambus <fred@statdns.com>
Date:   Mon, 24 Oct 2016 17:45:34 +0200

Merge pull request #2 from derhuerst/master

various fixes, request -> isomorphic-fetch
Diffstat:
A.gitignore | 1+
M.travis.yml | 9+++++----
Mindex.js | 2++
Mlib/alexarank.js | 54+++++++++++++++++++++++++++++-------------------------
Mpackage.json | 6++++--
5 files changed, 41 insertions(+), 31 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/.travis.yml b/.travis.yml @@ -1,6 +1,7 @@ language: node_js node_js: - - "0.10" - - "0.12" - - "4.0" - - "4.1" + - '0.10' + - '0.12' + - '4' + - '6' + - 'stable' diff --git a/index.js b/index.js @@ -1 +1,3 @@ +'use strict' + module.exports = require('./lib/alexarank'); diff --git a/lib/alexarank.js b/lib/alexarank.js @@ -1,3 +1,5 @@ +'use strict' + /*****************************************************************************/ /* */ /* alexarank 0.1.1 */ @@ -12,36 +14,38 @@ /* */ /*****************************************************************************/ -var request = require('request'); +require('es6-promise').polyfill(); +var fetch = require('isomorphic-fetch'); var xml2js = require('xml2js'); module.exports = function(url, callback) { - request('http://data.alexa.com/data?cli=10&url=' + url, function(error, response, body) { - if (error) { - callback(new Error('Cannot reach Alexa API')); - } else if (response.statusCode != 200) { - callback(new Error('Cannot fetch Alexa API Data')); - } else { - xml2js.parseString(body, { - normalizeTags: true, - explicitArray: false - }, function(error, result) { - if (error) { - callback(new Error('Cannot parse Alexa API Data')); - } else { - var alexa = {}; - - alexa.url = result.alexa.$.URL; - alexa.idn = result.alexa.$.IDN; + fetch('https://data.alexa.com/data?cli=10&url=' + encodeURIComponent(url)) + .then(function (res) { + return res.text(); + }) + .then(function (body) { + xml2js.parseString(body, { + normalizeTags: true, + explicitArray: false + }, function(error, result) { + if (error) { + callback(new Error('Cannot parse Alexa API Data')); + } else { + var alexa = {}; - if (typeof result.alexa.sd != "undefined") { - alexa.rank = result.alexa.sd.popularity.$.TEXT; - alexa.reach = result.alexa.sd.reach.$.RANK; - } + alexa.url = result.alexa.$.URL; + alexa.idn = result.alexa.$.IDN; - callback(null, alexa); + if (typeof result.alexa.sd != "undefined") { + alexa.rank = result.alexa.sd.popularity.$.TEXT; + alexa.reach = result.alexa.sd.reach.$.RANK; } - }); - } + + callback(null, alexa); + } + }); + }) + .catch(function (err) { + callback(err); }); }; diff --git a/package.json b/package.json @@ -2,6 +2,7 @@ "name": "alexarank", "version": "0.1.1", "description": "A simple node package to get Alexa traffic rank for a domain or URL", + "files": ["index.js", "lib", "bin"], "main": "index.js", "bin": { "alexarank": "./bin/alexarank" @@ -31,8 +32,9 @@ "url": "https://github.com/fcambus/alexarank/issues" }, "dependencies": { - "xml2js": "0.4.15", - "request": "2.67.0" + "es6-promise": "^4.0.5", + "isomorphic-fetch": "^2.2.1", + "xml2js": "^0.4" }, "devDependencies": { "mocha": "2.3.4",