-
Notifications
You must be signed in to change notification settings - Fork 0
/
runkeeper.js
107 lines (88 loc) · 3.51 KB
/
runkeeper.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
/*
* node-runkeeper - Node.js Client for Runkeeper Health Graph API
*
* runkeeper.js: Defines the HealthGraph class
*
* author: Mark Soper ([email protected])
*/
var request = require('request'),
API = {
"user":
{"media_type": "application/vnd.com.runkeeper.User+json",
"uri": "/user"},
"profile":
{"media_type": "application/vnd.com.runkeeper.Profile+json",
"uri": "/profile"},
"settings":
{"media_type": "application/vnd.com.runkeeper.Settings+json",
"uri": "/settings"},
"fitnessActivityFeed":
{"media_type": "application/vnd.com.runkeeper.FitnessActivityFeed+json",
"uri": "/fitnessActivities"},
"fitnessActivities":
{"media_type": "application/vnd.com.runkeeper.FitnessActivity+json",
"uri": "/fitnessActivities"},
};
var HealthGraph = exports.HealthGraph = function(options) {
this.client_id = options.client_id || null ;
this.client_secret = options.client_secret || null;
this.auth_url = options.auth_url || "https://runkeeper.com/apps/authorize";
this.access_token_url = options.access_token_url || "https://runkeeper.com/apps/token";
this.redirect_uri = options.redirect_uri || null;
this.access_token = options.access_token || null;
this.api_domain = options.api_domain || "api.runkeeper.com";
};
// Refer to Runkeeper OAuth docs: http://developer.runkeeper.com/healthgraph/registration-authorization
// Assumes Step 1 has been done, so you have the authorization_code
// getToken performs Step 2
HealthGraph.prototype.getNewToken = function (authorization_code, callback) {
var request_params = {
grant_type: "authorization_code",
code: authorization_code,
client_id: this.client_id,
client_secret: this.client_secret,
redirect_uri: this.redirect_uri
};
var paramlist = [];
for (pk in request_params) {
paramlist.push(pk + "=" + request_params[pk]);
};
var body_string = paramlist.join("&");
var request_details = {
method: "POST",
headers: {'content-type' : 'application/x-www-form-urlencoded'},
uri: this.access_token_url,
body: body_string
};
console.log(request_details['method'] + " " + request_details['uri'] + "\n body: \n" + body_string);
request(request_details,
function(error, response, body) {
console.log(request_details['method'] + " " + request_details['uri'] + "\n");
console.log("ERROR\n" + error);
console.log("RESPONSE\n" + response);
console.log("BODY\n" + body);
callback(JSON.parse(body)['access_token']);
});
};
for (func_name in API) {
console.log("generating function HealthGraph." + func_name);
HealthGraph.prototype[func_name] = (function(func_name) {
return function(callback) {
console.log("This method is -- " + func_name);
var request_details = {
method: API[func_name]['method'] || 'GET',
headers: {'Accept': API[func_name]['media_type'],
'Authorization' : 'Bearer ' + this.access_token},
uri: "https://" + this.api_domain + API[func_name]['uri']
};
request(request_details,
function(error, response, body) {
console.log(request_details['method'] + " " + request_details['uri'] + "\n");
console.log("ERROR\n" + error);
console.log("RESPONSE\n" + response);
console.log("BODY\n" + body);
callback(body);
});
};
})(func_name);
};