-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathathlete.js
125 lines (107 loc) · 3.61 KB
/
athlete.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
const request = require('./request')
const segment = require('./segment')
const activity = require('./activity')
const Cache = require('./cache')
const cache = new Cache()
function defaultMap({lat, lng}) {
return {
lng: lng,
lat: lat
}
}
class Athlete {
constructor(token, config) {
this.token = token
this.config = config
this.mapFn = this.config && this.config.map ? this.config.map : defaultMap
}
athlete() {
if (!this.athletePromise) {
this.athletePromise = request.get(this.token, '/api/v3/athlete')
}
return this.athletePromise
}
activities() {
return cache.get({
key: `activities-${this.config.key}`,
createFn: () => request.get(this.token, '/api/v3/athlete/activities?per_page=100')
.then(activities => activities.filter(a => this.filterActivity(a)))
})
}
activity(activityId) {
return this.athlete().then(athlete => {
return activity.get(this.token, athlete.id, activityId).mapRoute(this.mapFn)
})
}
starred() {
return cache.get({
key: `starred-${this.token}`,
createFn: () => request.get(this.token, '/api/v3/segments/starred?per_page=200')
.then(segments => segments.filter(this.filterStarred))
})
}
segments() {
return this.starred().then(starred =>
this.defaultSegments().then(segments => {
starred.forEach(segment => {
if (!segments.find(s => s.id === segment.id)) {
segments.push(segment)
}
})
return segments.map(s => this.mapSegment(s))
})
)
}
effort(segmentId) {
return this.athlete().then(athlete => {
return segment.get(this.token, athlete.id, segmentId, this.config.startDate).map(this.mapFn)
})
}
route(segmentId) {
return this.athlete().then(athlete => {
return segment.get(this.token, athlete.id, segmentId, this.config.startDate).mapRoute(this.mapFn)
})
}
defaultSegments() {
if (this.config && this.config.segments) {
return Promise.all(this.config.segments.map(id => this.loadSegment(id)))
} else {
return Promise.resolve([])
}
}
loadSegment(id) {
return cache.get({
key: `segment-${id}`,
keepAlive: true,
createFn: () => request.get(this.token, `api/v3/segments/${id}`)
})
}
filterActivity(activity) {
const distance = (p1, p2) => {
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2))
}
if (activity.type === `VirtualRide` && activity.start_latlng) {
const start = this.mapLatLng(activity.start_latlng)
if (distance(start, { x: 0, y: 0 }) < 10000000) {
return true;
}
}
return false;
}
filterStarred(segment) {
return segment.activity_type === 'VirtualRide'
}
mapSegment(segment) {
return {
id: segment.id,
name: segment.name,
distance: segment.distance,
start: this.mapLatLng(segment.start_latlng),
end: this.mapLatLng(segment.end_latlng)
}
}
mapLatLng(latLng) {
return this.mapFn({ lat: latLng[0], lng: latLng[1] })
}
}
module.exports = Athlete