strava-ruby-client 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +14 -0
  3. data/README.md +710 -88
  4. data/bin/strava-webhooks.rb +56 -0
  5. data/lib/strava-ruby-client.rb +37 -0
  6. data/lib/strava/api/client.rb +11 -141
  7. data/lib/strava/api/endpoints/activities.rb +120 -0
  8. data/lib/strava/api/endpoints/athletes.rb +42 -0
  9. data/lib/strava/api/endpoints/clubs.rb +75 -0
  10. data/lib/strava/api/endpoints/gears.rb +18 -0
  11. data/lib/strava/api/endpoints/routes.rb +55 -0
  12. data/lib/strava/api/endpoints/running_races.rb +30 -0
  13. data/lib/strava/api/endpoints/segment_efforts.rb +33 -0
  14. data/lib/strava/api/endpoints/segments.rb +111 -0
  15. data/lib/strava/api/endpoints/streams.rb +61 -0
  16. data/lib/strava/api/endpoints/uploads.rb +40 -0
  17. data/lib/strava/logger.rb +1 -1
  18. data/lib/strava/models/achievement.rb +9 -0
  19. data/lib/strava/models/activity.rb +11 -5
  20. data/lib/strava/models/activity_stats.rb +17 -0
  21. data/lib/strava/models/activity_total.rb +17 -0
  22. data/lib/strava/models/athlete.rb +10 -0
  23. data/lib/strava/models/club.rb +6 -0
  24. data/lib/strava/models/club_admin.rb +13 -0
  25. data/lib/strava/models/club_member.rb +16 -0
  26. data/lib/strava/models/explorer_segment.rb +19 -0
  27. data/lib/strava/models/gear.rb +5 -0
  28. data/lib/strava/models/heart_rate_zone_ranges.rb +8 -0
  29. data/lib/strava/models/kudoser.rb +10 -0
  30. data/lib/strava/models/lap.rb +3 -0
  31. data/lib/strava/models/mixins/distance.rb +18 -0
  32. data/lib/strava/models/power_zone_ranges.rb +7 -0
  33. data/lib/strava/models/route.rb +27 -0
  34. data/lib/strava/models/running_race.rb +33 -0
  35. data/lib/strava/models/segment.rb +15 -3
  36. data/lib/strava/models/segment_effort.rb +7 -2
  37. data/lib/strava/models/segment_leaderboard.rb +10 -0
  38. data/lib/strava/models/segment_leaderboard_entry.rb +12 -0
  39. data/lib/strava/models/segment_stats.rb +15 -0
  40. data/lib/strava/models/stream.rb +10 -0
  41. data/lib/strava/models/stream_set.rb +17 -0
  42. data/lib/strava/models/upload.rb +11 -0
  43. data/lib/strava/models/zone_range.rb +8 -0
  44. data/lib/strava/models/zones.rb +8 -0
  45. data/lib/strava/version.rb +1 -1
  46. data/lib/strava/web/client.rb +11 -1
  47. data/lib/strava/web/connection.rb +1 -1
  48. data/lib/strava/webhooks/client.rb +58 -0
  49. data/lib/strava/webhooks/config.rb +33 -0
  50. data/lib/strava/webhooks/models/challenge.rb +15 -0
  51. data/lib/strava/webhooks/models/event.rb +15 -0
  52. data/lib/strava/webhooks/models/subscription.rb +14 -0
  53. metadata +37 -2
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'dotenv/load'
4
+ require 'strava-ruby-client'
5
+ require 'webrick'
6
+
7
+ logger = ::Logger.new(STDOUT)
8
+ logger.level = Logger::INFO
9
+
10
+ client = Strava::Webhooks::Client.new(
11
+ client_id: ENV['STRAVA_CLIENT_ID'],
12
+ client_secret: ENV['STRAVA_CLIENT_SECRET'],
13
+ logger: logger
14
+ )
15
+
16
+ case ARGV[0]
17
+ when 'create' then
18
+ callback_url = ARGV[1]
19
+ raise 'Missing callback_url.' unless callback_url
20
+
21
+ client.logger.info "Subscribing to #{callback_url} ..."
22
+ subscription = client.create_push_subscription(callback_url: callback_url, verify_token: 'token')
23
+ puts subscription
24
+ when 'delete'
25
+ id = ARGV[1]
26
+ raise 'Missing id.' unless id
27
+
28
+ client.logger.info "Unsubscribing #{id} ..."
29
+ client.delete_push_subscription(id: id)
30
+ client.logger.info 'Done.'
31
+ when 'handle'
32
+ server = WEBrick::HTTPServer.new(Port: 4242)
33
+
34
+ trap 'INT' do
35
+ server.shutdown
36
+ end
37
+
38
+ server.mount_proc '/' do |req, res|
39
+ case req.request_method
40
+ when 'GET' then
41
+ challenge = Strava::Webhooks::Models::Challenge.new(req.query)
42
+ res.content_type = 'application/json'
43
+ res.body = challenge.response.to_json
44
+ when 'POST' then
45
+ event = Strava::Webhooks::Models::Event.new(JSON.parse(req.body))
46
+ server.logger.info event
47
+ res.content_type = 'application/json'
48
+ res.body = { ok: true }.to_json
49
+ end
50
+ end
51
+ server.start
52
+ else
53
+ client.push_subscriptions.each do |s|
54
+ puts s
55
+ end
56
+ end
@@ -24,6 +24,8 @@ require_relative 'strava/models/athlete'
24
24
  require_relative 'strava/models/map'
