Code coverage report for lib/couple.js

Statements: 63.33% (19 / 30)      Branches: 37.5% (6 / 16)      Functions: 75% (3 / 4)      Lines: 63.33% (19 / 30)      Ignored: none     

All files » lib/ » couple.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    1   1   1   1 3 3     1   1                                 1 1   1     1       1                         1     1 1 1           1 1                  
'use strict';
 
var request = require('request');
 
var JULIET_VER = '1.70'; // API version
 
exports = module.exports = Couple;
 
function Couple(options){
  options = options || {};
  this.authObject = {};
}
 
exports.version = JULIET_VER;
 
Couple.prototype.identify = function(){
  if(this.authObject === {}){
    return new Error('Must authenticate with Couple.');
  }
  if(typeof this.authObject.user === 'undefined' || this.authObject.user.other === 'undefined'){
    return new Error('Not properly authenticated with Couple.');
  }
  return {
    userID: this.authObject.user.userID,
    authToken: this.authObject.authenticationToken,
    otherID: this.authObject.user.other.userID,
    apiHost: this.authObject.base,
    userHash: this.authObject.user.uuid,
    pairHash: this.authObject.user.pairID
  };
};
 
Couple.prototype.authenticate = function(email, password, callback){
  var instance = this;
 
  Iif(typeof email === 'undefined'){
    return callback(new Error('Email required to authenticate with Couple.'));
  }
  Iif(typeof password === 'undefined'){
    return callback(new Error('Password required to authenticate with Couple.'));
  }
 
  request(
    {
      uri: 'https://api-ssl.tenthbit.com/authenticate',
      method: 'POST',
      form: {
        userID: email,
        secretKey: password
      },
      headers: {
        'x-juliet-ver': JULIET_VER
      },
      gzip: true
    }, function (err, res, body) {
      Iif (err) {
        return callback(err);
      }
      var responseObject;
      try {
        responseObject = JSON.parse(String(body));
      }
      catch (err) {
        return callback(new Error('There is a problem with the Couple API: ' + err));
      }
 
      Eif(typeof responseObject.error !== 'undefined'){
        return callback(new Error(responseObject.error), responseObject);
      }
 
      instance.authObject = responseObject;
      return callback(null, responseObject);
 
    }
  );
};