-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
336 lines (265 loc) · 9.25 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// index.js
'use strict';
// Include module dependencies
var request = require('request'),
_ = require('underscore');
// HELPER FUNCTIONS -- used in class methods
// converts the sign to it's hex code
function convertSign(sign) {
var output;
switch (sign) {
case '<':
output = '%3C';
break;
case '=':
output = '%3D';
break;
case '>':
output = '%3E';
break;
default:
output = sign;
break;
}
return output;
};
// adds parameters that describe how the results are displayed to the end of the url
function appendResultParams(params, url) {
// add the page number to the end of the query, if a page is specified
if (typeof params['page'] !== 'undefined') {
url += '&page=' + params['page'];
}
// add sort if exists
if (typeof params['sort'] !== 'undefined') {
url += '&sort=' + params['sort'];
}
// add order if exists
if (typeof params['order'] !== 'undefined') {
url += '&order=' + params['order'];
}
return url;
};
// function to add the first + sign in a query
// checks to see if this is the first element in the query or not
function addFirstPlus(url) {
// checks to see where the last character in the string is a =
if (url[url.length - 1] === '=') {
return '';
} else {
return '+';
}
};
// CLASS DEFINITION AND METHODS
// Create GithubSearcher class
function GithubSearcher(options) {
if (!(this instanceof GithubSearcher)) {
return new GithubSearcher(options);
}
if (_.isEmpty(options)) {
throw new Error('Please pass in your username and password!');
}
// create static member of this class that holds the authentication information
this.auth = {
username: options['username'],
password: options['password']
};
// create a static variable that holds the options that need to be passed into the GET request
this.options = {
url: '',
headers: {
'User-Agent': this.auth.username,
// add authorization to header so that you can send authenticated headers
'Authorization': 'Basic ' + new Buffer(this.auth.username + ':' + this.auth.password).toString('base64')
}
};
this.endpoints = {
base: 'https://api.github.com',
usersUrl: '/search/users',
reposUrl: '/search/repositories',
issuesUrl: '/search/issues',
codeUrl: '/search/code'
};
}
// method to search for users
GithubSearcher.prototype.searchUsers = function(params, callback) {
// throw error if the callback is not a function
if (typeof callback !== 'function') {
throw new Error('Callback is not a function!');
}
// ensure that some parameters are passed into the method
if (_.isEmpty(params) || params === null) {
throw new Error('Parameters are invalid!');
}
// create the base of the url
var url = this.endpoints.base + this.endpoints.usersUrl + '?q=';
// check type of argument entered
// if a query string, just concatenate the string directly to the url
if (typeof params === 'string') {
url += params;
// if an object, then create the url string by iterating through the object
} else if (typeof params === 'object') {
// add the term part first, since it is added differently
if (typeof params['term'] !== 'undefined') {
url += params['term'];
}
// go through members of the JSON object passed into the method
for (var k in params) {
if (k === 'repos' || k === 'followers') {
// repos and followers allows user to pass in comparison operators >, <, =
url += addFirstPlus(url) + k + ':' + convertSign(params[k][0]) + params[k].substring(1, params[k].length);
} else if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order') {
// term and page are handled separately/ differently
url += addFirstPlus(url) + k + ':' + params[k];
}
}
// add last parts to the url if any of them are defined
url = appendResultParams(params, url);
}
// Print the url we are pinging
console.log('Querying at endpoint: ' + url);
// update this.options to hold the new url
this.options.url = url;
// Use the request module to ping the url
request.get(this.options, function(error, response, body) {
if (!error && response.statusCode === 200) {
// pass our result to the callback
callback(JSON.parse(body));
}
});
// return this instance of GithubSearcher
return this;
};
// method to query repositories
GithubSearcher.prototype.searchRepos = function(params, callback) {
// throw error if the callback is not a function
if (typeof callback !== 'function') {
throw new Error('Callback is not a function!');
}
// ensure that some parameters are passed into the method
if (_.isEmpty(params) || params === null) {
throw new Error('Parameters are invalid!');
}
var url = this.endpoints.base + this.endpoints.reposUrl + '?q=';
if (typeof params === 'string') {
url += params;
} else if (typeof params === 'object') {
// add the term part first, since it is added differently
if (typeof params['term'] !== 'undefined') {
url += params['term'];
}
// go through members of the JSON object passed into the method
for (var k in params) {
if (k === 'forks' || k === 'stars' || k === 'size') {
// forks, stars and size allows user to pass in comparison operators >, <, =
// convert first element in string, and then concatenate the remainder of the string
url += addFirstPlus(url) + k + ':' + convertSign(params[k][0]) + params[k].substring(1, params[k].length);
} else if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order') {
// term, page, sort and order are handled separately/ differently
url += addFirstPlus(url) + k + ':' + params[k];
}
}
// add last parts to the url if any of them are defined
url = appendResultParams(params, url);
}
// Print the url we are pinging
console.log('Querying at endpoint: ' + url);
// updated this.options to hold the new url
this.options.url = url;
// use request module to ping url
request.get(this.options, function(error, response, body) {
if (!error && response.statusCode === 200) {
// pass our result to the callback
callback(JSON.parse(body));
}
});
// return current instance of GithubSearcher
return this;
};
GithubSearcher.prototype.searchCode = function(params, callback) {
// throw error if the callback is not a function
if (typeof callback !== 'function') {
throw new Error('Callback is not a function!');
}
// ensure that some parameters are passed into the method
if (_.isEmpty(params) || params === null) {
throw new Error('Parameters are invalid!');
}
var url = this.endpoints.base + this.endpoints.codeUrl + '?q=';
if (typeof params === 'string') {
url += params;
} else if (typeof params === 'object') {
// add the term part first, since it is added differently
if (typeof params['term'] !== 'undefined') {
url += params['term'];
}
// go through members of the JSON object passed into the method
for (var k in params) {
if (k === 'size') {
// size allows user to pass in comparison operators >, <, =
// convert first element in string, and then concatenate the remainder of the string
url += addFirstPlus(url) + k + ':' + convertSign(params[k][0]) + params[k].substring(1, params[k].length);
} else if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order') {
// term, page, sort and order are handled separately/ differently
url += addFirstPlus(url) + k + ':' + params[k];
}
}
// add last parts to the url if any of them are defined
url = appendResultParams(params, url);
}
// Print the url we are pinging
console.log('Querying at endpoint: ' + url);
// updated this.options to hold the new url
this.options.url = url;
// use request module to ping url
request.get(this.options, function(error, response, body) {
if (!error && response.statusCode === 200) {
// pass our result to the callback
callback(JSON.parse(body));
}
});
// return current instance of GithubSearcher
return this;
};
GithubSearcher.prototype.searchIssues = function(params, callback) {
// throw error if the callback is not a function
if (typeof callback !== 'function') {
throw new Error('Callback is not a function!');
}
// ensure that some parameters are passed into the method
if (_.isEmpty(params) || params === null) {
throw new Error('Parameters are invalid!');
}
var url = this.endpoints.base + this.endpoints.issuesUrl + '?q=';
if (typeof params === 'string') {
url += params;
} else if (typeof params === 'object') {
// add the term part first, since it is added differently
if (typeof params['term'] !== 'undefined') {
url += params['term'];
}
// go through members of the JSON object passed into the method
for (var k in params) {
// just add everything to the query
// there are no cases where <, >, = are used
if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order') {
url += addFirstPlus(url) + k + ':' + params[k];
}
}
// add last parts to the url if any of them are defined
url = appendResultParams(params, url);
}
// Print the url we are pinging
console.log('Querying at endpoint: ' + url);
// updated this.options to hold the new url
this.options.url = url;
// use request module to ping url
request.get(this.options, function(error, response, body) {
if (!error && response.statusCode === 200) {
// pass our result to the callback
callback(JSON.parse(body));
}
});
// return current instance of GithubSearcher
return this;
};
module.exports = GithubSearcher;