Code coverage report for lib/ListArchivers.js

Statements: 93.48% (43 / 46)      Branches: 100% (6 / 6)      Functions: 83.33% (15 / 18)      Lines: 93.48% (43 / 46)      Ignored: none     

All files » lib/ » ListArchivers.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122            1 1     1   12   12     12   12   12       1           1 8 7 7 7   7 7     1               1 3 1 1   1   1 1   1         2               1 3 3 3                   1 2 2 2                   1 1 1 1   1 3             1 1       1          
'use strict';
 
/*
    Module ListArchivers
*/
 
var ApiRequest = require('./shared/ApiRequest.js');
var utils = require('./utils');
 
 
function ListArchivers(options,listId) {
 
    this._options = options;
 
    this._listId = listId;
 
    // an object will have this common template
    this._template = '/lists/(:listId)/archivers';
 
    this._path = { listId : this._listId };
 
    this._info = null;
}
 
 
ListArchivers.prototype = new ApiRequest();
 
 
// Get the info for the list archivers, returns the object
// @public
// @async
ListArchivers.prototype.getInfo = function(callback) {
    if ( !this._info) {
        var that = this;
        return that.get().then(function(data) {
                    that._info = data;
                    // delete non required keys
                    delete that._info['http_etag'];
                    return utils.promisify(that,callback);
                });
    } else {
        return utils.promisify(this._info,callback);
    }
}
 
 
// public
// @async
// set the key,val pair where key is the name of the archiver
ListArchivers.prototype.setArchiver = function(key,value,callback) {
    if ( key && value ) {
        var that = this;
        return that.getInfo().then(function() {
                        // set the value
                        that._info[key] = value;
                        // make payload
                        var payload = that._info;
                        return that.put(payload);
                    }).then(function() {
                        return utils.promisify(that,callback);
                    }).catch(function(e) {
                        console.error(e);
                    });
    } else {
        throw new Error('Either key or value or both missing');
    }
}
 
 
// public
// @async
// get the val corresponding to a given archiver
ListArchivers.prototype.getArchiver = function(key,callback) {
    var that = this;
    return that.getInfo().then(function() {
                    return utils.promisify(that._info[key],callback);
                }).catch(function(e) {
                    console.error(e);
                });
}
 
 
// public
// @async
// Return the array of all list archivers
ListArchivers.prototype.all = function(callback) {
    var that = this;
    return that.getInfo().then(function() {
                    return utils.promisify(Object.keys(that._info),callback);
                }).catch(function(e) {
                    console.error(e);
                });
}
 
 
// public
// @async
// Iterating iterface to the ListArchivers, callback called with (archiver,value)
ListArchivers.prototype.each = function(callback) {
    var that = this;
    return that.getInfo().then(function() {
                    return that.all();
                }).then(function(archivers) {
                    archivers.forEach(function(archiver) {
                        callback(archiver,that._info[archiver]);
                    });
                });
}
 
 
// String representation of the Archiver
ListArchivers.prototype.toString = function() {
    return '<Archivers on ' + this._listId + '>';
}
 
 
module.exports = ListArchivers;