-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.js
More file actions
31 lines (29 loc) · 884 Bytes
/
profile.js
File metadata and controls
31 lines (29 loc) · 884 Bytes
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
// cached variables for use in module
var https = require("https"),
http = require("http"),
print = require("./print"),
perror = print.error,
pmessage = print.message,
body, profile;
// module to export
module.exports.get = function (username) {
https.get("https://teamtreehouse.com/" + username + ".json", function (response) {
body = "";
response.on("data", function (chunk) {
body += chunk;
})
.on("end", function () {
if(response.statusCode === 200) {
try {
profile = JSON.parse(body);
pmessage(username, profile.badges.length, profile.points.JavaScript);
} catch(error) {
perror(error);
}
} else {
perror({message: "There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"});
}
});
})
.on("error", perror);
}