25
25
  require_relative 'strava/models/activity'
26
26
  require_relative 'strava/models/club'
27
+ require_relative 'strava/models/club_admin'
28
+ require_relative 'strava/models/club_member'
27
29
  require_relative 'strava/models/segment_effort'
28
30
  require_relative 'strava/models/photos'
29
31
  require_relative 'strava/models/photo'
@@ -36,6 +38,23 @@ require_relative 'strava/models/segment'
36
38
  require_relative 'strava/models/activity_zone'
37
39
  require_relative 'strava/models/timed_zone_range'
38
40
  require_relative 'strava/models/comment'
41
+ require_relative 'strava/models/zones'
42
+ require_relative 'strava/models/heart_rate_zone_ranges'
43
+ require_relative 'strava/models/power_zone_ranges'
44
+ require_relative 'strava/models/zone_range'
45
+ require_relative 'strava/models/activity_total'
46
+ require_relative 'strava/models/activity_stats'
47
+ require_relative 'strava/models/route'
48
+ require_relative 'strava/models/kudoser'
49
+ require_relative 'strava/models/running_race'
50
+ require_relative 'strava/models/achievement'
51
+ require_relative 'strava/models/segment_stats'
52
+ require_relative 'strava/models/segment_leaderboard'
53
+ require_relative 'strava/models/segment_leaderboard_entry'
54
+ require_relative 'strava/models/explorer_segment'
55
+ require_relative 'strava/models/stream_set'
56
+ require_relative 'strava/models/stream'
57
+ require_relative 'strava/models/upload'
39
58
 
40
59
  require_relative 'strava/web/raise_error'
41
60
  require_relative 'strava/web/connection'
@@ -46,6 +65,24 @@ require_relative 'strava/web/client'
46
65
  require_relative 'strava/oauth/config'
47
66
  require_relative 'strava/oauth/client'
48
67
 
68
+ require_relative 'strava/webhooks/config'
69
+ require_relative 'strava/webhooks/client'
70
+ require_relative 'strava/webhooks/models/challenge'
71
+ require_relative 'strava/webhooks/models/subscription'
72
+ require_relative 'strava/webhooks/models/event'
73
+
49
74
  require_relative 'strava/api/config'
50
75
  require_relative 'strava/api/cursor'
76
+
77
+ require_relative 'strava/api/endpoints/activities'
78
+ require_relative 'strava/api/endpoints/athletes'
79
+ require_relative 'strava/api/endpoints/clubs'
80
+ require_relative 'strava/api/endpoints/gears'
81
+ require_relative 'strava/api/endpoints/routes'
82
+ require_relative 'strava/api/endpoints/running_races'
83
+ require_relative 'strava/api/endpoints/segment_efforts'
84
+ require_relative 'strava/api/endpoints/segments'
85
+ require_relative 'strava/api/endpoints/streams'
86
+ require_relative 'strava/api/endpoints/uploads'
87
+
51
88
  require_relative 'strava/api/client'
@@ -1,6 +1,17 @@
1
1
  module Strava
2
2
  module Api
3
3
  class Client < Strava::Web::Client
