all files / lib/ couple.js

100% Statements 23/23
100% Branches 11/11
100% Functions 4/4
100% Lines 23/23
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          10×                                                                          
'use strict';
 
var request = require('request');
 
var JULIET_VER = '1.70'; // API version
 
exports = module.exports = Couple;
 
function Couple(options) {
  this.options = options || {};
}
 
exports.version = JULIET_VER;
 
Couple.prototype.identify = function() {
  if (typeof this.authObject === 'undefined' || typeof this.authObject.user === 'undefined' || typeof this.authObject.user.other === 'undefined') {
    throw new Error('Must authenticated with Couple.');
  }
  return {
    userID: this.authObject.user.userID,
    authToken: this.authObject.authenticationToken,
    otherID: this.authObject.user.other.userID,
    apiHost: this.authObject.base,
    userUuid: this.authObject.user.uuid,
    pairUuid: this.authObject.user.pairID
  };
};
 
Couple.prototype.authenticate = function(email, password, callback) {
  var instance = this;
 
  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) {
    if (err) {
      return callback(err);
    }
    var responseObject;
    try {
      responseObject = JSON.parse(String(body));
    } catch (err) {
      return callback(new Error('The Couple API returned invalid JSON'), {
        invalidJSON: body
      });
    }
 
    if (typeof responseObject.error !== 'undefined') {
      return callback(new Error(responseObject.error), responseObject);
    }
 
    instance.authObject = responseObject;
    return callback(null, responseObject);
 
  });
};