lib/eggtart.js

Maintainability

76.43

Lines of code

97

Created with Raphaël 2.1.002550751002015-6-21

2015-6-21
Maintainability: 76.43

Created with Raphaël 2.1.002550751002015-6-21

2015-6-21
Lines of Code: 97

Difficulty

17.64

Estimated Errors

0.70

Function weight

By Complexity

Created with Raphaël 2.1.0Eggtart4

By SLOC

Created with Raphaël 2.1.0<anonymous>._init33
1
var fs = require('fs'),
2
  p = require('path'),
3
  req = require('bagofrequest'),
4
  xml2js = require('xml2js'),
5
  url = require('url'),
6
  util = require('util'),
7
  validator = require('./validator');
8
 
9
/**
10
 * class Eggtart
11
 *
12
 * @param {String} username: Delicious account username
13
 * @param {String} password: Delicious account password
14
 * @param {Object} opts: optional
15
 *   - version: API version, default: v1
16
 */
17
function Eggtart(username, password, opts) {
18
  opts = opts || {};
19
 
20
  this.version = opts.version || 'v1';
21
  this.wait = opts.wait || 1000; // mandatory wait to avoid forced throttling
22
  this.url = util.format('https://%s:%s@api.delicious.com/%s/',
23
    username,
24
    password,
25
    this.version);
26
 
27
  this._init();
28
}
29
 
30
// initialises methods and sub methods based on API configuration
31
Eggtart.prototype._init = function () {  
32
  var file = p.join(__dirname, '..', 'conf', this.version + '.json'),
33
    config = JSON.parse(fs.readFileSync(file)),
34
    self = this;
35
 
36
  Object.keys(config).forEach(function (method) {
37
    var subMethods = {};
38
 
39
    Object.keys(config[method]).forEach(function (subMethod) {
40
      subMethods[subMethod] = function (args, cb) {
41
        if (!cb) {
42
          cb = args;
43
          args = {};
44
        }
45
        
46
        var endpoint = config[method][subMethod].endpoint,
47
          rules = config[method][subMethod].args;
48
 
49
        validator.validate(rules, args, function (err) {
50
          if (err) {
51
            cb(err);
52
          } else {
53
            self._request(endpoint, args, cb);
54
          }
55
        });
56
      };
57
    });
58
 
59
    Eggtart.prototype[method] = function () {
60
      return subMethods;
61
    };
62
  });
63
};
64
 
65
// send request to Delicious
66
Eggtart.prototype._request = function (endpoint, params, cb) {
67
 
68
  var parser = new xml2js.Parser(),
69
    self = this;
70
 
71
  function _success(result, cb) {
72
    parser.parseString(result.body, cb);
73
  }
74
 
75
  function _error(result, cb) {
76
    parser.parseString(result.body, function (err, result) {
77
      cb(new Error(result.result.$.code));
78
    });
79
  }
80
 
81
  var opts = {
82
    queryStrings: params,
83
    handlers: {
84
      200: _success,
85
      401: _error, // access denied
86
      500: _error, // throttled
87
      999: _error  // throttled
88
    }
89
  };
90
 
91
  // mandatory wait to avoid forced throttling
92
  setTimeout(function () {
93
    req.request('get', self.url + endpoint, opts, cb);
94
  }, this.wait);
95
};
96
 
97
module.exports = Eggtart;