wahoo 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +23 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +168 -0
- data/Rakefile +8 -0
- data/docs/conversions.md +72 -0
- data/docs/getting_started.md +61 -0
- data/docs/workouts.md +54 -0
- data/examples/rails_webhook_controller.rb +22 -0
- data/examples/sync_workouts.rb +21 -0
- data/exe/wahoo-oauth-token +66 -0
- data/lib/wahoo/client.rb +26 -0
- data/lib/wahoo/configuration.rb +16 -0
- data/lib/wahoo/connection.rb +107 -0
- data/lib/wahoo/conversions.rb +64 -0
- data/lib/wahoo/data/workout_types.rb +44 -0
- data/lib/wahoo/error.rb +54 -0
- data/lib/wahoo/models/base.rb +53 -0
- data/lib/wahoo/models/plan.rb +24 -0
- data/lib/wahoo/models/power_zone.rb +27 -0
- data/lib/wahoo/models/route.rb +33 -0
- data/lib/wahoo/models/user.rb +26 -0
- data/lib/wahoo/models/workout.rb +74 -0
- data/lib/wahoo/models/workout_file_upload.rb +26 -0
- data/lib/wahoo/oauth.rb +104 -0
- data/lib/wahoo/resources/base.rb +55 -0
- data/lib/wahoo/resources/permissions.rb +11 -0
- data/lib/wahoo/resources/plans.rb +37 -0
- data/lib/wahoo/resources/power_zones.rb +28 -0
- data/lib/wahoo/resources/routes.rb +43 -0
- data/lib/wahoo/resources/uploads.rb +25 -0
- data/lib/wahoo/resources/user.rb +16 -0
- data/lib/wahoo/resources/workouts.rb +49 -0
- data/lib/wahoo/version.rb +5 -0
- data/lib/wahoo/webhooks/event.rb +26 -0
- data/lib/wahoo/webhooks/handler.rb +36 -0
- data/lib/wahoo.rb +50 -0
- data/sig/wahoo.rbs +4 -0
- metadata +156 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: af29cbaad7a2c7a4d7c5b273ef61c80a84c19e8653b13f238591a9cc2acdfde1
|
|
4
|
+
data.tar.gz: 70c00a7c7f298402fa33f6638e91f0a775387fdcca37702484693861a67efac5
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3383a502e6d64fc57beef31909ef64e040849839078a40c79b23386f345410f475a402bef42dfdb14aa219f70b9b8089c1148d5850a77438ac01b59817ae18ed
|
|
7
|
+
data.tar.gz: 63354ca207758aa56ff33bf6127b738bcace7be41a3e843e226f9a520e1e96673eaca237f83239a534617c0e266f41c4d7fbe49634ea33ba165ccd06062470ab
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.1.0] - 2026-11-30
|
|
4
|
+
|
|
5
|
+
Initial release — a Ruby client for the Wahoo Cloud API.
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
- OAuth 2.0 authorization flow (standard + PKCE); token refresh; deauthorize.
|
|
9
|
+
- Resources with full CRUD: workouts, routes, plans, power zones; user get/update.
|
|
10
|
+
- Workout file uploads (async, status polling).
|
|
11
|
+
- Read workout summaries and plans-for-workout.
|
|
12
|
+
- Webhooks: parse and verify incoming `workout_summary` events.
|
|
13
|
+
- First-class models with unit conversion helpers (km, miles, feet, pace,
|
|
14
|
+
human-readable durations) that handle Wahoo's string decimals.
|
|
15
|
+
- Automatic pagination with `limit` and `per_page`.
|
|
16
|
+
- Automatic retries on 429 and 5xx with configurable backoff.
|
|
17
|
+
- Three-window rate limit parsing (day, hour, 5-min).
|
|
18
|
+
- Workout type / location / family constants.
|
|
19
|
+
- CLI (`wahoo-oauth-token`) to obtain tokens from the terminal.
|
|
20
|
+
|
|
21
|
+
### Deprecated
|
|
22
|
+
- `Workouts#create_summary` — Wahoo deprecated this endpoint (2024-05-01).
|
|
23
|
+
Use `Uploads#create` (workout file uploads) instead.
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
"wahoo" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
|
|
4
|
+
|
|
5
|
+
* Participants will be tolerant of opposing views.
|
|
6
|
+
* Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
|
|
7
|
+
* When interpreting the words and actions of others, participants should always assume good intentions.
|
|
8
|
+
* Behaviour which can be reasonably considered harassment will not be tolerated.
|
|
9
|
+
|
|
10
|
+
If you have any concerns about behaviour within this project, please contact us at ["stefanachesilviu1993@gmail.com"](mailto:"stefanachesilviu1993@gmail.com").
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 swswiss
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 swswiss
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# wahoo
|
|
2
|
+
|
|
3
|
+
[](https://github.com/swswiss/wahoo/actions/workflows/main.yml)
|
|
4
|
+
[](https://rubygems.org/gems/wahoo)
|
|
5
|
+
|
|
6
|
+
A Ruby client for the [Wahoo Cloud API](https://cloud-api.wahooligan.com/).
|
|
7
|
+
OAuth 2.0 (standard + PKCE), workouts, routes, plans, power zones, the user,
|
|
8
|
+
file uploads and webhooks — with first-class models, automatic pagination,
|
|
9
|
+
unit conversions, and automatic retries.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
gem "wahoo"
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Then `bundle install`, or `gem install wahoo`.
|
|
18
|
+
|
|
19
|
+
## Configuration
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
Wahoo.configure do |c|
|
|
23
|
+
c.client_id = ENV["WAHOO_CLIENT_ID"]
|
|
24
|
+
c.client_secret = ENV["WAHOO_CLIENT_SECRET"]
|
|
25
|
+
c.redirect_uri = "https://yourapp.com/wahoo/callback"
|
|
26
|
+
end
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Register your redirect URI in the [Wahoo developer portal](https://developers.wahooligan.com)
|
|
30
|
+
— it must match `redirect_uri` exactly.
|
|
31
|
+
|
|
32
|
+
## Authentication
|
|
33
|
+
|
|
34
|
+
### Standard OAuth 2.0
|
|
35
|
+
|
|
36
|
+
```ruby
|
|
37
|
+
url = Wahoo::OAuth.authorize_url(scope: "user_read workouts_read")
|
|
38
|
+
tokens = Wahoo::OAuth.exchange_code(params[:code]) # in your callback
|
|
39
|
+
tokens = Wahoo::OAuth.refresh(tokens.refresh_token) if tokens.expired?
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### PKCE (public apps — set "Confidential? = No" in the portal)
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
verifier = Wahoo::OAuth.generate_code_verifier
|
|
46
|
+
challenge = Wahoo::OAuth.code_challenge_for(verifier)
|
|
47
|
+
|
|
48
|
+
url = Wahoo::OAuth.authorize_url(scope: "user_read workouts_read",
|
|
49
|
+
code_challenge: challenge)
|
|
50
|
+
# store `verifier`, then in your callback:
|
|
51
|
+
tokens = Wahoo::OAuth.exchange_code(params[:code], code_verifier: verifier)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Usage
|
|
55
|
+
|
|
56
|
+
```ruby
|
|
57
|
+
client = Wahoo::Client.new(access_token: tokens.access_token)
|
|
58
|
+
|
|
59
|
+
# User
|
|
60
|
+
client.user.get # => Wahoo::Models::User
|
|
61
|
+
client.user.update(weight: 72)
|
|
62
|
+
|
|
63
|
+
# Workouts
|
|
64
|
+
client.workouts.list # all pages, as models
|
|
65
|
+
client.workouts.list(limit: 50)
|
|
66
|
+
client.workouts.get(id)
|
|
67
|
+
client.workouts.create(name: "Ride", workout_token: "abc",
|
|
68
|
+
workout_type_id: 0, starts: Time.now.iso8601, minutes: 60)
|
|
69
|
+
client.workouts.update(id, name: "New name")
|
|
70
|
+
client.workouts.delete(id)
|
|
71
|
+
client.workouts.summary(id) # the workout summary
|
|
72
|
+
|
|
73
|
+
# Routes, plans, power zones (full CRUD)
|
|
74
|
+
client.routes.list
|
|
75
|
+
client.plans.list
|
|
76
|
+
client.power_zones.list
|
|
77
|
+
|
|
78
|
+
# Upload a FIT file (async — Wahoo generates the workout + summary)
|
|
79
|
+
upload = client.uploads.create(file: "ride.fit")
|
|
80
|
+
upload = client.uploads.get(upload.token) # poll until upload.complete?
|
|
81
|
+
|
|
82
|
+
# Revoke access
|
|
83
|
+
client.deauthorize
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Models & conversions
|
|
87
|
+
|
|
88
|
+
Responses are objects. Wahoo returns decimals as strings; the helpers handle that:
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
w = client.workouts.get(id)
|
|
92
|
+
w.name
|
|
93
|
+
w.workout_type # "BIKING" (from workout_type_id)
|
|
94
|
+
w.distance_km # 45.23 (from the string "45230.50")
|
|
95
|
+
w.duration_human # "1h 40m 12s"
|
|
96
|
+
w.avg_speed_kmh
|
|
97
|
+
w.raw # the original hash
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Webhooks
|
|
101
|
+
|
|
102
|
+
Set your webhook URL and token in the developer portal (requires `offline_data`).
|
|
103
|
+
|
|
104
|
+
```ruby
|
|
105
|
+
Wahoo.configure { |c| c.webhook_secret = "your_token" }
|
|
106
|
+
|
|
107
|
+
event = Wahoo::Webhooks::Handler.verify_token!(request.raw_post)
|
|
108
|
+
if event.workout_summary?
|
|
109
|
+
event.user_id
|
|
110
|
+
event.workout_summary.distance_km
|
|
111
|
+
event.workout
|
|
112
|
+
end
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Automatic retries
|
|
116
|
+
|
|
117
|
+
Retries on 429 and 5xx with backoff:
|
|
118
|
+
|
|
119
|
+
```ruby
|
|
120
|
+
Wahoo.configure do |c|
|
|
121
|
+
c.max_retries = 3
|
|
122
|
+
c.retry_backoff = 2
|
|
123
|
+
end
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Notes on Wahoo's behavior
|
|
127
|
+
|
|
128
|
+
- Wahoo returns decimal values as **strings** (e.g. `"450.00"`); the model
|
|
129
|
+
helpers convert them.
|
|
130
|
+
- Workouts you **create** via the API are owned by your app
|
|
131
|
+
(`fitness_app_id > 1000`) and won't appear in the Wahoo mobile app the same
|
|
132
|
+
way device-recorded workouts do — this is Wahoo's policy, not a bug.
|
|
133
|
+
- `routes`, `plans` and `power_zones` list only the records **your app**
|
|
134
|
+
created, not ones created in the Wahoo app.
|
|
135
|
+
- Sandbox apps have low rate limits; production apps require Wahoo review.
|
|
136
|
+
|
|
137
|
+
## Error handling
|
|
138
|
+
|
|
139
|
+
```ruby
|
|
140
|
+
rescue Wahoo::Unauthorized # 401 — refresh the token
|
|
141
|
+
rescue Wahoo::Forbidden # 403 — missing scope
|
|
142
|
+
rescue Wahoo::UnprocessableEntity # 422 — bad params / app not approved
|
|
143
|
+
rescue Wahoo::RateLimited => e # 429 — e.retry_after
|
|
144
|
+
rescue Wahoo::Error => e # e.status, e.body
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## CLI
|
|
148
|
+
|
|
149
|
+
Get a token from the terminal (uses HTTPS on localhost):
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
WAHOO_CLIENT_ID=... WAHOO_CLIENT_SECRET=... bundle exec wahoo-oauth-token
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Development
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
bin/setup
|
|
159
|
+
bundle exec rspec
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Contributing
|
|
163
|
+
|
|
164
|
+
Bug reports and pull requests are welcome on GitHub.
|
|
165
|
+
|
|
166
|
+
## License
|
|
167
|
+
|
|
168
|
+
MIT
|
data/Rakefile
ADDED
data/docs/conversions.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# Units & Conversions
|
|
2
|
+
|
|
3
|
+
The Wahoo API returns raw units: **meters** for distance/elevation, **seconds**
|
|
4
|
+
for duration, **m/s** for speed. `wahoo` adds helpers so you don't convert
|
|
5
|
+
by hand.
|
|
6
|
+
|
|
7
|
+
## On a workout summary
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
summary = client.workouts.get(id).workout_summary
|
|
11
|
+
|
|
12
|
+
# distance
|
|
13
|
+
summary.distance_km # 45.23
|
|
14
|
+
summary.distance_miles # 28.11
|
|
15
|
+
|
|
16
|
+
# elevation
|
|
17
|
+
summary.ascent_feet # 1115
|
|
18
|
+
summary.descent_feet
|
|
19
|
+
|
|
20
|
+
# duration
|
|
21
|
+
summary.duration_minutes # 100.2
|
|
22
|
+
summary.duration_hours # 1.67
|
|
23
|
+
summary.duration_human # "1h 40m 12s"
|
|
24
|
+
|
|
25
|
+
# speed / pace
|
|
26
|
+
summary.speed_avg_kmh # 26.3
|
|
27
|
+
summary.speed_avg_mph # 16.3
|
|
28
|
+
summary.speed_avg_pace_per_km # "2:17 /km"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## On a workout (shortcuts)
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
workout = client.workouts.get(id)
|
|
35
|
+
workout.distance_km
|
|
36
|
+
workout.distance_miles
|
|
37
|
+
workout.ascent_feet
|
|
38
|
+
workout.duration_human
|
|
39
|
+
workout.avg_speed_kmh
|
|
40
|
+
workout.avg_pace_per_km
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## On a route
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
route = client.routes.get(id)
|
|
47
|
+
route.distance_km
|
|
48
|
+
route.distance_miles
|
|
49
|
+
route.ascent_feet
|
|
50
|
+
route.descent_feet
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Using the raw module directly
|
|
54
|
+
|
|
55
|
+
All helpers are pure functions in `Wahoo::Conversions`:
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
Wahoo::Conversions.meters_to_km(45230) # 45.23
|
|
59
|
+
Wahoo::Conversions.meters_to_feet(340) # 1115
|
|
60
|
+
Wahoo::Conversions.seconds_to_human(6012) # "1h 40m 12s"
|
|
61
|
+
Wahoo::Conversions.mps_to_kmh(7.3) # 26.3
|
|
62
|
+
Wahoo::Conversions.mps_to_pace_per_km(4.17) # "4:00 /km"
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Raw values
|
|
66
|
+
|
|
67
|
+
Every model keeps the original data if you need it:
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
summary.distance_accum # 45230.0 (raw meters)
|
|
71
|
+
summary.raw # the full original hash
|
|
72
|
+
```
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Getting Started
|
|
2
|
+
|
|
3
|
+
This guide walks you from zero to reading a user's workouts.
|
|
4
|
+
|
|
5
|
+
## 1. Register a Wahoo app
|
|
6
|
+
|
|
7
|
+
Go to [developers.wahooligan.com](https://developers.wahooligan.com), create an
|
|
8
|
+
application (Sandbox first), and note your **Client ID** and **Client Secret**.
|
|
9
|
+
Set the redirect URI to where Wahoo should send users back after they authorize.
|
|
10
|
+
|
|
11
|
+
No device is required to develop — the Sandbox gives you test data.
|
|
12
|
+
|
|
13
|
+
## 2. Install
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
gem "wahoo"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 3. Configure
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
Wahoo.configure do |c|
|
|
23
|
+
c.client_id = ENV["WAHOO_CLIENT_ID"]
|
|
24
|
+
c.client_secret = ENV["WAHOO_CLIENT_SECRET"]
|
|
25
|
+
c.redirect_uri = "https://yourapp.com/wahoo/callback"
|
|
26
|
+
end
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## 4. Authenticate a user (OAuth 2.0)
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
# Step 1 — send the user to Wahoo
|
|
33
|
+
url = Wahoo::OAuth.authorize_url(scope: "user_read workouts_read")
|
|
34
|
+
# redirect the user's browser to `url`
|
|
35
|
+
|
|
36
|
+
# Step 2 — in your callback endpoint, exchange the code
|
|
37
|
+
tokens = Wahoo::OAuth.exchange_code(params[:code])
|
|
38
|
+
|
|
39
|
+
# store tokens.access_token and tokens.refresh_token for this user
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## 5. Read data
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
client = Wahoo::Client.new(access_token: tokens.access_token)
|
|
46
|
+
|
|
47
|
+
client.user.get.name # the user's name
|
|
48
|
+
client.workouts.list.size # all their workouts (all pages)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## 6. Refresh tokens when they expire
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
if tokens.expired?
|
|
55
|
+
tokens = Wahoo::OAuth.refresh(tokens.refresh_token)
|
|
56
|
+
# save the new tokens
|
|
57
|
+
end
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Next: see [Working with workouts](workouts.md) and [Units & conversions](conversions.md).
|
|
61
|
+
|
data/docs/workouts.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# Working with Workouts
|
|
2
|
+
|
|
3
|
+
## List all workouts
|
|
4
|
+
|
|
5
|
+
Pagination is automatic — you get every page as models:
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
client.workouts.list # all workouts
|
|
9
|
+
client.workouts.list(limit: 50) # stop after 50
|
|
10
|
+
client.workouts.list(per_page: 100) # 100 per request
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Get one workout
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
workout = client.workouts.get(123)
|
|
17
|
+
workout.name
|
|
18
|
+
workout.starts # a Time
|
|
19
|
+
workout.distance_km
|
|
20
|
+
workout.duration_human
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Access the summary metrics
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
s = workout.workout_summary
|
|
27
|
+
s.power_avg
|
|
28
|
+
s.heart_rate_avg
|
|
29
|
+
s.calories_accum
|
|
30
|
+
s.distance_km
|
|
31
|
+
s.ascent_feet
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Iterate
|
|
35
|
+
|
|
36
|
+
```ruby
|
|
37
|
+
client.workouts.list.each do |w|
|
|
38
|
+
puts "#{w.name}: #{w.distance_km} km in #{w.duration_human}"
|
|
39
|
+
end
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Errors
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
begin
|
|
46
|
+
client.workouts.list
|
|
47
|
+
rescue Wahoo::Unauthorized
|
|
48
|
+
# token expired — refresh it
|
|
49
|
+
rescue Wahoo::RateLimited => e
|
|
50
|
+
# the client already retried; if it still fails, back off e.retry_after
|
|
51
|
+
rescue Wahoo::Error => e
|
|
52
|
+
warn "Wahoo error #{e.status}: #{e.message}"
|
|
53
|
+
end
|
|
54
|
+
```
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class WahooWebhooksController < ApplicationController
|
|
4
|
+
skip_before_action :verify_authenticity_token
|
|
5
|
+
|
|
6
|
+
def create
|
|
7
|
+
event = Wahoo::Webhooks::Handler.verify_token!(request.raw_post)
|
|
8
|
+
|
|
9
|
+
if event.workout_summary?
|
|
10
|
+
user = User.find_by(wahoo_user_id: event.user_id) # event.user.id
|
|
11
|
+
if user
|
|
12
|
+
s = event.workout_summary
|
|
13
|
+
Rails.logger.info("User #{user.id}: tură #{s.distance_km} km, #{s.duration_human}")
|
|
14
|
+
# ...
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
head :ok
|
|
19
|
+
rescue Wahoo::Unauthorized
|
|
20
|
+
head :unauthorized
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "wahoo"
|
|
4
|
+
|
|
5
|
+
Wahoo.configure do |c|
|
|
6
|
+
c.client_id = ENV["WAHOO_CLIENT_ID"]
|
|
7
|
+
c.client_secret = ENV["WAHOO_CLIENT_SECRET"]
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
client = Wahoo::Client.new(access_token: ENV.fetch("WAHOO_ACCESS_TOKEN"))
|
|
11
|
+
|
|
12
|
+
puts "User: #{client.user.get.name}"
|
|
13
|
+
puts
|
|
14
|
+
|
|
15
|
+
client.workouts.list(limit: 10).each do |w|
|
|
16
|
+
line = "#{w.starts&.strftime('%d %b')} · #{w.name}"
|
|
17
|
+
line += " · #{w.distance_km} km" if w.distance_km
|
|
18
|
+
line += " · #{w.duration_human}" if w.duration_human
|
|
19
|
+
line += " · #{w.avg_speed_kmh} km/h" if w.avg_speed_kmh
|
|
20
|
+
puts line
|
|
21
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "wahoo"
|
|
5
|
+
require "webrick"
|
|
6
|
+
require "webrick/https"
|
|
7
|
+
require "openssl"
|
|
8
|
+
require "cgi"
|
|
9
|
+
|
|
10
|
+
PORT = (ENV["WAHOO_AUTH_PORT"] || 8080).to_i
|
|
11
|
+
REDIRECT = "https://localhost:#{PORT}/callback"
|
|
12
|
+
SCOPE = ENV["WAHOO_SCOPE"] || "user_read user_write workouts_read routes_read plans_read power_zones_read power_zones_write offline_data workouts_write"
|
|
13
|
+
|
|
14
|
+
Wahoo.configure do |c|
|
|
15
|
+
c.client_id = ENV.fetch("WAHOO_CLIENT_ID")
|
|
16
|
+
c.client_secret = ENV.fetch("WAHOO_CLIENT_SECRET")
|
|
17
|
+
c.redirect_uri = REDIRECT
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
url = Wahoo::OAuth.authorize_url(scope: SCOPE)
|
|
21
|
+
|
|
22
|
+
puts "\nOpen in browser (or it is automatically opened):"
|
|
23
|
+
puts url
|
|
24
|
+
puts
|
|
25
|
+
|
|
26
|
+
system("open '#{url}'") || system("xdg-open '#{url}'") rescue nil
|
|
27
|
+
|
|
28
|
+
key = OpenSSL::PKey::RSA.new(2048)
|
|
29
|
+
cert = OpenSSL::X509::Certificate.new
|
|
30
|
+
cert.version = 2
|
|
31
|
+
cert.serial = 1
|
|
32
|
+
cert.subject = OpenSSL::X509::Name.parse("/CN=localhost")
|
|
33
|
+
cert.issuer = cert.subject
|
|
34
|
+
cert.public_key = key.public_key
|
|
35
|
+
cert.not_before = Time.now
|
|
36
|
+
cert.not_after = Time.now + 3600
|
|
37
|
+
cert.sign(key, OpenSSL::Digest.new("SHA256"))
|
|
38
|
+
|
|
39
|
+
server = WEBrick::HTTPServer.new(
|
|
40
|
+
Port: PORT,
|
|
41
|
+
Logger: WEBrick::Log.new(File::NULL),
|
|
42
|
+
AccessLog: [],
|
|
43
|
+
SSLEnable: true,
|
|
44
|
+
SSLCertificate: cert,
|
|
45
|
+
SSLPrivateKey: key
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
trap("INT") { server.shutdown }
|
|
49
|
+
|
|
50
|
+
server.mount_proc "/callback" do |req, res|
|
|
51
|
+
code = req.query["code"]
|
|
52
|
+
if code
|
|
53
|
+
tokens = Wahoo::OAuth.exchange_code(code)
|
|
54
|
+
puts "\n✓ Received token!\n\n"
|
|
55
|
+
puts "access_token: #{tokens.access_token}"
|
|
56
|
+
puts "refresh_token: #{tokens.refresh_token}"
|
|
57
|
+
puts "expires_in: #{tokens.expires_in}"
|
|
58
|
+
res.body = "<h2>Done! You can close the tab and return at terminal.</h2>"
|
|
59
|
+
Thread.new { sleep 1; server.shutdown }
|
|
60
|
+
else
|
|
61
|
+
res.body = "<h2>The code is missing. Try again.</h2>"
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
puts "Wait for the callback #{REDIRECT} ...\n"
|
|
66
|
+
server.start
|
data/lib/wahoo/client.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wahoo
|
|
4
|
+
class Client
|
|
5
|
+
attr_reader :access_token
|
|
6
|
+
|
|
7
|
+
def initialize(access_token:, max_retries: nil, retry_backoff: nil)
|
|
8
|
+
raise ArgumentError, "access_token missing" if access_token.nil?
|
|
9
|
+
@access_token = access_token
|
|
10
|
+
@connection = Connection.new(access_token: access_token,
|
|
11
|
+
max_retries: max_retries, retry_backoff: retry_backoff)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def user; @user ||= Resources::User.new(@connection); end
|
|
15
|
+
def workouts; @workouts ||= Resources::Workouts.new(@connection); end
|
|
16
|
+
def routes; @routes ||= Resources::Routes.new(@connection); end
|
|
17
|
+
def plans; @plans ||= Resources::Plans.new(@connection); end
|
|
18
|
+
def power_zones; @power_zones ||= Resources::PowerZones.new(@connection); end
|
|
19
|
+
def uploads; @uploads ||= Resources::Uploads.new(@connection); end
|
|
20
|
+
def permissions; @permissions ||= Resources::Permissions.new(@connection); end
|
|
21
|
+
|
|
22
|
+
def deauthorize
|
|
23
|
+
permissions.revoke
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wahoo
|
|
4
|
+
class Configuration
|
|
5
|
+
attr_accessor :client_id, :client_secret, :redirect_uri, :base_url,
|
|
6
|
+
:default_per_page, :max_retries, :retry_backoff,
|
|
7
|
+
:webhook_secret
|
|
8
|
+
|
|
9
|
+
def initialize
|
|
10
|
+
@base_url = "https://api.wahooligan.com"
|
|
11
|
+
@default_per_page = 30
|
|
12
|
+
@max_retries = 3
|
|
13
|
+
@retry_backoff = 2
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|