4
+ include Endpoints::Activities
5
+ include Endpoints::Athletes
6
+ include Endpoints::Clubs
7
+ include Endpoints::Gears
8
+ include Endpoints::Routes
9
+ include Endpoints::RunningRaces
10
+ include Endpoints::SegmentEfforts
11
+ include Endpoints::Segments
12
+ include Endpoints::Streams
13
+ include Endpoints::Uploads
14
+
4
15
  attr_accessor(*Config::ATTRIBUTES)
5
16
 
6
17
  def initialize(options = {})
@@ -14,147 +25,6 @@ module Strava
14
25
  { 'Authorization' => "Bearer #{access_token}" }
15
26
  end
16
27
 
17
- #
18
- # Get logged-in athlete.
19
- #
20
- def athlete
21
- Strava::Models::Athlete.new(get('athlete'))
22
- end
23
-
24
- #
25
- # Get logged-in athlete.
26
- #
27
- def activity(options = {})
28
- throw ArgumentError.new('Required argument :id missing') if options[:id].nil?
29
- Strava::Models::Activity.new(get("activities/#{options[:id]}", options.except(:id)))
30
- end
31
-
32
- #
33
- # List logged-in athlete activities.
34
- #
35
- # @option options [Integer] :before
36
- # An epoch timestamp to use for filtering activities that have taken place before a certain time.
37
- # @option options [Integer] :after
38
- # An epoch timestamp to use for filtering activities that have taken place after a certain time.
39
- # @option options [Integer] :page
40
- # Page number.
41
- # @option options [Integer] :per_page
42
- # Number of items per page. Defaults to 30.
43
- #
44
- def athlete_activities(options = {}, &block)
45
- paginate 'athlete/activities', options, Strava::Models::Activity, &block
46
- end
47
-
48
- #
49
- # Create an activity.
50
- #
51
- def create_activity(options = {})
52
- Strava::Models::Activity.new(post('activities', options))
53
- end
54
-
55
- #
56
- # Update an activity.
57
- # @option options [Boolean] :commute
58
- # Whether this activity is a commute.
59
- # @option options [Boolean] :trainer
60
- # Whether this activity was recorded on a training machine.
61
- # @option options [String] :description
62
- # The description of the activity.
63
- # @option options [String] :name
64
- # The name of the activity.
65
- # @option options [String] :type
66
- # Activity type.
67
- # @option options [String] :gear_id
68
- # Identifier for the gear associated with the activity. Specifying "none" clears gear from activity.
69
- #
70
- def update_activity(options = {})
71
- throw ArgumentError.new('Required argument :id missing') if options[:id].nil?
72
- Strava::Models::Activity.new(put("activities/#{options[:id]}", options.except(:id)))
73
- end
74
-
75
- #
76
- # List logged-in athlete clubs.
77
- #
78
- # @option options [Integer] :page
79
- # Page number.
80
- # @option options [Integer] :per_page
81
- # Number of items per page. Defaults to 30.
82
- #
83
- def athlete_clubs(options = {}, &block)
84
- paginate 'athlete/clubs', options, Strava::Models::Club, &block
85
- end
86
-
87
- #
88
- # List club activities.
89
- #
90
- # @option options [String] :id
91
- # Club id.
92
- # @option options [Integer] :page
93
- # Page number.
94
- # @option options [Integer] :per_page
95
- # Number of items per page. Defaults to 30.
96
- #
97
- def club_activities(options = {}, &block)
98
- throw ArgumentError.new('Required argument :id missing') if options[:id].nil?
99
- paginate "clubs/#{options[:id]}/activities", options.except(:id), Strava::Models::Activity, &block
100
- end
101
-
102
- #
103
- # List activity comments.
104
- #
105
- # @option options [String] :id
106
- # Activity id.
107
- # @option options [Integer] :page
108
- # Page number.
109
- # @option options [Integer] :per_page
110
- # Number of items per page. Defaults to 30.
111
- #
112
- def activity_comments(options = {}, &block)
113
- throw ArgumentError.new('Required argument :id missing') if options[:id].nil?
114
- paginate "activities/#{options[:id]}/comments", options.except(:id), Strava::Models::Comment, &block
115
- end
116
-
117
- #
118
- # List activity kudoers.
119
- #
120
- # @option options [String] :id
121
- # Activity id.
122
- # @option options [Integer] :page
123
- # Page number.
124
- # @option options [Integer] :per_page
125
- # Number of items per page. Defaults to 30.
126
- #
127
- def activity_kudos(options = {}, &block)
128
- throw ArgumentError.new('Required argument :id missing') if options[:id].nil?
129
- paginate "activities/#{options[:id]}/kudos", options.except(:id), Strava::Models::Athlete, &block
130
- end
131
-
132
- #
133
- # Get activity zones.
134
- #
135
- # @option options [String] :id
136
- # Activity id.
137
- #
138
- def activity_zones(options = {})
139
- throw ArgumentError.new('Required argument :id missing') if options[:id].nil?
140
- get("activities/#{options[:id]}/zones", options).map do |row|
141
- Strava::Models::ActivityZone.new(row)
142
- end
143
- end
144
-
145
- #
146
- # Get activity laps.
147
- #
148
- # @option options [String] :id
149
- # Activity id.
150
- #
151
- def activity_laps(options = {})
152
- throw ArgumentError.new('Required argument :id missing') if options[:id].nil?
153
- get("activities/#{options[:id]}/laps", options).map do |row|
154
- Strava::Models::Lap.new(row)
155
- end
156
- end
157
-
158
28
  class << self
