-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
170 lines (143 loc) · 4.26 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* 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 () {
// FIXME: 40x and 50x response status codes should be accounted as errors.
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;
}
});
}
/**
* Returns the API URL for the specified enpoint.
* @private
* @param {String} endpoint Name of the entity to get URL for.
* @return {String} URL to the specified endpoint.
*/
function _resolveURL(endpoint) {
var url = API_URL + '/' + endpoint + '.json';
return url;
}
/**
* Generates a model getter function based on the name of the model.
* @private
* @param {String} endpoint Name of the model you'd like to return.
* Should be the same as URI for the model.
* @return {Function} getModel A model getter function.
*/
function _getModelGenerator(endpoint) {
return function (fn) {
var url = _resolveURL(endpoint);
_getJSON(url, fn);
};
}
/**
* Returns an array with all the categories available.
* @param {Function} fn Callback
*/
var getCategories = _getModelGenerator('category');
/**
* Returns an array with all the developers available.
* @param {Function} fn Callback
*/
var getDevelopers = _getModelGenerator('developer');
/**
* Returns an array with all the skills available.
* @param {Function} fn Callback
*/
var getSkills = _getModelGenerator('skill');
/**
* Generaters a model getter function that filters by id.
* @param {String} endpoint Name of the model you'd like to return.
* @return {Function} getModelById A model getter function that filters by id.
*/
function _getModelByIdGenerator(endpoint) {
return function (id, fn) {
// TODO: Find a clever way to do this.
endpoint = endpoint + '/id/' + id;
_getModelGenerator(endpoint)(fn);
};
}
/**
* Returns a single category by id.
* @param {Number} id Unique integer that identifies a category.
* @param {Function} fn Callback
*/
var getCategoryById = _getModelByIdGenerator('category');
/**
* Returns a single developer by id.
* @param {Number} id Unique integer that identifies a developer.
* @param {Function} fn Callback
*/
var getDeveloperById = _getModelByIdGenerator('developer');
/**
* Returns a single skill by id.
* @param {Number} id Unique integer that identifies a skill.
* @param {Function} fn Callback
*/
var getSkillById = _getModelByIdGenerator('skill');
// TODO: Document this.
function _getPagedModelGenerator(endpoint) {
return function (pageNumber, fn) {
endpoint = endpoint + '/page/' + pageNumber;
_getModelGenerator(endpoint)(fn);
};
}
// TODO: Document this.
var getCategoriesInPage = _getPagedModelGenerator('category');
// TODO: Document this.
var getDevelopersInPage = _getPagedModelGenerator('developer');
// TODO: Document this.
var getSkillsInPage = _getPagedModelGenerator('skill');
module.exports = {
getCategories: getCategories,
getSkills: getSkills,
getDevelopers: getDevelopers,
getCategoryById: getCategoryById,
getDeveloperById: getDeveloperById,
getSkillById: getSkillById,
getCategoriesInPage: getCategoriesInPage,
getDevelopersInPage: getDevelopersInPage,
getSkillsInPage: getSkillsInPage
};