Code coverage report for lib/Address.js

Statements: 88.89% (48 / 54)      Branches: 50% (4 / 8)      Functions: 78.57% (11 / 14)      Lines: 88.89% (48 / 54)      Ignored: none     

All files » lib/ » Address.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            1 1 1       1   36   36   36   36     36   36   36   36   36         1         1   6 6 6 6 6 6 6 6                     1 6 6   36     4 4                         6   1 1                             1 1 1 1 1 1 1                   1 1 1 1 1 1 1               1         1
'use strict';
 
/*
*   Address module
*/
 
var ApiRequest = require('./shared/ApiRequest.js');
var Preferences = require('./Preferences.js');
var utils = require('./utils');
 
 
// Address class for a particular email address
function Address(options,address) {
 
    this._options = options || {};
 
    this._supportedMethods = ['get','post'];
 
    this.email = address.email;
 
    this._url = address.self_link;
    
    // each individual method will use it own template seperately
    this._template = '';
 
    this._path = {};
 
    this._preferences = null;
 
    this._info = null;
 
    this._keys = [];
 
}
 
 
Address.prototype  = new ApiRequest();
 
 
// @async 
// @public
Address.prototype.getInfo = function(callback) {
    // setting the template
    this._template = this._url.split(this._options.endpoint)[1];
    Eif ( !this._info ) {
        var that = this;
        return that.get().then(function(data) {
                        that._info = data;
                        that._keys = Object.keys(that._info);
                        that._setAccessors();
                        return utils.promisify(that,callback);
                    });
    }
    else {
        return utils.promisify(this,callback);
    }
}
 
 
// define getters on the existing address object
// @private
Address.prototype._setAccessors = function() {
    Eif ( this._keys.length != 0 ) {
        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 address hashmap');
                },
 
                configurable : true
            });
 
        }).bind(this)
 
        );
 
        // getter for Preferences object associated to the address
        Object.defineProperty(this,'preferences',{
            get : function() {
                var endpoint = '/addresses/' + this.email + '/preferences';
                return new Preferences(this._options,endpoint);
            },
            configurable : true
        });
    } 
    // if data is not fetched at all
    else {
        throw new Error('getInfo() should be called first');
    }
}
 
 
// Verify the address
// @async
// @public
Address.prototype.verify = function(callback) {
    var that = this;
    that._template = '/addresses/(:email)/verify';
    that._path.email = that.email;
    return that.post().then(function(data) {
                    that._info = null;
                    return that.getInfo(callback);
                }).catch(function(e) {
                    console.error(e);
                });
}
 
 
// unverify the address
// @async
// @public
Address.prototype.unverify = function(callback) {
    var that = this;
    that._template = '/addresses/(:email)/unverify';
    that._path.email = that.email;
    return that.post().then(function(data) {
                    that._info = null;
                    return that.getInfo(callback);
                }).catch(function(e) {
                    console.error(e);
                });
}
 
 
// string representation of the address object
Address.prototype.toString = function() {
    return this.email;
}
 
 
module.exports = Address;