-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathactivity.js
More file actions
48 lines (41 loc) · 1.42 KB
/
activity.js
File metadata and controls
48 lines (41 loc) · 1.42 KB
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
const request = require('./request')
const Cache = require('./cache')
const cache = new Cache()
function get(token, athleteId, activityId) {
return cache.get({
key: `athlete-${athleteId}-activity-${activityId}`,
createFn: () => new Activity(token, athleteId, activityId)
})
}
class Activity {
constructor(token, athleteId, activityId) {
this.token = token
this.athleteId = athleteId
this.activityId = activityId
}
route() {
if (!this.routePromise) {
this.routePromise = request.get(this.token, `/api/v3/activities/${this.activityId}/streams/latlng?series_type=time&resolution=high`)
}
return this.routePromise
}
mapRoute(mapFn) {
return this.route().then(stream => {
const latLngData = stream ? stream.find(s => s.type === 'latlng') : null;
const timeData = stream ? stream.find(s => s.type === 'time') : null;
if (latLngData && timeData) {
const firstTime = timeData.data[0]
return latLngData.data.map((latLng, i) => {
let point = mapFn({ lat: latLng[0], lng: latLng[1] })
point.time = timeData.data[i] - firstTime
return point
})
} else {
return null
}
})
}
}
module.exports = {
get
}