Code coverage report for lib/Addresses.js

Statements: 95.45% (42 / 44)      Branches: 78.57% (11 / 14)      Functions: 90% (9 / 10)      Lines: 95.35% (41 / 43)      Ignored: none     

All files » lib/ » Addresses.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          1 1 1         1   13     13   13   13   13     13   13       1           1 7   7 7 7 7 7 7   7 19   7 7                             1 1 1 1 3                 1 3 2 6 1   2 2 2 2 1   1         1
'use strict';
/*
*   Addresses module
*/
 
var ApiRequest = require('./shared/ApiRequest.js');
var Address = require('./Address.js');
var utils = require('./utils');
 
 
// Addresses class 
// for a given user
function Addresses(options,template,userId) {
 
    this._options = options || {};
 
    // override supported methods by BaseRequest
    this._supportedMethods = ['get'];
 
    this._addresses = [];
 
    this._addressItems = []; // Address objects
 
    this._userId = userId;
 
    // individual methods will use this template
    this._template = template || '/users/(:user)/addresses';
 
    this._path = { user : this._userId };
}
 
 
Addresses.prototype = new ApiRequest();
 
 
// @private
// @async
// find all the associated addresses
Addresses.prototype.getAddresses = function(callback) {
    Eif ( this._addresses.length == 0 )
    {
        var that = this;
        return that.get().then(function(content) {
                        var items = [];
                        Eif ( 'entries' in content ) {
                            that._addresses = content['entries'];
                            that.length = that._addresses.length;
                        }
                        that._addresses.forEach(function(addrEntry) {
                            items.push(new Address(that._options,addrEntry));
                        });
                        that._addressItems = items;
                        return utils.promisify(that._addressItems,callback);
                    }).catch(function(e) {
                        // this should be passed to the node style callback when there is error and callback
                        console.error(e);
                    });
    } 
    else {
        return utils.promisify(this._addressItems,callback);
    }
}
 
 
// iterator over each of the individual Address object
// @async
// @public
Addresses.prototype.each = function(callback) {
    var that = this;
    that.getAddresses().then(function() {
        for ( var index = 0 ; index < that.length ; index++ ) {
            callback(new Address(that._options,that._addresses[index]));
        }
    });
}
 
 
// filter Address object based upon email addresses
// @async
// @public
Addresses.prototype.filterByEmail = function(email,callback) {
    if ( typeof email === 'undefined' )     { throw new Error('email not provided'); }
    var filterCallback = function(add) {
        if ( add.email === email )
            return true;
    } 
    var that = this;
    return that.getAddresses().then(function() {
                   var filteredArray = that._addressItems.filter(filterCallback);
                   if ( filteredArray.length != 0 )
                       return utils.promisify(filteredArray[0],callback);
                   else 
                       return utils.promisify(null,callback);
               });
}
 
 
module.exports = Addresses;