Code coverage report for lib/Member.js

Statements: 90.91% (40 / 44)      Branches: 50% (3 / 6)      Functions: 83.33% (10 / 12)      Lines: 90.91% (40 / 44)      Ignored: none     

All files » lib/ » Member.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          1 1 1 1       1   27     27     27   27     27   27   27   27         1           1   12   12 12 12 12 12 12                         1   12 48   10 10                     12   1   1 1   1         12   1                 1 1   1 1 1             1
'use strict';
/*
    Member module
*/
 
var ApiRequest = require('./shared/ApiRequest.js');
var Preferences = require('./Preferences.js');
var User = require('./User.js');
var utils = require('./utils');
 
 
// constructor for the Member object
function Member(options,url) {
 
    this._options = options;
 
    // self_link to the Member
    this._url = url;
 
    // override the supported methods by the BaseRequest
    this._supportedMethods = ['get','delete'];
 
    this._rootTemplate = this._url.split(this._options.endpoint)[1];
 
    // set the template in the corresponding request methods only
    this._template = '';
 
    this._info = null;
 
    this._preferences = null;
 
    this._keys = ['email','role','list_id','self_link'];
}
 
 
// inheriting from the AppiRequest
Member.prototype  = new ApiRequest();
 
 
// ensure that the data has been GETed
// @async
// @public
Member.prototype.getInfo = function(callback) {
    // set the template string for the URL to request
    this._template = this._rootTemplate;
    // check if the info has been already there or not
    Eif ( !this._info) {
        var that = this;
        return that.get().then(function(data) {
                        that._info = data;
                        that._setAccessors();
                        return utils.promisify(that,callback);
                    }).catch(function(e) {
                        console.error(e);
                    });
    } else {
        return utils.promisify(this,callback);
    }
}
 
 
// set the GET accerssors on the Member object
// private
// @sync
Member.prototype._setAccessors = function() {
    // setting only GET accessors
    this._keys.forEach( (function(key) {
        Object.defineProperty(this,key,{
                get : function() {
                    Eif ( key in this._info )
                        return this._info[key];
                    else 
                        throw new Error('KeyError : key not found in the info hashmap');
                },
 
                configurable : true
            });
        }).bind(this)
    );
 
    // GETTER for the Preferences object corresponding to the Member
    Object.defineProperty(this,'preferences',{
        get : function() {
            Eif ( !this._preferences )
            {   
                var endpoint = this._rootTemplate + '/preferences';
                this._preferences = new Preferences(this._options,endpoint);
            }
            return this._preferences;
        }
    });
 
    // GETTER for the User object corresponding to the Member
    Object.defineProperty(this,'user',{
        get : function() {
            return new User(this._options,this._info.user);
        }
    });
}
 
 
// public
// @async
// Unsubscribe a member from a mailing list
Member.prototype.unsubscribe = function(callback) {
    var that = this;
    //  setting the template
    that._template = that._rootTemplate;
    return that.delete().then(function(data) {
                    return utils.promisify(that,callback);
                }).catch(function(e) {
                    console.error(e);
                });
}
 
 
module.exports = Member;