-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.js
77 lines (65 loc) · 2.07 KB
/
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
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
// Look at user's badge count & js points
//Use node.js to connect to Treehouse's API and get necessary profile info
var https = require('https');
var http = require('http');
//Print message about user
function printMessage(username, badgeCount, points, topic){
var message;
if(topic){
message = username + ' has ' + + badgeCount + ' total badge(s) and ' + points + ' points in ' + topic;
}else{
message = username + ' has ' + + badgeCount + ' total badge(s) and ' + points + ' total points';
}
console.log(message);
}
//Print error messages
function printError(error){
console.error(error.message);
}
//Connect to API URL
function getProfile(topic,username){
var myHttp = 'https://teamtreehouse.com/' + username + '.json';
var request = https.get(myHttp, function(response){
var body = '';
//Read data
response.on('data', function(chunk){
body += chunk;
});
response.on('end', function(){
if(response.statusCode === 200){
//Parse data
try{
var profile = JSON.parse(body);
// console.log(profile);
//check that the topic exists
if(profile.points[topic]){
printMessage(profile.name, profile.badges.length, profile.points[topic], topic);
}else{
// only print error on the first username passed
once(printError({message: 'There was an error finding the topic ' + topic}));
printMessage(profile.name, profile.badges.length, profile.points.total);
}
}catch(error){
//Parse error
printError(error);
}
}else{
//Status code error (must be for http.STATUS_CODES, not https)
printError({message: 'There was an error getting the profile for ' + username + '. (' + http.STATUS_CODES[response.statusCode] + ')'});
}
});
});
//Connection error
request.on('error', printError);
}
var once = function(func){
var counter = 0;
var newFunc = function(){
if(counter<1){
func();
}
counter ++;
}
return newFunc;
}
module.exports.get = getProfile;