Code coverage report for lib/Settings.js

Statements: 89.66% (52 / 58)      Branches: 75% (9 / 12)      Functions: 80% (16 / 20)      Lines: 89.66% (52 / 58)      Ignored: none     

All files » lib/ » Settings.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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177          1 1     1                                               1   13     13     13   13   13     13           1           1 67 8 8 8 8   8 8             59               1 10 10 10 10 10                 1   8   408     2 2             1                             1 51 51 51 51 51 51       51                   1 1 1   1   51 32           1 1 1                 1 1       1
'use strict';
/*
Settings module
*/
 
var ApiRequest = require('./shared/ApiRequest');
var utils = require('./utils');
 
 
var LIST_READ_ONLY_ATTRS = [
    'bounces_address',
    'created_at',
    'digest_last_sent_at',
    'fqdn_listname',
    'http_etag',
    'join_address',
    'last_post_at',
    'leave_address',
    'list_id',
    'list_name',
    'mail_host',
    'next_digest_number',
    'no_reply_address',
    'owner_address',
    'post_id',
    'posting_address',
    'request_address',
    'scheme',
    'volume',
    'web_host',
    ];
 
// constructor function
function Settings(options,template) {
 
    this._options = options || {};
 
    // override supported methods by BaseRequest
    this._supportedMethods = ['get','patch'];
 
    // pattern to be followed by all the URLs in the module
    this._template = template;
 
    this._info = null;
 
    this._keys = [];
    
    // initially the length is 0
    this.length = 0;
 
}
 
 
// inheriting from ApiRequest
Settings.prototype = new ApiRequest();
 
 
// ensure that the settings has been fetched
// @async 
// @public
Settings.prototype.getInfo = function(callback) {
    if ( !this._info ) {
        var that = this;
        return that.get().then(function(data) {
                        that._info = data;
                        return that.getKeys();
                    }).then(function() {
                        that._setAccessors();
                        return utils.promisify(that,callback);
                    }).catch(function(e) {
                        console.error(e);
                    });
    }
    // if info is not fetched
    else {
        return utils.promisify(this,callback);
    }
}
 
 
// collect the keys of settings _info object in an array
// @async
// @public
Settings.prototype.getKeys = function(callback) {
    var that = this;
    return that.getInfo().then(function() {
                    that._keys = Object.keys(that._info);
                    that.length = that._keys.length;
                    return utils.promisify(that._keys,callback);
                }).catch(function(e) {
                    console.error(e);
                });
}
 
 
// define getters & setters on the settings object
// @private
Settings.prototype._setAccessors = function() {
 
    this._keys.forEach( (function(key) {
 
        Object.defineProperty(this,key,{
 
            get : function() {
                Eif ( key in this._info )
                    return this._info[key];
                else {
                    throw new Error(key + ' not found in the Settings hashmap');
                }
            },
 
            set : function(value) {
                this._info[key] = value;
            },
 
            configurable : true
        });
 
    }).bind(this)
 
    );
}
 
 
// Given a key, returns a value
// @async
// @public
Settings.prototype.getValue = function(key,defaultValue,callback) {
    defaultValue = defaultValue || null;
    var that = this;
    return that.getInfo().then(function (data){
                    var retValue;
                    Eif ( key in that._info ) {
                        retValue = that._info[key];
                    } else {
                        retValue = defaultValue;
                    }
                    return utils.promisify(retValue,callback);
                }).catch(function(e) {
                    console.error(e);
                });
}
 
 
// save the settings -- there should be a getInfo call before calling the save
// @aync
// @public
Settings.prototype.save = function(callback) {
    var data = {};
    var that = this;
 
    that._keys.forEach(function(key) {
 
            if ( LIST_READ_ONLY_ATTRS.indexOf(key) == -1) {
                data[key] = that._info[key];
            }
            
        }
    );
 
    return that.patch(data).then(function() {
                    that._info = null;
                    return utils.promisify(that,callback);
                }).catch(function(e) {
                    console.error(e);
                });
}
 
 
// string representation 
// @public
Settings.prototype.toString = function() {
    return JSON.stringify(this._info);
}
 
 
module.exports = Settings;