Code coverage report for lib/Preferences.js

Statements: 91.07% (51 / 56)      Branches: 66.67% (8 / 12)      Functions: 84.21% (16 / 19)      Lines: 91.07% (51 / 56)      Ignored: none     

All files » lib/ » Preferences.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          1 1 1     1                   1               1   15     15     15     15   15     15     15           1           1 7 7 7 49                     1 14 7 7 7 7   7 7             7             1 8 8 8 8 8                 1   7   63     2 2           1                             1 1 1 1   9 9           1 1               1 1       1    
'use strict';
/*
Preferences module
*/
 
var ApiRequest = require('./shared/ApiRequest');
var utils = require('./utils');
var lodash = require('lodash');
 
 
var PREFERENCE_FIELDS = [
    'acknowledge_posts',
    'delivery_mode',
    'delivery_status',
    'hide_address',
    'preferred_language',
    'receive_list_copy',
    'receive_own_postings',
    ];
 
var PREF_READ_ONLY_ATTRS = [
    'http_etag',
    'self_link',
    ];
 
// constructor
// @options - The options hash
// @template - template string defining the path to request
function Preferences(options,template) {
 
    this._options = options || {};
 
    // override supported methods by BaseRequest
    this._supportedMethods = ['get','patch'];
 
    // URL template should be flexible
    this._template = template;
 
    // preferences object
    this._preferences = null;
 
    this.delivery_mode = null;
 
    // keys in the preferences object
    this._keys = [];
 
    // initially the length is 0
    this.length = 0;
    
}   
 
 
// inherit from ApiRequest
Preferences.prototype = new ApiRequest();
 
 
// Nice little helping function for setting the preferences from the data
// @private
// @sync
Preferences.prototype._setPreferences = function(data) {
    this._preferences = {};
    lodash.extend(this._preferences,data);
    PREFERENCE_FIELDS.forEach( (function(key) {
            Iif (!key in data) {
                this._preferences[key] = null;
            }
        }).bind(this)
    );
}
 
 
// ensure preferences has been fetched
// @public
// @async function
Preferences.prototype.getPreferences = function(callback) {
    if ( !this._preferences ) {
        var that = this;
        return that.get().then(function(data) {
                        that._setPreferences(data);
                        return that.keys();
                    }).then(function() {
                        that._setAccessors();
                        return utils.promisify(that,callback);
                    }).catch(function(e) {
                        console.error(e);
                    });
    }
    // already this._preferences exists
    else {
        return utils.promisify(this,callback);
    }
}
 
 
// collect the keys of preferences object in an array if _preferences is present
// @public
Preferences.prototype.keys = function(callback) {
    var that = this;
    return that.getPreferences().then(function() {
                    that._keys = Object.keys(that._preferences);
                    that.length = that._keys.length;
                    return utils.promisify(that._keys,callback);
                }).catch(function(e) {
                    console.error(e);
                });
}
 
 
// define getters & setters on the existing preferences object
// @private
Preferences.prototype._setAccessors = function() {
 
    this._keys.forEach( (function(key) {
 
        Object.defineProperty(this,key,{
 
            get : function() {
                Eif ( key in this._preferences )
                    return this._preferences[key];
                else 
                    throw new Error('KeyError : key not found in the preferences hashmap');
            },
 
            set : function(value) {
                this._preferences[key] = value;
            },
 
            configurable : true
        });
 
    }).bind(this)
 
    );
}
 
 
// save the preferences
// @async
// @public
Preferences.prototype.save = function(callback) {
    var data = {};
    var that = this;
    that._keys.forEach(function(key) {
 
            Eif ( !( key in PREF_READ_ONLY_ATTRS ) && that._preferences[key] != null ) {
                data[key] = that._preferences[key];
            }
            
        }
    );
    // return a promise to the Preferences object itself
    return that.patch(data).then(function(data) {
                    return utils.promisify(that,callback)
                }).catch(function(e) {
                    console.error(e);
                });
}
 
 
// string representation 
Preferences.prototype.toString = function() {
    return JSON.stringify(this._preferences);
}
 
 
module.exports = Preferences;