forked from developersdo/ddskills-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (85 loc) · 2.09 KB
/
index.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
99
100
101
102
103
104
105
/**
* URL to the Dominican Developer Skills API.
* @type {String}
*/
var API_URL = 'http://skills-devdom.herokuapp.com/api';
/**
* A wrapper for the `http.get` method.
* @private
* @param {String} url URL to request.
* @param {Function} fn Callback
* First argument is `err`, second is `result`.
*/
function _get(url, fn) {
var http = require('http');
http.get(url, function (res) {
var buff = '';
// TODO: Find a clever way to do this.
res.on('data', function (d) {
buff += d;
});
res.on('error', function (err) {
fn(err, null);
});
res.on('end', function () {
fn(null, buff);
});
});
}
/**
* Same as `_get` but with JSON.
* @private
* @param {String} url URL to request.
* @param {Function} fn Callback
*/
function _getJSON(url, fn) {
_get(url, function (err, result) {
if (err) {
fn(err, null);
return;
}
try {
var json = JSON.parse(result);
fn(null, json);
} catch (e) {
var err = new Error('Unable to parse JSON.', e);
err.inner = e;
throw err;
}
});
}
// TODO:
//
// Refactor getter methods.
// Use closures to abstract the 'changing' parts.
/**
* Returns an array with all the categories available.
* @param {Function} fn Callback
*/
function getCategories(fn) {
var url = API_URL + '/category' + '.json';
_getJSON(url, fn);
}
/**
* Returns an array with all the skills available.
* @param {Function} fn Callback
*/
function getSkills(fn) {
var url = API_URL + '/skill' + '.json';
// _getJSON(url, fn);
var err = new Error('Method not supported by API.');
fn(err, null);
}
/**
* Returns an array with all the developers available.
* @param {Function} fn Callback
*/
function getDevelopers(fn) {
var url = API_URL + '/developer' + '.json';
_getJSON(url, fn);
}
module.exports = {
getCategories: getCategories,
getSkills: getSkills,
getDevelopers: getDevelopers
};