strava-ruby-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +6 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +15 -0
  5. data/.rubocop_todo.yml +47 -0
  6. data/.travis.yml +9 -0
  7. data/CHANGELOG.md +3 -0
  8. data/CONTRIBUTING.md +125 -0
  9. data/Dangerfile +2 -0
  10. data/Gemfile +15 -0
  11. data/LICENSE.md +22 -0
  12. data/README.md +231 -0
  13. data/RELEASING.md +61 -0
  14. data/Rakefile +17 -0
  15. data/bin/oauth-token.rb +28 -0
  16. data/lib/strava-ruby-client.rb +30 -0
  17. data/lib/strava/api/client.rb +38 -0
  18. data/lib/strava/api/config.rb +31 -0
  19. data/lib/strava/errors/fault.rb +13 -0
  20. data/lib/strava/logger.rb +13 -0
  21. data/lib/strava/models/activity.rb +57 -0
  22. data/lib/strava/models/athlete.rb +25 -0
  23. data/lib/strava/models/map.rb +9 -0
  24. data/lib/strava/models/model.rb +5 -0
  25. data/lib/strava/models/token.rb +12 -0
  26. data/lib/strava/oauth/client.rb +70 -0
  27. data/lib/strava/oauth/config.rb +33 -0
  28. data/lib/strava/version.rb +3 -0
  29. data/lib/strava/web/client.rb +31 -0
  30. data/lib/strava/web/config.rb +41 -0
  31. data/lib/strava/web/connection.rb +35 -0
  32. data/lib/strava/web/raise_error.rb +29 -0
  33. data/lib/strava/web/request.rb +38 -0
  34. data/spec/fixtures/strava/client_athlete.yml +59 -0
  35. data/spec/fixtures/strava/client_athlete_activities.yml +121 -0
  36. data/spec/fixtures/strava/oauth_token_authorization_code.yml +53 -0
  37. data/spec/fixtures/strava/oauth_token_invalid_client.yml +50 -0
  38. data/spec/fixtures/strava/oauth_token_invalid_code.yml +50 -0
  39. data/spec/fixtures/strava/oauth_token_refresh_token.yml +52 -0
  40. data/spec/spec_helper.rb +10 -0
  41. data/spec/strava/api/client_spec.rb +36 -0
  42. data/spec/strava/api/config_spec.rb +22 -0
  43. data/spec/strava/oauth/client_spec.rb +120 -0
  44. data/spec/strava/oauth/config_spec.rb +24 -0
  45. data/spec/strava/version_spec.rb +7 -0
  46. data/spec/strava/web/client_spec.rb +9 -0
  47. data/spec/strava/web/config_spec.rb +4 -0
  48. data/spec/support/shared/it_behaves_like_web_client.rb +131 -0
  49. data/spec/support/vcr.rb +12 -0
  50. data/strava-ruby-client.gemspec +21 -0
  51. metadata +150 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bd3998f1cfc79f2b07fc5ef6b9d25f8717e411c7d6a9066d6eb9e77154ea4cd1
