developing a hapijs edge by Van Nguyen Daniel Bretoi Wyatt Preul Lloyd Benson

developing a hapijs edge by Van Nguyen Daniel Bretoi Wyatt Preul Lloyd Benson

Author:Van Nguyen, Daniel Bretoi, Wyatt Preul, Lloyd Benson
Language: eng
Format: epub
Publisher: Bleeding Edge Press
Published: 2015-07-02T00:00:00+00:00


NPM Downloads

To start, again, we configure another instantiable model class object NPMDownloads. Following the convention from before, we split the URL into host and parameterized URI functions. We know that all four API functions will basically be the same except for subtle changes to the URI, therefore, we define a shared method getDownloadsBy to be used by the other methods.

The lastYear’s URI is slightly more complicated than the rest, since we have calculated date ranges for today and a year prior to today. But it is not that complicated once you break it down.

var Hoek = require('hoek'); var Wreck = require('wreck'); var internals = {}; internals._defaults = { host: 'https://api.npmjs.org', uris: { 'last-day': function (_module) { return '/downloads/range/last-day/' + _module; }, 'last-week': function (_module) { return '/downloads/range/last-week/' + _module; }, 'last-month': function (_module) { return '/downloads/range/last-month/' + _module; }, 'last-year': function (_module) { var today = new Date(); var from = [today.getFullYear() - 1,today.getMonth + 1, today.getDate].join('-'); var to = [today.getFullYear(),today.getMonth + 1, today.getDate].join('-'); var dateRange = [from, to].join(':'); return '/downloads/range/' + dateRange + "/" + _module; } } }; internals.NpmDownloads = function (options) { this.options = Hoek.applyToDefaults(internals._defaults, options || {}); return this; }; internals.NpmDownloads.prototype.getDownloadsBy = function () {}; internals.NpmDownloads.prototype.lastDay = function () {}; internals.NpmDownloads.prototype.lastWeek = function () {}; internals.NpmDownloads.prototype.lastMonth = function () {}; internals.NpmDownloads.prototype.lastYear = function () {}; module.exports = internals.NpmDownloads;

Next, we fill out the shared function for calling the downloads request. But unlike the previous functions, this one returns a function that we use to generate the other requests. The other functions basically become wrappers for getDownloadsBy.

// ... internals.NpmDownloads.prototype.getDownloadsBy = function (_type, range) { var self = this; return function (moduleName, callback) { var method = "GET"; var uri = self.options.host + self.options.uris[range](moduleName); var options = { redirects: 3, timeout: 30000, rejectUnauthorized: true }; var processResponse = function (err, body) { // Leave room in case response modification required return callback(err, body); }; var readResponse = function (err, res) { if (err) { return callback(err); } Wreck.read(res, {json:true}, processResponse); }; var req = Wreck.request(method, uri, options, readResponse); }; }; // ...

Finally, we factor in getDownloadsBy for the remaining API methods by configuring only the differences between functions:

// ... internals.NpmDownloads.prototype.lastDay = function (moduleName, callback) { return this.getDownloadsBy('point', 'last-day')(moduleName, callback); }; internals.NpmDownloads.prototype.lastWeek = function (moduleName, callback) { return this.getDownloadsBy('point', 'last-week')(moduleName, callback); }; internals.NpmDownloads.prototype.lastMonth = function (moduleName, callback) { return this.getDownloadsBy('point', 'last-month')(moduleName, callback); }; internals.NpmDownloads.prototype.lastYear = function (moduleName, callback) { return this.getDownloadsBy('point', 'last-year')(moduleName, callback); }; // ...



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.