Class: Strava::Api::Ratelimit
- Inherits:
-
Object
- Object
- Strava::Api::Ratelimit
- Defined in:
- lib/strava/api/ratelimit.rb
Overview
Handles Strava API rate limit information from HTTP responses.
Strava enforces rate limits on API requests with two windows:
- 15-minute window: 600 requests per 15 minutes (short-term limit)
- Daily window: 30,000 requests per day (long-term limit)
This class extracts and provides easy access to rate limit data from HTTP response headers.
Instance Method Summary collapse
- #exceeded ⇒ Object
- #exceeded? ⇒ Boolean
-
#fifteen_minutes ⇒ NilClass, Integer
fifteen minute ratelimit.
-
#fifteen_minutes_remaining ⇒ NilClass, Integer
fifteen minute ratelimit remaining.
-
#fifteen_minutes_usage ⇒ NilClass, Integer
fifteen minute ratelimit used.
-
#initialize(response) ⇒ Ratelimit
constructor
Initialize a new Ratelimit instance from an HTTP response.
-
#limit ⇒ String
returns a string containing the ratelimit for 15 minutes before the comma and the daily ratelimit after the comma.
-
#limit? ⇒ TrueClass
checks for presence of the header, as it is i.e.
-
#to_h ⇒ Hash
represents all valid ratelimits in a Hash.
- #to_s ⇒ Object
-
#total_day ⇒ NilClass, Integer
total day ratelimit.
-
#total_day_remaining ⇒ NilClass, Integer
total day ratelimit remaining.
-
#total_day_usage ⇒ NilClass, Integer
total day ratelimit used.
-
#usage ⇒ String
returns a string containing the used ratelimit for 15 minutes before the comma and the daily ratelimit after the comma.
Constructor Details
#initialize(response) ⇒ Ratelimit
Initialize a new Ratelimit instance from an HTTP response.
40 41 42 43 44 |
# File 'lib/strava/api/ratelimit.rb', line 40 def initialize(response) @response = response @headers = response.headers @body = response.body end |
Instance Method Details
#exceeded ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/strava/api/ratelimit.rb', line 46 def exceeded return false unless limit? @exceeded ||= if fifteen_minutes_remaining && fifteen_minutes_remaining <= 0 { fifteen_minutes_remaining: fifteen_minutes_remaining } elsif total_day_remaining && total_day_remaining <= 0 { total_day_remaining: total_day_remaining } end end |
#exceeded? ⇒ Boolean
56 57 58 |
# File 'lib/strava/api/ratelimit.rb', line 56 def exceeded? !!exceeded end |
#fifteen_minutes ⇒ NilClass, Integer
fifteen minute ratelimit
134 135 136 |
# File 'lib/strava/api/ratelimit.rb', line 134 def fifteen_minutes limit? ? extract_ratelimit!(limit).first : nil end |
#fifteen_minutes_remaining ⇒ NilClass, Integer
fifteen minute ratelimit remaining
174 175 176 177 178 |
# File 'lib/strava/api/ratelimit.rb', line 174 def fifteen_minutes_remaining return nil unless fifteen_minutes && fifteen_minutes_usage fifteen_minutes - fifteen_minutes_usage end |
#fifteen_minutes_usage ⇒ NilClass, Integer
fifteen minute ratelimit used
154 155 156 |
# File 'lib/strava/api/ratelimit.rb', line 154 def fifteen_minutes_usage limit? ? extract_ratelimit!(usage).first : nil end |
#limit ⇒ String
returns a string containing the ratelimit for 15 minutes before the comma and the daily ratelimit after the comma
109 110 111 |
# File 'lib/strava/api/ratelimit.rb', line 109 def limit @headers['x-ratelimit-limit'] end |
#limit? ⇒ TrueClass
checks for presence of the header, as it is i.e. not included when asking for a token.
94 95 96 |
# File 'lib/strava/api/ratelimit.rb', line 94 def limit? @headers.key?('x-ratelimit-limit') end |
#to_h ⇒ Hash
represents all valid ratelimits in a Hash
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/strava/api/ratelimit.rb', line 71 def to_h if limit? { limit: limit, usage: usage, total_day: total_day, total_day_usage: total_day_usage, total_day_remaining: total_day_remaining, fifteen_minutes: fifteen_minutes, fifteen_minutes_usage: fifteen_minutes_usage, fifteen_minutes_remaining: fifteen_minutes_remaining } else {} end end |
#to_s ⇒ Object
60 61 62 63 64 |
# File 'lib/strava/api/ratelimit.rb', line 60 def to_s to_h.map do |k, v| "#{k}: #{v}" end.join(', ') end |
#total_day ⇒ NilClass, Integer
total day ratelimit
144 145 146 |
# File 'lib/strava/api/ratelimit.rb', line 144 def total_day limit? ? extract_ratelimit!(limit).last : nil end |
#total_day_remaining ⇒ NilClass, Integer
total day ratelimit remaining
186 187 188 189 190 |
# File 'lib/strava/api/ratelimit.rb', line 186 def total_day_remaining return nil unless total_day && total_day_usage total_day - total_day_usage end |
#total_day_usage ⇒ NilClass, Integer
total day ratelimit used
164 165 166 |
# File 'lib/strava/api/ratelimit.rb', line 164 def total_day_usage limit? ? extract_ratelimit!(usage).last : nil end |
#usage ⇒ String
returns a string containing the used ratelimit for 15 minutes before the comma and the daily ratelimit after the comma
124 125 126 |
# File 'lib/strava/api/ratelimit.rb', line 124 def usage @headers['x-ratelimit-usage'] end |