Skip to content

Commit 22a3500

Browse files
authored
raises error when ratelimit exceeded (#71)
* fixes #11 * moves RatelimitError into namespace of other errors * adds docs
1 parent dd9b1f2 commit 22a3500

File tree

11 files changed

+312
-2
lines changed

11 files changed

+312
-2
lines changed

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ Metrics:
2222

2323
Layout/LineLength:
2424
Max: 256
25+
26+
Style/MultilineBlockChain:
27+
Exclude:
28+
- spec/**/*

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* [#62](https://github.com/dblock/strava-ruby-client/pull/68): Drops `Activity#type` attribute as it is being deprecated by Strava, dropping `Activity#type_emoji` with it - [@simonneutert](https://github.com/simonneutert).
44
* [#23](https://github.com/dblock/strava-ruby-client/pull/23): Failed uploads raise Strava::Errors::UploadError - [@ylecuyer](https://github.com/ylecuyer), [@simonneutert](https://github.com/simonneutert).
5+
* [#69](https://github.com/dblock/strava-ruby-client/pull/69): Raises `Strava::Api::RatelimitError`, when API ratelimit exceeded - [@simonneutert](https://github.com/simonneutert).
56
* Your contribution here.
67

78
### 1.0.0 (2022/12/29)

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Unlike other clients, including [strava-api-v3](https://github.com/jaredholdcrof
6464
- [Command Line OAuth Workflow](#command-line-oauth-workflow)
6565
- [Webhooks](#webhooks)
6666
- [Ratelimit](#ratelimit)
67+
- [Exceeded Ratelimit](#exceeded-ratelimit)
6768
- [Configuration](#configuration)
6869
- [Web Client Options](#web-client-options)
6970
- [API Client Options](#api-client-options)
@@ -969,6 +970,16 @@ You can access the Hash containing all limits by calling `to_h`.
969970
}
970971
```
971972

973+
#### Exceeded Ratelimit
974+
975+
Strava answers with HTTP status code 429, when ratelimits are exceeded. This will in return raise `Strava::Errors::RatelimitError`.
976+
977+
```ruby
978+
error.is_a?(Strava::Errors::RatelimitError) #=> true
979+
error.ratelimit.is_a?(Strava::Api::Ratelimit) #=> true
980+
# see Strava::Api::Ratelimit
981+
```
982+
972983
## Configuration
973984

974985
### Web Client Options

UPGRADING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Dropping `Activity` attribute `type` and `type_emoji` in favor of `sport_type` and `sport_type_emoji`. Creating or updating an `Activity` requires you to use `sport_type` instead of `type`, as refered in the [README](README.md#create-an-activity). For details visit the official [Strava docs: Create Activity](https://developers.strava.com/docs/reference/#api-Activities-createActivity) and the entry from June 15, 2022 in [Strava's V3 API Changelog](https://developers.strava.com/docs/changelog/).
66
- Uploading a file using `create_upload` requires you to check its process, using `client.updload('your-unique-upload-id')`. A successful upload does just mean, that the file was accepted. The Processing of the file is an independent process on Strava's side. From now on, `client.updload('your-unique-upload-id')` will raise `Strava::Errors::UploadError`, if Strava failed processing the file, e.g. it being a duplicate.
7+
- Exceeded Ratelimits (HTTP Status: 429) do now raise a customized Error `Strava::Errors::RatelimitError`. You can use the `Strava::Api::Ratelimit` object coming with the error, for further inspection of your current ratelimits.
78

89
### Upgrading to >= 1.0.0
910

lib/strava-ruby-client.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
require_relative 'strava/errors/fault'
2020
require_relative 'strava/errors/upload_error'
21+
require_relative 'strava/errors/ratelimit_error'
2122

2223
require_relative 'strava/models/mixins/time'
2324
require_relative 'strava/models/mixins/distance'

lib/strava/api/ratelimit.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ def initialize(response)
99
@body = response.body
1010
end
1111

12+
def exceeded
13+
return false unless limit?
14+
15+
@exceeded ||= if fifteen_minutes_remaining && fifteen_minutes_remaining <= 0
16+
{ fifteen_minutes_remaining: fifteen_minutes_remaining }
17+
elsif total_day_remaining && total_day_remaining <= 0
18+
{ total_day_remaining: total_day_remaining }
19+
end
20+
end
21+
22+
def exceeded?
23+
!!exceeded
24+
end
25+
1226
def to_s
1327
to_h.map do |k, v|
1428
"#{k}: #{v}"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
module Strava
4+
module Errors
5+
class RatelimitError < ::Faraday::ClientError
6+
attr_reader :http_response, :ratelimit, :error_message
7+
8+
def initialize(http_response, error_message = nil)
9+
@response = http_response.response
10+
@ratelimit = Strava::Api::Ratelimit.new(@response)
11+
@error_message = error_message || message
12+
super({
13+
status: http_response.status,
14+
headers: http_response.response_headers,
15+
body: http_response.body
16+
})
17+
end
18+
19+
def message
20+
response[:body]['message'] || super
21+
end
22+
23+
def headers
24+
response[:headers]
25+
end
26+
27+
def errors
28+
response[:body]['errors']
29+
end
30+
end
31+
end
32+
end

lib/strava/web/raise_response_error.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ def on_complete(env)
1212
when 407
1313
# mimic the behavior that we get with proxy requests with HTTPS
1414
raise Faraday::ConnectionFailed, %(407 "Proxy Authentication Required ")
15+
when 429
16+
raise Strava::Errors::RatelimitError.new(env, 'Too Many Requests')
1517
when CLIENT_ERROR_STATUSES
1618
raise Strava::Errors::Fault, response_values(env)
1719
end

0 commit comments

Comments
 (0)