Code coverage report for lib/eggtart.js

Statements: 100% (34 / 34)      Branches: 100% (10 / 10)      Functions: 100% (12 / 12)      Lines: 100% (34 / 34)      Ignored: none     

All files » lib/ » eggtart.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 981                               1 8   8 8 8         8       1 8       8 20   20 86 2 1 1     2     2 2 1   1           20 4           1   2     1 1     1 1 1       2                     2 2       1  
var fs = require('fs'),
  p = require('path'),
  req = require('bagofrequest'),
  xml2js = require('xml2js'),
  url = require('url'),
  util = require('util'),
  validator = require('./validator');
 
/**
 * class Eggtart
 *
 * @param {String} username: Delicious account username
 * @param {String} password: Delicious account password
 * @param {Object} opts: optional
 *   - version: API version, default: v1
 */
function Eggtart(username, password, opts) {
  opts = opts || {};
 
  this.version = opts.version || 'v1';
  this.wait = opts.wait || 1000; // mandatory wait to avoid forced throttling
  this.url = util.format('https://%s:%s@api.delicious.com/%s/',
    username,
    password,
    this.version);
 
  this._init();
}
 
// initialises methods and sub methods based on API configuration
Eggtart.prototype._init = function () {  
  var file = p.join(__dirname, '..', 'conf', this.version + '.json'),
    config = JSON.parse(fs.readFileSync(file)),
    self = this;
 
  Object.keys(config).forEach(function (method) {
    var subMethods = {};
 
    Object.keys(config[method]).forEach(function (subMethod) {
      subMethods[subMethod] = function (args, cb) {
        if (!cb) {
          cb = args;
          args = {};
        }
        
        var endpoint = config[method][subMethod].endpoint,
          rules = config[method][subMethod].args;
 
        validator.validate(rules, args, function (err) {
          if (err) {
            cb(err);
          } else {
            self._request(endpoint, args, cb);
          }
        });
      };
    });
 
    Eggtart.prototype[method] = function () {
      return subMethods;
    };
  });
};
 
// send request to Delicious
Eggtart.prototype._request = function (endpoint, params, cb) {
 
  var parser = new xml2js.Parser(),
    self = this;
 
  function _success(result, cb) {
    parser.parseString(result.body, cb);
  }
 
  function _error(result, cb) {
    parser.parseString(result.body, function (err, result) {
      cb(new Error(result.result.$.code));
    });
  }
 
  var opts = {
    queryStrings: params,
    handlers: {
      200: _success,
      401: _error, // access denied
      500: _error, // throttled
      999: _error  // throttled
    }
  };
 
  // mandatory wait to avoid forced throttling
  setTimeout(function () {
    req.request('get', self.url + endpoint, opts, cb);
  }, this.wait);
};
 
module.exports = Eggtart;