159
29
  def configure
160
30
  block_given? ? yield(Config) : Config
@@ -0,0 +1,120 @@
1
+ module Strava
2
+ module Api
3
+ module Endpoints
4
+ module Activities
5
+ #
6
+ # Create an activity.
7
+ #
8
+ def create_activity(options = {})
9
+ Strava::Models::Activity.new(post('activities', options))
10
+ end
11
+
12
+ #
13
+ # Get activity.
14
+ #
15
+ # @option options [String] :id
16
+ # Activity id.
17
+ #
18
+ def activity(id_or_options, options = {})
19
+ id, options = parse_args(id_or_options, options)
20
+ Strava::Models::Activity.new(get("activities/#{id}", options))
21
+ end
22
+
23
+ #
24
+ # List activity comments.
25
+ #
26
+ # @option options [String] :id
27
+ # Activity id.
28
+ # @option options [Integer] :page
29
+ # Page number.
30
+ # @option options [Integer] :per_page
31
+ # Number of items per page. Defaults to 30.
32
+ #
33
+ def activity_comments(id_or_options, options = {}, &block)
34
+ id, options = parse_args(id_or_options, options)
35
+ paginate "activities/#{id}/comments", options, Strava::Models::Comment, &block
36
+ end
37
+
38
+ #
39
+ # List activity kudoers.
40
+ #
41
+ # @option options [String] :id
42
+ # Activity id.
43
+ # @option options [Integer] :page
44
+ # Page number.
45
+ # @option options [Integer] :per_page
46
+ # Number of items per page. Defaults to 30.
47
+ #
48
+ def activity_kudos(id_or_options, options = {}, &block)
49
+ id, options = parse_args(id_or_options, options)
50
+ paginate "activities/#{id}/kudos", options, Strava::Models::Athlete, &block
51
+ end
52
+
53
+ #
54
+ # Get activity laps.
55
+ #
56
+ # @option options [String] :id
57
+ # Activity id.
58
+ #
59
+ def activity_laps(id_or_options, options = {})
60
+ id, options = parse_args(id_or_options, options)
61
+ get("activities/#{id}/laps", options).map do |row|
62
+ Strava::Models::Lap.new(row)
63
+ end
64
+ end
65
+
66
+ #
67
+ # List logged-in athlete activities.
68
+ #
69
+ # @option options [Integer] :before
70
+ # An epoch timestamp to use for filtering activities that have taken place before a certain time.
71
+ # @option options [Integer] :after
72
+ # An epoch timestamp to use for filtering activities that have taken place after a certain time.
73
+ # @option options [Integer] :page
74
+ # Page number.
75
+ # @option options [Integer] :per_page
76
+ # Number of items per page. Defaults to 30.
77
+ #
78
+ def athlete_activities(options = {}, &block)
79
+ paginate 'athlete/activities', options, Strava::Models::Activity, &block
80
+ end
81
+
82
+ #
83
+ # Get activity zones.
84
+ #
85
+ # @option options [String] :id
86
+ # Activity id.
87
+ #
88
+ def activity_zones(id_or_options, options = {})
89
+ id, options = parse_args(id_or_options, options)
90
+ get("activities/#{id}/zones", options).map do |row|
91
+ Strava::Models::ActivityZone.new(row)
92
+ end
93
+ end
94
+
95
+ #
96
+ # Update an activity.
97
+ #
98
+ # @option options [String] :id
99
+ # Activity id.
100
+ # @option options [Boolean] :commute
101
+ # Whether this activity is a commute.
102
+ # @option options [Boolean] :trainer
103
+ # Whether this activity was recorded on a training machine.
104
+ # @option options [String] :description
105
+ # The description of the activity.
106
+ # @option options [String] :name
107
+ # The name of the activity.
108
+ # @option options [String] :type
109
+ # Activity type.
110
+ # @option options [String] :gear_id
111
+ # Identifier for the gear associated with the activity. Specifying "none" clears gear from activity.
112
+ #
113
+ def update_activity(id_or_options, options = {})
114
+ id, options = parse_args(id_or_options, options)
115
+ Strava::Models::Activity.new(put("activities/#{id}", options))
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,42 @@
1
+ module Strava
2
+ module Api
3
+ module Endpoints
4
+ module Athletes
5
+ #
6
+ # Returns the currently authenticated athlete.
7
+ #
8
+ def athlete
9
+ Strava::Models::Athlete.new(get('athlete'))
10
+ end
11
+
12
+ #
13
+ # Returns the the authenticated athlete's heart rate and power zones.
14
+ #
15
+ def athlete_zones(options = {})
16
+ Strava::Models::Zones.new(get('athlete/zones', options))
17
+ end
18
+
19
+ #
20
+ # Returns the activity stats of an athlete.
21
+ #
22
+ # @option options [String] :id
23
+ # Athlete id.
24
+ #
25
+ def athlete_stats(id_or_options, options = {})
26
+ id, options = parse_args(id_or_options, options)
27
+ Strava::Models::ActivityStats.new(get("athletes/#{id}/stats", options))
28
+ end
29
+
30
+ #
31
+ # Update the currently authenticated athlete.
32
+ #
33
+ # @option options [Float] :weight
34
+ # The weight of the athlete in kilograms.
35
+ #
36
+ def update_athlete(options = {})
37
+ Strava::Models::Athlete.new(put('athlete', options))
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,75 @@
1
+ module Strava
2
+ module Api
3
+ module Endpoints
4
+ module Clubs
5
+ #
6
+ # List club activities.
7
+ #
8
+ # @option options [String] :id
9
+ # Club id.
10
+ # @option options [Integer] :page
11
+ # Page number.
12
+ # @option options [Integer] :per_page
13
+ # Number of items per page. Defaults to 30.
14
+ #
15
+ def club_activities(id_or_options, options = {}, &block)
16
+ id, options = parse_args(id_or_options, options)
17
+ paginate "clubs/#{id}/activities", options, Strava::Models::Activity, &block
18
+ end
19
+
20
+ #
21
+ # List club administrators.
22
+ #
23
+ # @option options [String] :id
24
+ # Club id.
25
+ # @option options [Integer] :page
26
+ # Page number.
27
+ # @option options [Integer] :per_page
28
+ # Number of items per page. Defaults to 30.
29
+ #
30
+ def club_admins(id_or_options, options = {}, &block)
31
+ id, options = parse_args(id_or_options, options)
32
+ paginate "clubs/#{id}/admins", options, Strava::Models::ClubAdmin, &block
33
+ end
34
+
35
+ #
36
+ # Get club.
37
+ #
38
+ # @option options [String] :id
39
+ # Club id.
40
+ #
41
+ def club(id_or_options, options = {})
42
+ id, options = parse_args(id_or_options, options)
43
+ Strava::Models::Club.new(get("clubs/#{id}", options))
44
+ end
45
+
46
+ #
47
+ # List club members.
48
+ #
49
+ # @option options [String] :id
50
+ # Club id.
51
+ # @option options [Integer] :page
52
+ # Page number.
53
+ # @option options [Integer] :per_page
54
+ # Number of items per page. Defaults to 30.
55
+ #
56
+ def club_members(id_or_options, options = {}, &block)
57
+ id, options = parse_args(id_or_options, options)
58
+ paginate "clubs/#{id}/members", options, Strava::Models::ClubMember, &block
59
+ end
60
+
61
+ #
62
+ # List logged-in athlete clubs.
63
+ #
64
+ # @option options [Integer] :page
65
+ # Page number.
66
+ # @option options [Integer] :per_page
67
+ # Number of items per page. Defaults to 30.
68
+ #
69
+ def athlete_clubs(options = {}, &block)
70
+ paginate 'athlete/clubs', options, Strava::Models::Club, &block
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end