4
+ data.tar.gz: 82adf5a179351b1d3b410c72710d5ce945bf5f303398e5c21e482b34ce294d95
5
+ SHA512:
6
+ metadata.gz: 05fe3b3d099ca87559a99d45b33c160a3cf378a094181b462d5a33e43dd871e451e91eba8ce3b119759206865185f28e51a24bed6be77729fc12e2d886132e1c
7
+ data.tar.gz: bb2b409a5e9d0f1b762f230959721c0efb23e2cfdded64a25df02a618b6f8083134854905d0a728e2f004d01ad3324c82749c75bd8c5082a2c520595afb9c672
@@ -0,0 +1,6 @@
1
+ .env
2
+ pkg
3
+ Gemfile.lock
4
+ .DS_Store
5
+ .bundle
6
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,15 @@
1
+ AllCops:
2
+ Exclude:
3
+ - vendor/**/*
4
+
5
+ Naming/MethodName:
6
+ Enabled: false
7
+
8
+ Naming/FileName:
9
+ Exclude:
10
+ - 'lib/strava-ruby-client.rb'
11
+
12
+ Style/ModuleFunction:
13
+ EnforcedStyle: extend_self
14
+
15
+ inherit_from: .rubocop_todo.yml
@@ -0,0 +1,47 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2018-11-23 11:26:24 -0500 using RuboCop version 0.60.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ Metrics/AbcSize:
11
+ Max: 30
12
+
13
+ # Offense count: 7
14
+ # Configuration parameters: CountComments, ExcludedMethods.
15
+ # ExcludedMethods: refine
16
+ Metrics/BlockLength:
17
+ Max: 127
18
+
19
+ # Offense count: 1
20
+ Metrics/CyclomaticComplexity:
21
+ Max: 9
22
+
23
+ # Offense count: 2
24
+ # Configuration parameters: CountComments, ExcludedMethods.
25
+ Metrics/MethodLength:
26
+ Max: 18
27
+
28
+ # Offense count: 1
29
+ Metrics/PerceivedComplexity:
30
+ Max: 9
31
+
32
+ # Offense count: 1
33
+ # Configuration parameters: EnforcedStyleForLeadingUnderscores.
34
+ # SupportedStylesForLeadingUnderscores: disallowed, required, optional
35
+ Naming/MemoizedInstanceVariableName:
36
+ Exclude:
37
+ - 'lib/strava/logger.rb'
38
+
39
+ # Offense count: 19
40
+ Style/Documentation:
41
+ Enabled: false
42
+
43
+ # Offense count: 25
44
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
45
+ # URISchemes: http, https
46
+ Metrics/LineLength:
47
+ Max: 112
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+
3
+ cache: bundler
4
+
5
+ rvm:
6
+ - 2.5.1
7
+
8
+ before_script:
9
+ - bundle exec danger
@@ -0,0 +1,3 @@
1
+ ### 0.1.0 (2018/11/23)
2
+
3
+ * Initial public release, OAuth, current Athlete and Athlete Activities - [@dblock](https://github.com/dblock).
@@ -0,0 +1,125 @@
1
+ # Contributing to Strava-Ruby-Client
2
+
3
+ This project is work of [many contributors](https://github.com/dblock/strava-ruby-client/graphs/contributors).
4
+
5
+ You're encouraged to submit [pull requests](https://github.com/dblock/strava-ruby-client/pulls), [propose features and discuss issues](https://github.com/dblock/strava-ruby-client/issues).
6
+
7
+ In the examples below, substitute your Github username for `contributor` in URLs.
8
+
9
+ ### Fork the Project
10
+
11
+ Fork the [project on Github](https://github.com/dblock/strava-ruby-client) and check out your copy.
12
+
13
+ ```
14
+ git clone https://github.com/contributor/strava-ruby-client.git
15
+ cd strava-ruby-client
16
+ git remote add upstream https://github.com/dblock/strava-ruby-client.git
17
+ ```
18
+
19
+ ### Bundle Install and Test
20
+
21
+ Ensure that you can build the project and run tests.
22
+
23
+ ```
24
+ bundle install
25
+ bundle exec rake
26
+ ```
27
+
28
+ ## Contribute Code
29
+
30
+ ### Create a Topic Branch
31
+
32
+ Make sure your fork is up-to-date and create a topic branch for your feature or bug fix.
33
+
34
+ ```
35
+ git checkout master
36
+ git pull upstream master
37
+ git checkout -b my-feature-branch
38
+ ```
39
+
40
+ ### Write Tests
41
+
42
+ Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build. Add tests to [spec](spec).
43
+
44
+ We definitely appreciate pull requests that highlight or reproduce a problem, even without a fix.
45
+
46
+ ### Write Code
47
+
48
+ Implement your feature or bug fix.
49
+
50
+ Ruby style is enforced with [Rubocop](https://github.com/bbatsov/rubocop). Run `bundle exec rubocop` and fix any style issues highlighted, auto-correct issues when possible with `bundle exec rubocop -a`. To silence generally ingored issues, including line lengths or code complexity metrics, run `bundle exec rubocop --auto-gen-config`.
51
+
52
+ Make sure that `bundle exec rake` completes without errors.
53
+
54
+ ### Write Documentation
55
+
56
+ Document any external behavior in the [README](README.md).
57
+
58
+ ### Update Changelog
59
+
60
+ Add a line to [CHANGELOG](CHANGELOG.md) under *Next Release*. Don't remove *Your contribution here*.
61
+
62
+ Make it look like every other line, including a link to the issue being fixed, your name and link to your Github account.
63
+
64
+ ### Commit Changes
65
+
66
+ Make sure git knows your name and email address:
67
+
68
+ ```
69
+ git config --global user.name "Your Name"
70
+ git config --global user.email "contributor@example.com"
71
+ ```
72
+
73
+ Writing good commit logs is important. A commit log should describe what changed and why.
74
+
75
+ ```
76
+ git add ...
77
+ git commit
78
+ ```
79
+
80
+ ### Push
81
+
82
+ ```
83
+ git push origin my-feature-branch
84
+ ```
85
+
86
+ ### Make a Pull Request
87
+
88
+ Go to https://github.com/contributor/strava-ruby-client and select your feature branch. Click the 'Pull Request' button and fill out the form. Pull requests are usually reviewed within a few days.
89
+
90
+ ### Update CHANGELOG Again
91
+
92
+ Update the [CHANGELOG](CHANGELOG.md) with the pull request number. A typical entry looks as follows.
93
+
94
+ ```
95
+ * [#123](https://github.com/dblock/strava-ruby-client/pull/123): Reticulated splines - [@contributor](https://github.com/contributor).
96
+ ```
97
+
98
+ Amend your previous commit and force push the changes.
99
+
100
+ ```
101
+ git commit --amend
102
+ git push origin my-feature-branch -f
103
+ ```
104
+
105
+ ### Rebase
106
+
107
+ If you've been working on a change for a while, rebase with upstream/master.
108
+
109
+ ```
110
+ git fetch upstream
111
+ git rebase upstream/master
112
+ git push origin my-feature-branch -f
113
+ ```
114
+
115
+ ### Check on Your Pull Request
116
+
117
+ Go back to your pull request after a few minutes and see whether it passed muster with Travis-CI. Everything should look green, otherwise fix issues and amend your commit as described above.
118
+
119
+ ### Be Patient
120
+
121
+ It's likely that your change will not be merged and that the nitpicky maintainers will ask you to do more, or fix seemingly benign problems. Hang on there!
122
+
123
+ ## Thank You
124
+
125
+ Please do know that we really appreciate and value your time and work. We love you, really.
@@ -0,0 +1,2 @@
1
+ toc.check
2
+ changelog.check
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem 'danger-changelog', '~> 0.3.0'
7
+ gem 'danger-toc', '~> 0.1'
8
+ gem 'dotenv'
9
+ gem 'pry'
10
+ gem 'rake', '~> 10'
11
+ gem 'rspec'
12
+ gem 'rubocop', '0.60.0'
13
+ gem 'vcr'
14
+ gem 'webmock'
15
+ end
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Daniel Doubrovkine and Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,231 @@
1
+ Strava Ruby Client
2
+ ==================
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/strava-ruby-client.svg)](https://badge.fury.io/rb/strava-ruby-client)
5
+ [![Build Status](https://travis-ci.org/dblock/strava-ruby-client.svg?branch=master)](https://travis-ci.org/dblock/strava-ruby-client)
6
+
7
+ A newer Ruby client for the [Strava API v3](https://developers.strava.com).
8
+
9
+ Unlike [strava-api-v3](https://github.com/jaredholdcroft/strava-api-v3) provides a first class interface to Strava models and more consistent error handling.
10
+
11
+ # Table of Contents
12
+
13
+ - [Installation](#installation)
14
+ - [Usage](#usage)
15
+ - [API](#api)
16
+ - [Athlete](#athlete)
17
+ - [Athlete Activities](#athlete-activities)
18
+ - [OAuth](#oauth)
19
+ - [Configuration](#configuration)
20
+ - [Web Client Options](#web-client-options)
21
+ - [API Client Options](#api-client-options)
22
+ - [OAuth Client Options](#oauth-client-options)
23
+ - [Errors](#errors)
24
+ - [Tools](#tools)
25
+ - [OAuth Token](#oauth-token)
26
+ - [Contributing](#contributing)
27
+ - [Copyright and License](#copyright-and-license)
28
+
29
+ ## Installation
30
+
31
+ Add to Gemfile.
32
+
33
+ ```
34
+ gem 'strava-ruby-client'
35
+ ```
36
+
37
+ Run `bundle install`.
38
+
39
+ ## Usage
40
+
41
+ ### API
42
+
43
+ Use an access token obtained from [My API Application](https://www.strava.com/settings/api) in the Strava UI, the [oauth-token tool](#oauth-token) or the [OAuth Workflow](#oauth) in your application.
44
+
45
+ ```ruby
46
+ client = Strava::Api::Client.new(
47
+ access_token: "12345678987654321"
48
+ )
49
+ ```
50
+
51
+ #### Athlete
52
+
53
+ Get currently logged-in athlete.
54
+
55
+ ```ruby
56
+ client.athlete # => Strava::Models::Athlete
57
+ ```
58
+
59
+ See [Strava::Models::Athlete](lib/strava/models/athlete.rb) for all available properties.
60
+
61
+ #### Athlete Activities
62
+
63
+ Get currently logged-in athlete activities.
64
+
65
+ ```ruby
66
+ client.athlete_activities # => Array[Strava::Models::Activity]
67
+ ```
68
+
69
+ See [Strava::Models::Activity](lib/strava/models/activity.rb) for all available properties.
70
+
71
+ ### OAuth
72
+
73
+ Obtain a redirect URL.
74
+
75
+ ```ruby
76
+ client = Strava::OAuth::Client.new(
77
+ client_id: "12345",
78
+ client_secret: "12345678987654321"
79
+ )
80
+
81
+ redirect_url = client.authorize_url(
82
+ redirect_uri: 'https://example.com/oauth',
83
+ approval_prompt: 'force',
84
+ response_type: 'code',
85
+ scope: 'activity:read_all',
86
+ state: 'magic'
87
+ )
88
+ ```
89
+
90
+ Once the user is redirected to your application, perform a token exchange to obtain a refresh and access token.
91
+
92
+ ```ruby
93
+ response = client.oauth_token(code: '1234556789901234567890')
94
+
95
+ response # => Strava::Models::Token
96
+
97
+ response.access_token # access token
98
+ response.refresh_token # refresh token
99
+ response.expires_at # timestamp when the access token expires
100
+ response.athlete # => Strava::Models::Athlete
101
+ ```
102
+
103
+ See [Strava authentication documentation](https://developers.strava.com/docs/authentication/), [Strava::Models::Token](lib/strava/models/token.rb) and [Strava::Models::Athlete](lib/strava/models/athlete.rb) for all available properties in the response.
104
+
105
+ If the access token is expired, refresh it before making any requests. You will get back all new tokens.
106
+
107
+ ```ruby
108
+ response = client.oauth_token(
109
+ refresh_token: '...',
110
+ grant_type: 'refresh_token'
111
+ )
112
+
113
+ response.access_token # new access token
114
+ response.refresh_token # new refresh token
115
+ response.expires_at # new timestamp when the access token expires
116
+ ```
117
+
118
+ ## Configuration
119
+
120
+ ### Web Client Options
121
+
122
+ You can configure web client options used in the OAuth and API clients, globally.
123
+
124
+ ```ruby
125
+ Strava::Web::Client.configure do |config|
126
+ config.user_agent = 'Strava Ruby Client/1.0'
127
+ end
128
+ ```
129
+
130
+ The following settings are supported.
131
+
132
+ setting | description
133
+ --------------------|------------
134
+ user_agent | User-agent, defaults to _Strava Ruby Client/version_.
135
+ proxy | Optional HTTP proxy.
136
+ ca_path | Optional SSL certificates path.
137
+ ca_file | Optional SSL certificates file.
138
+ logger | Optional `Logger` instance that logs HTTP requests.
139
+ timeout | Optional open/read timeout in seconds.
140
+ open_timeout | Optional connection open timeout in seconds.
141
+
142
+ ### API Client Options
143
+
144
+ The API client inherits web client options and provides additional application configuration. These can be configured globally or for a client instance.
145
+
146
+ ```ruby
147
+ Strava::API.configure do |config|
148
+ config.access_token = "..." # Strava access token
149
+ end
150
+ ```
151
+
152
+ ```ruby
153
+ client = Strava::API::Client.new(
154
+ access_token: "...",
155
+ user_agent: "..."
156
+ )
157
+ ```
158
+
159
+ The following settings are supported.
160
+
161
+ setting | description
162
+ --------------------|------------
163
+ access_token | Access token to pass in the `Authorization` header.
164
+ endpoint | Defaults to `https://www.strava.com/api/v3`.
165
+
166
+ ### OAuth Client Options
167
+
168
+ The OAuth client inherits web client options and provides additional application configuration. These can be configured globally or for a client instance.
169
+
170
+ ```ruby
171
+ Strava::OAuth.configure do |config|
172
+ config.client_id = "..." # Strava client ID
173
+ config.client_secret = "..." # Strava client secret
174
+ end
175
+ ```
176
+
177
+ ```ruby
178
+ client = Strava::OAuth::Client.new(
179
+ client_id: "...",
180
+ client_secret: "...",
181
+ user_agent: "..."
182
+ )
183
+ ```
184
+
185
+ The following settings are supported.
186
+
187
+ setting | description
188
+ --------------------|------------
189
+ client_id | Application client ID.
190
+ client_secret | Application client secret.
191
+ endpoint | Defaults to `https://www.strava.com/oauth`.
192
+
193
+ ## Errors
194
+
195
+ All errors that return HTTP codes 400-600 result in either `Faraday::Error::ResourceNotFound`, `Faraday::Error::ConnectionFailed` or [Strava::Errors::Fault](lib/strava/errors/fault.rb) exceptions.
196
+
197
+ ```ruby
198
+ begin
199
+ client.oauth_token(code: 'invalid')
200
+ rescue Strava::Errors::Fault => e
201
+ e.message # => Bad Request
202
+ e.errors # => [{ 'code' => 'invalid', 'field' => 'code', 'resource' => 'RequestToken' }]
203
+ end
204
+ ```
205
+
206
+ ## Tools
207
+
208
+ ### OAuth Token
209
+
210
+ Use [bin/oauth-token](bin/outh-token) to obtain a token from the command-line.
211
+
212
+ ```bash
213
+ $ STRAVA_CLIENT_ID=... STRAVA_CLIENT_SECRET=... bundle exec bin/oauth-token.rb
214
+
215
+ Opening browser at https://www.strava.com/oauth/authorize?...
216
+ Copy paste the code from the redirect URL: 1234556789901234567890
217
+ token_type: Bearer
218
+ refresh_token: 013612374123716234842346234
219
+ access_token: 7348562936591928461923619823
220
+ expires_at: 2018-11-23 16:25:52 -0500
221
+ ```
222
+
223
+ ## Contributing
224
+
225
+ See [CONTRIBUTING](CONTRIBUTING.md).
226
+
227
+ ## Copyright and License
228
+
229
+ Copyright (c) 2018, [Daniel Doubrovkine](https://twitter.com/dblockdotorg) and [Contributors](CHANGELOG.md).
230
+
231
+ This project is licensed under the [MIT License](LICENSE.md).