-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.js
41 lines (33 loc) · 966 Bytes
/
profile.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
var https = require('https');
// print user stats message
function printMessage(username, badgeCount, points) {
var message = username + ' has ' + badgeCount + ' total badge(s) and ' + points + ' points in JavaScript';
console.log(message);
}
// print error message
function printError(error) {
console.log(error.message);
}
function get(username) {
var data = '';
// grab the data from treehouse
var request = https.get('https://teamtreehouse.com/' + username + '.json', function(response) {
response.on('data', function(chunk) {
data += chunk;
});
response.on('end', function() {
// parse the data
try {
data = JSON.parse(data);
}
catch(error) {
printError(error);
}
// print relevant data
printMessage(username, data.badges.length, data.points.JavaScript);
});
});
// in case of connection error
request.on('error', printError);
}
module.exports.get = get;