-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
181 lines (149 loc) · 4.67 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
"use strict";
var assert = require("assert"),
util = require("util");
var request = require("crequest");
var STRAVA_URL_PREFIX = process.env.STRAVA_URL_PREFIX || "https://www.strava.com/";
/**
* Swap the order of callback args to increase the data's prominence.
*/
var swapArgs = function(callback) {
return function(err, res, body) {
return callback(err, body, res);
};
};
var Strava = function(options) {
this.id = options.id || process.env.STRAVA_CLIENT_ID;
this.secret = options.secret || process.env.STRAVA_CLIENT_SECRET;
this.token = options.token || process.env.STRAVA_ACCESS_TOKEN;
};
Strava.prototype.toString = function() {
return "[Strava]";
};
Strava.prototype.getAuthorizationUrl = function(options) {
assert.ok(this.id, "A client ID is required.");
return util.format("%s?client_id=%s&redirect_uri=%s&scope=%s&state=%s&approval_prompt=%s" +
"&response_type=%s",
STRAVA_URL_PREFIX + "oauth/authorize",
this.id,
encodeURIComponent(options.redirectUri),
options.scope || "",
options.state || "",
options.approvalPrompt || "force",
"code");
};
Strava.prototype.acquireToken = function(code, callback) {
assert.ok(this.id, "A client ID is required.");
assert.ok(this.secret, "A client secret is required.");
return request.post({
url: STRAVA_URL_PREFIX + "oauth/token",
qs: {
client_id: this.id,
client_secret: this.secret,
code: code
}
}, function(err, rsp, body) {
if (err) {
return callback(err);
}
return callback(null, body.access_token);
});
};
/**
* OAuth 2 support.
*/
Strava.prototype.authHeaders = function() {
assert.ok(this.secret, "An access token is required.");
return {
"Authorization": "access_token " + this.token
};
};
//
// Athletes
//
Strava.prototype.getAthlete = function(athleteId, callback) {
var url = STRAVA_URL_PREFIX + "api/v3/athlete";
switch (arguments.length) {
case 1:
callback = arguments[0];
athleteId = null;
break;
}
if (athleteId) {
url = util.format("%sapi/v3/athletes/%s",
STRAVA_URL_PREFIX, athleteId);
}
return request.get({
url: url,
headers: this.authHeaders()
}, swapArgs(callback));
};
Strava.prototype.updateAthlete = function(options, callback) {
return request.put({
url: STRAVA_URL_PREFIX + "api/v3/athlete",
headers: this.authHeaders(),
form: options
}, swapArgs(callback));
};
Strava.prototype.deleteProfilePicture = function(callback) {
// TODO crequest needs to support delete for this to work
return request.del({
url: STRAVA_URL_PREFIX + "api/v3/athlete/picture",
headers: this.authHeaders()
}, swapArgs(callback));
};
Strava.prototype.getBikes = function(athleteId, callback) {
// TODO extract this as just get() w/ boolean for including headers
return request.get({
url: util.format("%sathletes/%d/gear", STRAVA_URL_PREFIX, athleteId)
}, swapArgs(callback));
};
//
// Activities
//
Strava.prototype.getActivities = function(options, callback) {
callback = Array.prototype.slice.call(arguments).pop();
var url = STRAVA_URL_PREFIX + "api/v3/activities";
if (options.athleteId) {
url = util.format("%sapi/v3/athletes/%s/activities",
STRAVA_URL_PREFIX,
options.athleteId);
delete options.athleteId;
}
return request.get({
url: url,
qs: options,
headers: this.authHeaders()
}, swapArgs(callback));
};
Strava.prototype.getActivity = function(activityId, callback) {
return request.get({
url: STRAVA_URL_PREFIX + "api/v3/activities/" + activityId,
headers: this.authHeaders()
}, swapArgs(callback));
};
Strava.prototype.getActivityStream = function(activityId, types, options, callback) {
var url = util.format("%sapi/v3/activities/%s/streams/%s",
STRAVA_URL_PREFIX, activityId, types);
return request.get({
url: url,
qs: options,
headers: this.authHeaders()
}, swapArgs(callback));
};
Strava.prototype.getSegment = function(segmentId, callback) {
return request.get({
url: STRAVA_URL_PREFIX + "api/v3/segments/" + segmentId,
headers: this.authHeaders()
}, swapArgs(callback));
};
/**
* Get a customized leaderboard for the specified segment. This means top 10
* plus you ± 2 (if you're not in the top 10).
*/
Strava.prototype.getSegmentLeaderboard = function(segmentId, callback) {
return request.get({
url: STRAVA_URL_PREFIX + "api/v3/segments/" + segmentId + "/leaderboard",
headers: this.authHeaders()
}, swapArgs(callback));
};
module.exports = Strava;