Esempio n. 1
0
PersonService.prototype.findById = function findById (id, callback) {
    mandatory(id).is('string', 'Please provide a proper person id');
    mandatory(callback).is('function', 'Please provide a proper callback function');

    function onQuery (err, persons) {
        if (err) {
            debug('[ERROR] failed to find the person with the id "%s" from all providers', id);
            return callback(new VError(err, 'failed to find the person with the id "%s" from all providers', id));
        }

        if (persons.length > 1) {
            debug('Autsch! Found multiple persons with the id "%s". That should not be possible!', id);
        }

        debug('received person with the id "%s"', id);

        callback(null, persons[0]);
    }

    let query = {id: id};

    debug('request the person with the id "%s"', id);

    this.$query(this.$type, query, onQuery);
};
Esempio n. 2
0
BucketDB.prototype.query = function query (q, callback) {
    var results = [];
    var strom = null;

    mandatory(q).is('object', 'Please provide a proper query object.');
    mandatory(callback).is('function', 'Please provide a proper callback function.');

    function onData (record) {
        results.push(record);
    }

    function onError (err) {
        callback(new VError(err, 'failed to query the storage layer.'));
    }

    function onEnd () {
        callback(null, results);
    }

    strom = this.$bucket.createValueStream();

    // If an query has been defined -> pipe it through the 'jsonquery' module.
    if (Object.keys(q).length > 0) {
        strom = strom.pipe(jsonquery(q));
    }

    strom.on('data', onData)
        .once('error', onError)
        .once('end', onEnd);
};
Esempio n. 3
0
BucketDB.prototype.remove = function remove (id, callback) {
    mandatory(id).is('string', 'Please provide the id of the record which should be removed.');
    mandatory(callback).is('function', 'Please provide a proper callback function.');

    function onDelete (err) {
        if (err) {
            return callback(new VError(err, 'failed to delete the record with the id: %s', id));
        }

        callback(null);
    }

    this.$bucket.del(id, onDelete);
};
Esempio n. 4
0
module.exports = function create (type) {
    var sheet = AVAILABLE_SHEETS[type];

    mandatory(sheet).is('function', 'Please provide a proper sheet name.');

    return sheet();
};
Esempio n. 5
0
/**
 * Grabs the trending repositories of a particular language.
 * 
 * @param {string} language
 * The language for which the trending repos should be fetched.
 * 
 * @param {function} callback
 * Will be invoked when the trending repository
 * data has been fetched. Invoked as `callback(err, repositories)`.
 * 
 */
