Code coverage report for lib/Domain.js

Statements: 91.03% (71 / 78)      Branches: 50% (4 / 8)      Functions: 78.57% (22 / 28)      Lines: 91.03% (71 / 78)      Ignored: none     

All files » lib/ » Domain.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          1 1 1 1       1   22   22   22   22   22   22   22       1         1 10 10 10 10 10 10 10 10                           1 10 10 60   11 11                     1 1 1 1 1 1 1 1     1                   1 1 1 1 1 1   1 1 1 15     1 32   1                   1 1 1 1     1   1   1                   1 1 1 1 1                   1 1 1 1 1 1                   1 17       1  
'use strict';
/*
    Domain module
*/
 
var ApiRequest = require('./shared/ApiRequest.js');
var List = require('./List.js');
var User = require('./User.js');
var utils = require('./utils');
 
 
// Domain class
function Domain(options,url) {
 
    this._options = options;
 
    this._url = url;
 
    this._rootTemplate = this._url.split(this._options.endpoint)[1];
 
    this._supportedMethods = ['get','post','delete'];
 
    this._info = null;
 
    this._keys = [];
 
    this._path = {};
}
 
 
Domain.prototype = new ApiRequest();
 
// @async
// @public
// GET the domain info
Domain.prototype.getInfo = function(callback) {
    Eif (!this._info) {
        var that = this;
        that._template = that._rootTemplate;
        return that.get().then(function(resp) {
                        that._info = resp;
                        that._keys = Object.keys(resp);
                        that._setAccessors();
                        return utils.promisify(that,callback);
                    }).catch(function(e) {
                        console.error(e);
                    });
    }
    else {
        return utils.promisify(this,callback);
    }
}
 
 
// set the accessors for the Domain object
// @private
// @sync
Domain.prototype._setAccessors = function() {
    var that = this;
    that._keys.forEach(function(key) {
        Object.defineProperty(that,key, {
            get : function() {
                Eif ( key in that._info )
                    return that._info[key];
            },
            configurable : true
        });
    });
}
 
 
// @async
// @public
// GET the owners( User instances ) corresponding to the domain
Domain.prototype.getOwners = function(callback) {
    var that = this;
    that._template = that._rootTemplate + '/owners';
    return that.get().then(function(resp) {
                    var items = [];
                    Eif ( 'entries' in resp ) {
                        resp.entries.forEach(function(entry) {
                            items.push(new User(that._options,entry.self_link));
                        });
                    }
                    return utils.promisify(items,callback);
                }).catch(function(e) {
                    console.error(e);
                });
}
 
 
// @async
// @public
// Returns the List objects corresponding to this domain
Domain.prototype.getLists = function(callback) {
    var that = this;
    return that.getInfo().then(function() {
                    that._template = '/domains/(:mailHost)/lists';
                    that._path.mailHost = that.mail_host;
                    return that.get();
               }).then(function(resp) {
                    var items = [];
                    Eif ( 'entries' in  resp ) {
                        resp.entries.forEach(function(entry) {
                            items.push(new List(that._options,entry['self_link']));
                        });
                    }
                    items.sort(function(x,y) {
                        return (x._rootTemplate > y._rootTemplate) - (x._rootTemplate < y._rootTemplate);
                    });
                    return utils.promisify(items,callback);
               }).catch(function(e) {
                    console.error(e);
               });
}
 
 
// @async
// @public
// Create a List correspoding to the domain
Domain.prototype.createList = function(listName,callback) {
    var that = this;
    return  that.getInfo().then(function() {
                    var payload = {
                        fqdn_listname : listName + '@' + that.mail_host
                    };
                    that._template = '/lists';
                    // get the raw response object
                    return that.post(payload,null,'identity');
                }).then(function(resp) {
                    return utils.promisify(new List(that._options,resp.headers.location),callback);
                }).catch(function(e) {
                    console.error(e);
                });
}
 
 
// @async
// @public
// Remove all the List owners
Domain.prototype.removeOwners = function(callback) {
    var that = this;
    that._template = that._rootTemplate + '/owners';
    return that.delete().then(function() {
                    return utils.promisify(that,callback);
                }).catch(function(e) {
                    console.error(e);
                });
}
 
 
// @async
// @public
// Add a list owner
Domain.prototype.addOwner = function(owner,callback) {
    var that = this;
    that._template = that._rootTemplate + '/owners';
    var payload = {'owner' : owner};
    return that.post(payload).then(function() {
                    return utils.promisify(that,callback);
                }).catch(function(e) {
                    console.error(e);
                });
}
 
 
// @sync
// @public
// string representation of the domain object
Domain.prototype.toString = function() {
    return '<Domain ' + this._url.split(this._options.endpoint +'/domains/')[1] + '>';
}
 
 
module.exports = Domain;