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
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wahoo
|
|
4
|
+
class Connection
|
|
5
|
+
def initialize(access_token:, max_retries: nil, retry_backoff: nil)
|
|
6
|
+
@access_token = access_token
|
|
7
|
+
@base_url = Wahoo.configuration.base_url
|
|
8
|
+
@max_retries = max_retries || Wahoo.configuration.max_retries
|
|
9
|
+
@retry_backoff = retry_backoff || Wahoo.configuration.retry_backoff
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def get(path, params = {})
|
|
13
|
+
with_retries { request(:get, path, params) }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def post(path, body = {})
|
|
17
|
+
with_retries { request(:post, path, body) }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def put(path, body = {})
|
|
21
|
+
with_retries { request(:put, path, body) }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def delete(path, params = {})
|
|
25
|
+
with_retries { request(:delete, path, params) }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def with_retries
|
|
31
|
+
attempts = 0
|
|
32
|
+
begin
|
|
33
|
+
yield
|
|
34
|
+
rescue Wahoo::RateLimited => e
|
|
35
|
+
attempts += 1
|
|
36
|
+
raise e if attempts > @max_retries
|
|
37
|
+
sleep(e.retry_after || (@retry_backoff * attempts))
|
|
38
|
+
retry
|
|
39
|
+
rescue Wahoo::ServerError => e
|
|
40
|
+
attempts += 1
|
|
41
|
+
raise e if attempts > @max_retries
|
|
42
|
+
sleep(@retry_backoff * attempts)
|
|
43
|
+
retry
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def request(method, path, data)
|
|
48
|
+
resp = conn.public_send(method) do |req|
|
|
49
|
+
req.url(path)
|
|
50
|
+
req.headers["Authorization"] = "Bearer #{@access_token}"
|
|
51
|
+
req.headers["Accept"] = "application/json"
|
|
52
|
+
if %i[get delete].include?(method)
|
|
53
|
+
req.params = data
|
|
54
|
+
else
|
|
55
|
+
req.headers["Content-Type"] = "application/json"
|
|
56
|
+
req.body = data.to_json
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
handle(resp)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def handle(resp)
|
|
63
|
+
parsed = resp.body.to_s.empty? ? {} : JSON.parse(resp.body)
|
|
64
|
+
unless resp.success?
|
|
65
|
+
error = Wahoo::Error.from(resp.status, parsed)
|
|
66
|
+
if error.is_a?(Wahoo::RateLimited)
|
|
67
|
+
error.retry_after = resp.headers["x-ratelimit-reset"]&.to_i
|
|
68
|
+
end
|
|
69
|
+
raise error
|
|
70
|
+
end
|
|
71
|
+
Response.new(body: parsed, headers: resp.headers)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def conn
|
|
75
|
+
@conn ||= Faraday.new(url: @base_url)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
class Response
|
|
80
|
+
attr_reader :body, :headers
|
|
81
|
+
|
|
82
|
+
def initialize(body:, headers:)
|
|
83
|
+
@body = body
|
|
84
|
+
@headers = headers
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def rate_limit
|
|
88
|
+
parse_triple(headers["x-ratelimit-limit"])
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def rate_limit_remaining
|
|
92
|
+
parse_triple(headers["x-ratelimit-remaining"])
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def rate_limit_reset
|
|
96
|
+
headers["x-ratelimit-reset"]&.to_i
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
private
|
|
100
|
+
|
|
101
|
+
def parse_triple(value)
|
|
102
|
+
return nil unless value
|
|
103
|
+
day, hour, five = value.split(",").map { |v| v.strip.to_i }
|
|
104
|
+
{ day: day, hour: hour, five_min: five }
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wahoo
|
|
4
|
+
module Conversions
|
|
5
|
+
module_function
|
|
6
|
+
|
|
7
|
+
def to_f_or_nil(v)
|
|
8
|
+
return nil if v.nil? || v.to_s.strip.empty?
|
|
9
|
+
v.to_f
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def meters_to_km(m)
|
|
13
|
+
v = to_f_or_nil(m); v && (v / 1000).round(2)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def meters_to_miles(m)
|
|
17
|
+
v = to_f_or_nil(m); v && (v / 1609.344).round(2)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def meters_to_feet(m)
|
|
21
|
+
v = to_f_or_nil(m); v && (v * 3.28084).round(0).to_i
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def seconds_to_minutes(s)
|
|
25
|
+
v = to_f_or_nil(s); v && (v / 60).round(1)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def seconds_to_hours(s)
|
|
29
|
+
v = to_f_or_nil(s); v && (v / 3600).round(2)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def seconds_to_human(s)
|
|
33
|
+
v = to_f_or_nil(s); return nil unless v
|
|
34
|
+
v = v.to_i
|
|
35
|
+
h = v / 3600
|
|
36
|
+
m = (v % 3600) / 60
|
|
37
|
+
sec = v % 60
|
|
38
|
+
parts = []
|
|
39
|
+
parts << "#{h}h" if h.positive?
|
|
40
|
+
parts << "#{m}m" if m.positive? || h.positive?
|
|
41
|
+
parts << "#{sec}s"
|
|
42
|
+
parts.join(" ")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def mps_to_kmh(mps)
|
|
46
|
+
v = to_f_or_nil(mps); v && (v * 3.6).round(1)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def mps_to_mph(mps)
|
|
50
|
+
v = to_f_or_nil(mps); v && (v * 2.236936).round(1)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def mps_to_pace_per_km(mps)
|
|
54
|
+
v = to_f_or_nil(mps); return nil if v.nil? || v <= 0
|
|
55
|
+
spk = 1000.0 / v
|
|
56
|
+
min = (spk / 60).floor
|
|
57
|
+
sec = (spk % 60).round
|
|
58
|
+
if sec == 60
|
|
59
|
+
min += 1; sec = 0
|
|
60
|
+
end
|
|
61
|
+
format("%d:%02d /km", min, sec)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wahoo
|
|
4
|
+
module Data
|
|
5
|
+
WORKOUT_TYPES = {
|
|
6
|
+
0 => "BIKING", 1 => "RUNNING", 2 => "FE", 3 => "RUNNING_TRACK",
|
|
7
|
+
4 => "RUNNING_TRAIL", 5 => "RUNNING_TREADMILL", 6 => "WALKING",
|
|
8
|
+
7 => "WALKING_SPEED", 8 => "WALKING_NORDIC", 9 => "HIKING",
|
|
9
|
+
10 => "MOUNTAINEERING", 11 => "BIKING_CYCLECROSS", 12 => "BIKING_INDOOR",
|
|
10
|
+
13 => "BIKING_MOUNTAIN", 14 => "BIKING_RECUMBENT", 15 => "BIKING_ROAD",
|
|
11
|
+
16 => "BIKING_TRACK", 17 => "BIKING_MOTOCYCLING", 18 => "FE_GENERAL",
|
|
12
|
+
19 => "FE_TREADMILL", 20 => "FE_ELLIPTICAL", 21 => "FE_BIKE",
|
|
13
|
+
22 => "FE_ROWER", 23 => "FE_CLIMBER", 25 => "SWIMMING_LAP",
|
|
14
|
+
26 => "SWIMMING_OPEN_WATER", 27 => "SNOWBOARDING", 28 => "SKIING",
|
|
15
|
+
29 => "SKIING_DOWNHILL", 30 => "SKIINGCROSS_COUNTRY", 31 => "SKATING",
|
|
16
|
+
32 => "SKATING_ICE", 33 => "SKATING_INLINE", 34 => "LONG_BOARDING",
|
|
17
|
+
35 => "SAILING", 36 => "WINDSURFING", 37 => "CANOEING", 38 => "KAYAKING",
|
|
18
|
+
39 => "ROWING", 40 => "KITEBOARDING", 41 => "STAND_UP_PADDLE_BOARD",
|
|
19
|
+
42 => "WORKOUT", 43 => "CARDIO_CLASS", 44 => "STAIR_CLIMBER",
|
|
20
|
+
45 => "WHEELCHAIR", 46 => "GOLFING", 47 => "OTHER",
|
|
21
|
+
49 => "BIKING_INDOOR_CYCLING_CLASS", 56 => "WALKING_TREADMILL",
|
|
22
|
+
61 => "BIKING_INDOOR_TRAINER", 62 => "MULTISPORT", 63 => "TRANSITION",
|
|
23
|
+
64 => "EBIKING", 65 => "TICKR_OFFLINE", 66 => "YOGA",
|
|
24
|
+
67 => "RUNNING_RACE", 68 => "BIKING_INDOOR_VIRTUAL", 69 => "MENTAL_STRENGTH",
|
|
25
|
+
70 => "HANDCYCLING", 71 => "RUNNING_INDOOR_VIRTUAL", 255 => "UNKNOWN"
|
|
26
|
+
}.freeze
|
|
27
|
+
|
|
28
|
+
WORKOUT_TYPE_LOCATIONS = {
|
|
29
|
+
0 => "INDOOR", 1 => "OUTDOOR", 255 => "UNKNOWN"
|
|
30
|
+
}.freeze
|
|
31
|
+
|
|
32
|
+
WORKOUT_TYPE_FAMILIES = {
|
|
33
|
+
0 => "BIKING", 1 => "RUNNING", 2 => "SWIMMING", 3 => "WATER SPORTS",
|
|
34
|
+
4 => "SNOW SPORTS", 5 => "SKATING", 6 => "GYM", 9 => "WALKING",
|
|
35
|
+
30 => "N/A", 31 => "OTHER", 255 => "UNKNOWN"
|
|
36
|
+
}.freeze
|
|
37
|
+
|
|
38
|
+
module_function
|
|
39
|
+
|
|
40
|
+
def workout_type(id); WORKOUT_TYPES[id]; end
|
|
41
|
+
def workout_type_location(id); WORKOUT_TYPE_LOCATIONS[id]; end
|
|
42
|
+
def workout_type_family(id); WORKOUT_TYPE_FAMILIES[id]; end
|
|
43
|
+
end
|
|
44
|
+
end
|
data/lib/wahoo/error.rb
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wahoo
|
|
4
|
+
class Error < StandardError
|
|
5
|
+
attr_reader :status, :body
|
|
6
|
+
|
|
7
|
+
def initialize(message, status: nil, body: nil)
|
|
8
|
+
@status = status
|
|
9
|
+
@body = body
|
|
10
|
+
super(message)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.from(status, body)
|
|
14
|
+
message =
|
|
15
|
+
if body.is_a?(Hash)
|
|
16
|
+
body["error"] || body["errors"] || body["message"]
|
|
17
|
+
else
|
|
18
|
+
body.to_s
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
klass =
|
|
22
|
+
case status
|
|
23
|
+
when 400 then BadRequest
|
|
24
|
+
when 401 then Unauthorized
|
|
25
|
+
when 403 then Forbidden
|
|
26
|
+
when 404 then NotFound
|
|
27
|
+
when 405 then MethodNotAllowed
|
|
28
|
+
when 406 then NotAcceptable
|
|
29
|
+
when 410 then Gone
|
|
30
|
+
when 422 then UnprocessableEntity
|
|
31
|
+
when 429 then RateLimited
|
|
32
|
+
when 500..599 then ServerError
|
|
33
|
+
else Error
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
klass.new(message || "Wahoo API error (#{status})", status: status, body: body)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
class BadRequest < Error; end
|
|
41
|
+
class Unauthorized < Error; end
|
|
42
|
+
class Forbidden < Error; end
|
|
43
|
+
class NotFound < Error; end
|
|
44
|
+
class MethodNotAllowed < Error; end
|
|
45
|
+
class NotAcceptable < Error; end
|
|
46
|
+
class Gone < Error; end
|
|
47
|
+
class UnprocessableEntity < Error; end
|
|
48
|
+
class ServerError < Error; end
|
|
49
|
+
class ConfigurationError < Error; end
|
|
50
|
+
|
|
51
|
+
class RateLimited < Error
|
|
52
|
+
attr_accessor :retry_after
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wahoo
|
|
4
|
+
module Models
|
|
5
|
+
|
|
6
|
+
class Base
|
|
7
|
+
attr_reader :raw
|
|
8
|
+
|
|
9
|
+
def initialize(attributes = {})
|
|
10
|
+
@raw = attributes || {}
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.property(name, key: nil, as: nil)
|
|
14
|
+
json_key = (key || name).to_s
|
|
15
|
+
|
|
16
|
+
define_method(name) do
|
|
17
|
+
value = @raw[json_key]
|
|
18
|
+
return nil if value.nil?
|
|
19
|
+
|
|
20
|
+
case as
|
|
21
|
+
when nil then value
|
|
22
|
+
when :time then (Time.parse(value) rescue value)
|
|
23
|
+
when :date then (Date.parse(value) rescue value)
|
|
24
|
+
when Class then as.new(value)
|
|
25
|
+
when Proc then as.call(value)
|
|
26
|
+
else value
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.from(hash)
|
|
32
|
+
return nil if hash.nil?
|
|
33
|
+
new(hash)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.from_list(array)
|
|
37
|
+
Array(array).map { |h| new(h) }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def [](key)
|
|
41
|
+
@raw[key.to_s]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def to_h
|
|
45
|
+
@raw
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def inspect
|
|
49
|
+
"#<#{self.class.name} #{@raw.inspect}>"
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wahoo
|
|
4
|
+
module Models
|
|
5
|
+
class Plan < Base
|
|
6
|
+
property :id
|
|
7
|
+
property :user_id
|
|
8
|
+
property :name
|
|
9
|
+
property :description
|
|
10
|
+
property :file
|
|
11
|
+
property :workout_type_family_id
|
|
12
|
+
property :workout_type_location_id
|
|
13
|
+
property :external_id
|
|
14
|
+
property :provider_updated_at, as: :time
|
|
15
|
+
property :deleted
|
|
16
|
+
property :created_at, as: :time
|
|
17
|
+
property :updated_at, as: :time
|
|
18
|
+
|
|
19
|
+
def file_url
|
|
20
|
+
file.is_a?(Hash) ? file["url"] : nil
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wahoo
|
|
4
|
+
module Models
|
|
5
|
+
class PowerZone < Base
|
|
6
|
+
property :id
|
|
7
|
+
property :user_id
|
|
8
|
+
property :zone_1
|
|
9
|
+
property :zone_2
|
|
10
|
+
property :zone_3
|
|
11
|
+
property :zone_4
|
|
12
|
+
property :zone_5
|
|
13
|
+
property :zone_6
|
|
14
|
+
property :zone_7
|
|
15
|
+
property :zone_count
|
|
16
|
+
property :ftp
|
|
17
|
+
property :critical_power
|
|
18
|
+
property :workout_type_id
|
|
19
|
+
property :workout_type_family_id
|
|
20
|
+
property :workout_type_location_id
|
|
21
|
+
property :originator_type
|
|
22
|
+
property :fitness_app_id
|
|
23
|
+
property :created_at, as: :time
|
|
24
|
+
property :updated_at, as: :time
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wahoo
|
|
4
|
+
module Models
|
|
5
|
+
class Route < Base
|
|
6
|
+
property :id
|
|
7
|
+
property :user_id
|
|
8
|
+
property :name
|
|
9
|
+
property :description
|
|
10
|
+
property :file
|
|
11
|
+
property :workout_type_family_id
|
|
12
|
+
property :external_id
|
|
13
|
+
property :provider_updated_at, as: :time
|
|
14
|
+
property :deleted
|
|
15
|
+
property :start_lat
|
|
16
|
+
property :start_lng
|
|
17
|
+
property :distance
|
|
18
|
+
property :ascent
|
|
19
|
+
property :descent
|
|
20
|
+
property :created_at, as: :time
|
|
21
|
+
property :updated_at, as: :time
|
|
22
|
+
|
|
23
|
+
def file_url
|
|
24
|
+
file.is_a?(Hash) ? file["url"] : nil
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def distance_km; Conversions.meters_to_km(distance); end
|
|
28
|
+
def distance_miles; Conversions.meters_to_miles(distance); end
|
|
29
|
+
def ascent_feet; Conversions.meters_to_feet(ascent); end
|
|
30
|
+
def descent_feet; Conversions.meters_to_feet(descent); end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wahoo
|
|
4
|
+
module Models
|
|
5
|
+
class User < Base
|
|
6
|
+
property :id
|
|
7
|
+
property :email
|
|
8
|
+
property :first
|
|
9
|
+
property :last
|
|
10
|
+
property :height
|
|
11
|
+
property :weight
|
|
12
|
+
property :birth, as: :date
|
|
13
|
+
property :gender
|
|
14
|
+
property :created_at, as: :time
|
|
15
|
+
property :updated_at, as: :time
|
|
16
|
+
|
|
17
|
+
def name
|
|
18
|
+
[first, last].compact.join(" ").strip
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def gender_label
|
|
22
|
+
{ 0 => "male", 1 => "female", 2 => "other", 3 => "prefer not to say" }[gender]
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wahoo
|
|
4
|
+
module Models
|
|
5
|
+
class Workout < Base
|
|
6
|
+
property :id
|
|
7
|
+
property :name
|
|
8
|
+
property :starts, as: :time
|
|
9
|
+
property :minutes
|
|
10
|
+
property :day_code
|
|
11
|
+
property :plan_id
|
|
12
|
+
property :plan_ids
|
|
13
|
+
property :route_id
|
|
14
|
+
property :workout_token
|
|
15
|
+
property :workout_type_id
|
|
16
|
+
property :created_at, as: :time
|
|
17
|
+
property :updated_at, as: :time
|
|
18
|
+
property :workout_summary, as: ->(h) { WorkoutSummary.from(h) }
|
|
19
|
+
|
|
20
|
+
def workout_type
|
|
21
|
+
Wahoo::Data::WORKOUT_TYPES[workout_type_id]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def completed?
|
|
25
|
+
!workout_summary.nil?
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def distance_km; workout_summary&.distance_km; end
|
|
29
|
+
def distance_miles; workout_summary&.distance_miles; end
|
|
30
|
+
def duration_human; workout_summary&.duration_human; end
|
|
31
|
+
def avg_speed_kmh; workout_summary&.speed_avg_kmh; end
|
|
32
|
+
def avg_pace_per_km; workout_summary&.speed_avg_pace_per_km; end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
class WorkoutSummary < Base
|
|
36
|
+
property :id
|
|
37
|
+
property :name
|
|
38
|
+
property :ascent_accum # metri
|
|
39
|
+
property :cadence_avg # rpm
|
|
40
|
+
property :calories_accum # kCal
|
|
41
|
+
property :distance_accum # metri
|
|
42
|
+
property :duration_active_accum # secunde
|
|
43
|
+
property :duration_paused_accum # secunde
|
|
44
|
+
property :duration_total_accum # secunde
|
|
45
|
+
property :heart_rate_avg # bpm
|
|
46
|
+
property :power_bike_np_last # watts (normalized power)
|
|
47
|
+
property :power_bike_tss_last # unitless (TSS)
|
|
48
|
+
property :power_avg # watts
|
|
49
|
+
property :speed_avg # m/s
|
|
50
|
+
property :work_accum # joules
|
|
51
|
+
property :time_zone
|
|
52
|
+
property :manual
|
|
53
|
+
property :edited
|
|
54
|
+
property :fitness_app_id
|
|
55
|
+
property :file # { "url" => "...fit" }
|
|
56
|
+
property :created_at, as: :time
|
|
57
|
+
property :updated_at, as: :time
|
|
58
|
+
|
|
59
|
+
def file_url
|
|
60
|
+
file.is_a?(Hash) ? file["url"] : nil
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def distance_km; Conversions.meters_to_km(distance_accum); end
|
|
64
|
+
def distance_miles; Conversions.meters_to_miles(distance_accum); end
|
|
65
|
+
def ascent_feet; Conversions.meters_to_feet(ascent_accum); end
|
|
66
|
+
def duration_minutes; Conversions.seconds_to_minutes(duration_active_accum); end
|
|
67
|
+
def duration_hours; Conversions.seconds_to_hours(duration_active_accum); end
|
|
68
|
+
def duration_human; Conversions.seconds_to_human(duration_active_accum); end
|
|
69
|
+
def speed_avg_kmh; Conversions.mps_to_kmh(speed_avg); end
|
|
70
|
+
def speed_avg_mph; Conversions.mps_to_mph(speed_avg); end
|
|
71
|
+
def speed_avg_pace_per_km; Conversions.mps_to_pace_per_km(speed_avg); end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wahoo
|
|
4
|
+
module Models
|
|
5
|
+
class WorkoutFileUpload < Base
|
|
6
|
+
property :id
|
|
7
|
+
property :user_id
|
|
8
|
+
property :token
|
|
9
|
+
property :status
|
|
10
|
+
property :time_zone
|
|
11
|
+
property :workout_id
|
|
12
|
+
property :workout_summary_id
|
|
13
|
+
property :workout_file_id
|
|
14
|
+
property :workout_name
|
|
15
|
+
property :error
|
|
16
|
+
property :target_workout_id
|
|
17
|
+
property :created_at, as: :time
|
|
18
|
+
property :updated_at, as: :time
|
|
19
|
+
|
|
20
|
+
def complete?; status == "complete"; end
|
|
21
|
+
def error?; status == "error"; end
|
|
22
|
+
def duplicate?; status == "duplicate"; end
|
|
23
|
+
def pending?; %w[pending in_progress].include?(status); end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
data/lib/wahoo/oauth.rb
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "uri"
|
|
4
|
+
require "securerandom"
|
|
5
|
+
require "digest"
|
|
6
|
+
require "base64"
|
|
7
|
+
|
|
8
|
+
module Wahoo
|
|
9
|
+
# OAuth 2.0: flux standard + PKCE. Plus deauthorize (revoke).
|
|
10
|
+
module OAuth
|
|
11
|
+
AUTHORIZE_PATH = "/oauth/authorize"
|
|
12
|
+
TOKEN_PATH = "/oauth/token"
|
|
13
|
+
|
|
14
|
+
Tokens = Struct.new(:access_token, :refresh_token, :expires_in, :created_at,
|
|
15
|
+
keyword_init: true) do
|
|
16
|
+
def expired?
|
|
17
|
+
return false unless created_at && expires_in
|
|
18
|
+
Time.now.to_i >= (created_at.to_i + expires_in.to_i)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
module_function
|
|
23
|
+
|
|
24
|
+
def authorize_url(scope:, state: nil, code_challenge: nil, code_challenge_method: nil)
|
|
25
|
+
cfg = require_config!
|
|
26
|
+
params = {
|
|
27
|
+
client_id: cfg.client_id,
|
|
28
|
+
redirect_uri: cfg.redirect_uri,
|
|
29
|
+
scope: scope,
|
|
30
|
+
response_type: "code"
|
|
31
|
+
}
|
|
32
|
+
params[:state] = state if state
|
|
33
|
+
|
|
34
|
+
if code_challenge
|
|
35
|
+
params[:code_challenge] = code_challenge
|
|
36
|
+
params[:code_challenge_method] = code_challenge_method || "S256"
|
|
37
|
+
end
|
|
38
|
+
"#{cfg.base_url}#{AUTHORIZE_PATH}?#{URI.encode_www_form(params)}"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def exchange_code(code, code_verifier: nil)
|
|
42
|
+
cfg = require_config!
|
|
43
|
+
params = {
|
|
44
|
+
grant_type: "authorization_code",
|
|
45
|
+
code: code,
|
|
46
|
+
client_id: cfg.client_id,
|
|
47
|
+
redirect_uri: cfg.redirect_uri
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if code_verifier
|
|
51
|
+
params[:code_verifier] = code_verifier
|
|
52
|
+
else
|
|
53
|
+
params[:client_secret] = cfg.client_secret
|
|
54
|
+
end
|
|
55
|
+
post_token(params)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def refresh(refresh_token, code_verifier: nil)
|
|
59
|
+
cfg = require_config!
|
|
60
|
+
params = {
|
|
61
|
+
grant_type: "refresh_token",
|
|
62
|
+
refresh_token: refresh_token,
|
|
63
|
+
client_id: cfg.client_id
|
|
64
|
+
}
|
|
65
|
+
if code_verifier
|
|
66
|
+
params[:code_verifier] = code_verifier
|
|
67
|
+
else
|
|
68
|
+
params[:client_secret] = cfg.client_secret
|
|
69
|
+
end
|
|
70
|
+
post_token(params)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def generate_code_verifier
|
|
74
|
+
SecureRandom.urlsafe_base64(64).tr("=", "")
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def code_challenge_for(verifier)
|
|
78
|
+
digest = Digest::SHA256.digest(verifier)
|
|
79
|
+
Base64.urlsafe_encode64(digest).tr("=", "")
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def post_token(params)
|
|
83
|
+
cfg = Wahoo.configuration
|
|
84
|
+
resp = Faraday.post("#{cfg.base_url}#{TOKEN_PATH}", params)
|
|
85
|
+
body = resp.body.to_s.empty? ? {} : JSON.parse(resp.body)
|
|
86
|
+
raise Wahoo::Error.from(resp.status, body) unless resp.success?
|
|
87
|
+
|
|
88
|
+
Tokens.new(
|
|
89
|
+
access_token: body["access_token"],
|
|
90
|
+
refresh_token: body["refresh_token"],
|
|
91
|
+
expires_in: body["expires_in"],
|
|
92
|
+
created_at: body["created_at"] || Time.now.to_i
|
|
93
|
+
)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def require_config!
|
|
97
|
+
cfg = Wahoo.configuration
|
|
98
|
+
if cfg.client_id.nil?
|
|
99
|
+
raise Wahoo::ConfigurationError.new("Set client_id to Wahoo.configure")
|
|
100
|
+
end
|
|
101
|
+
cfg
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wahoo
|
|
4
|
+
module Resources
|
|
5
|
+
class Base
|
|
6
|
+
def initialize(connection)
|
|
7
|
+
@connection = connection
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
def paginate(path, model:, root: nil, per_page: nil, limit: nil, **params)
|
|
13
|
+
per_page ||= Wahoo.configuration.default_per_page
|
|
14
|
+
results = []
|
|
15
|
+
page = 1
|
|
16
|
+
|
|
17
|
+
loop do
|
|
18
|
+
body = @connection.get(path, params.merge(page: page, per_page: per_page)).body
|
|
19
|
+
items = root ? Array(body[root.to_s]) : Array(body)
|
|
20
|
+
break if items.empty?
|
|
21
|
+
|
|
22
|
+
results.concat(items.map { |h| model.new(h) })
|
|
23
|
+
break if items.size < per_page
|
|
24
|
+
break if limit && results.size >= limit
|
|
25
|
+
page += 1
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
limit ? results.first(limit) : results
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def get_one(path, model:)
|
|
32
|
+
model.from(@connection.get(path).body)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def post_model(path, body, model:)
|
|
36
|
+
model.from(@connection.post(path, body).body)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def put_model(path, body, model:)
|
|
40
|
+
model.from(@connection.put(path, body).body)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def delete_path(path)
|
|
44
|
+
@connection.delete(path).body
|
|
45
|
+
rescue Wahoo::NotFound
|
|
46
|
+
nil
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def file_data_uri(file, mime)
|
|
50
|
+
content = file.is_a?(String) ? File.binread(file) : file.read
|
|
51
|
+
"data:#{mime};base64,#{[content].pack('m0')}"
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|