function trending (language, callback) {

    function onResponse (err, html) {
        var repositories;
        
        if (err) {
            return callback(new VError(err, 'failed to fetch trending repositories (language: %s).', language));
        }

        repositories = scraper.repositories(html);

        callback(null, repositories);
    }

    if ('function' === typeof language) {
        callback = language;

        language = undefined;
    }

    mandatory(callback)
        .is('function', 'Please define a proper callback function.');

    language = (language || 'all').toLowerCase();

    request(endpoint, {l: language}, onResponse);
}
Esempio n. 6
0
exports.instantiate = function instantiate (credentials) {

    mandatory(credentials).is('object', 'Please provide the proper credentials for the FastBill API.');

    return {
        customer: Customer.instantiate(credentials)
    };
};
Esempio n. 7
0
BucketDB.prototype.insert = function insert (record, callback) {
    var self = this;

    mandatory(record).is('object', 'Please provide a proper "' + this.$type + '" record.');
    mandatory(callback).is('function', 'Please provide a proper callback function.');

    function onInsert (err) {
        if (err) {
            return callback(new VError(err, 'failed to insert %s: %s', self.$type, record));
        }

        callback(null, record);
    }

    if (!record.id) {
        record.id = this.$generateId();
    }

    this.$bucket.put(record.id, record, onInsert);
};
Esempio n. 8
0
Entity.prototype.query = function query (q, callback) {
    mandatory(q).is('object', 'Please provide a proper query object');
    mandatory(callback).is('function', 'Please provide a proper callback function');

    let self = this;

    function onQuery (err, results) {
        if (err) {
            debug('failed to execute query "%j" on the "%s" entity', q, self.$type);
            return callback(new VError(err, 'failed to execute query "%j" on the "%s" entity', q, self.$type));
        }

        debug('queried the "%s" bucket successfully (%d result(s))', self.$type, results.length);

        callback(null, results);
    }

    debug('about to query the "%s" bucket', this.$type);

    this.$bucket.query(q, onQuery);
};
Esempio n. 9
0
BucketDB.prototype.update = function update (record, callback) {
    var self = this;

    mandatory(record).is('object', 'Please provide a proper data record which should be updated.');
    mandatory(callback).is('function', 'Please provide a proper callback function.');

    if (!record.id) {
        return process.nextTick(function onTick () {
            var err = new VError('failed to update the record. The record does not have an id and therefore does not exist.');
            err.type = 'NotFoundError';

            callback(err);
        });
    }

    function onFind (err, found) {
        if (err) {
            return callback(new VError(err, 'failed to search for the record before updating it.'));
        }

        if (found.length === 0) {
            err = new VError('unable to update a non-existing record.');
            err.type = 'NotFoundError';

            return callback(err);
        }

        self.$bucket.put(record.id, record, onUpdate);
    }

    function onUpdate (err) {
        if (err) {
            return callback(new VError(err, 'failed to update the record.'));
        }

        callback(null, record);
    }

    this.query({id: record.id}, onFind);
};
Esempio n. 10
0
UsersResource.prototype.findById = function findById (id, callback) {
    let action = '/users/:id';

    mandatory(id).is('number', 'Please provide a proper users id.');
    mandatory(callback).is('function', 'Please provide a proper callback function.');

    let uri = this.$prepareURI(action.replace(':id', id));

    function onResponse (err, user) {
        if (err) {
            debug('[ERROR] failed to receive the torial user with the id "%d"', id);
            return callback(new VError(err, 'failed to receive the torial user with the id "%d"', id));
        }

        debug('received torial user with the id "%d"', id);

        callback(null, user);
    }

    debug('request a particular torial user with the id "%d"', id);

    duperagent.get(uri, onResponse);
};
Esempio n. 11
0
exports.languages = function languages (html) {
    var langs = [];
    var $;

    mandatory(html).is('string');

    $ = cheerio.load(html, {
        normalizeWhitespace: true
    });

    $('.select-menu-item-text.js-select-button-text.js-navigation-open').each(function onEach (index, elem) {
        elem = $(elem);
        langs.push(elem.text().trim());
    });

    return langs;
};
Esempio n. 12
0
CahootsSheet.prototype.findAll = function findAll (callback) {
    mandatory(callback).is('function', 'Please provide a proper callback function');

    function onFetch (err, rows) {
        if (err) {
            return callback(new VError(err, 'failed to fetch all cahoots from the spreadsheet'));
        }

        debug('received all cahoots from sheet (found %d cahoots(s))', rows.length);

        callback(null, rows);
    }

    debug('request all cahoots from sheet');

    this.$fetch(this.$name, onFetch);
};
Esempio n. 13
0
trending.languages = function languages (callback) {

    function onResponse (err, html) {
        var languages;

        if (err) {
            return callback(new VError(err, 'failed to fetch all available languages.'));
        }

        languages = scraper.languages(html);

        callback(null, languages);
    }

    mandatory(callback)
        .is('function', 'Please define a proper callback function.');

    request(endpoint, onResponse);
};
Esempio n. 14
0
PersonService.prototype.findAll = function findAll (callback) {
    mandatory(callback).is('function', 'Please provide a proper callback function');

    function onQuery (err, persons) {
        if (err) {
            debug('[ERROR] failed to find all persons via all providers');

            return callback(new VError(err, 'failed to find all persons via all providers'));
        }

        debug('received all persons (found %d person(s))', persons.length);

        callback(null, persons);
    }

    let query = {};

    debug('request all persons');

    this.$query(this.$type, query, onQuery);
};
Esempio n. 15
0
UsersResource.prototype.list = function list (callback) {
    let action = '/users';

    mandatory(callback).is('function', 'Please provide a proper callback function.');

    let uri = this.$prepareURI(action);

    function onResponse (err, users) {
        if (err) {
            debug('[ERROR] failed to receive all torial users');
            return callback(new VError(err, 'failed to grab all torial users.'));
        }

        debug('received list with all torial users');
        callback(null, users);
    }

    debug('request the list with all torial users');

    duperagent.get(uri, onResponse);
};
Esempio n. 16
0
module.exports = function instantiate (dbPath) {

    mandatory(dbPath).is('string', 'Please provide a proper database path');

    storage.setDatabasePath(dbPath);

    function database (bucketName) {
        var bucket = storage(bucketName);
        var bucketdb = new BucketDB(bucket);

        return {
            insert: bucketdb.insert.bind(bucketdb),
            update: bucketdb.update.bind(bucketdb),
            remove: bucketdb.remove.bind(bucketdb),
            query: bucketdb.query.bind(bucketdb)
        };
    }

    database.destroy = storage.destroy.bind(storage);

    return database;
};
Esempio n. 17
0
exports.repositories = function repositories (html) {
    var repos = [];
    var $;

    mandatory(html).is('string');

    $ = cheerio.load(html, {
        normalizeWhitespace: true
    });

    $('li.repo-list-item').each(function onEach() {

        var repository = {};
        var $elem = $(this);
        var language = $elem.find('.repo-list-meta').text().split('•');
        repository.title = $elem.find('.repo-list-name a').attr('href');

        // Remove the owner name and all slashes
        repository.title = repository.title.split('/')[2];

        repository.owner = $elem.find('span.prefix').text();
        repository.description = $elem.find('p.repo-list-description').text().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
        repository.url = 'https://github.com/' + repository.owner + '/' + repository.title;
        repository.language = language[0].trim();

        repository.stars = {
            count: parseInt(language[1].trim()),
            text: language[1].trim()
        };

        repos.push(repository);

    });

    return repos;
};