Class: Strava::Api::Ratelimit

Inherits:
Object
  • Object
show all
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.

Examples:

Checking rate limits after an API call

activity = client.activity(1234567890)
ratelimit = activity.http_response.ratelimit

puts "15-minute limit: #{ratelimit.fifteen_minutes_limit}"
puts "15-minute usage: #{ratelimit.fifteen_minutes_usage}"
puts "15-minute remaining: #{ratelimit.fifteen_minutes_remaining}"

puts "Daily limit: #{ratelimit.total_day}"
puts "Daily usage: #{ratelimit.total_day_usage}"
puts "Daily remaining: #{ratelimit.total_day_remaining}"

if ratelimit.exceeded?
  puts "Rate limit exceeded!"
end

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Ratelimit

Initialize a new Ratelimit instance from an HTTP response.

Parameters:

  • response (Faraday::Response)

    HTTP response containing rate limit headers



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

#exceededObject



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

Returns:

  • (Boolean)


56
57
58
# File 'lib/strava/api/ratelimit.rb', line 56

def exceeded?
  !!exceeded
end

#fifteen_minutesNilClass, Integer

fifteen minute ratelimit

Returns:

  • (NilClass)

    if no ratelimit in http headers

  • (Integer)

    representing the ratelimit



134
135
136
# File 'lib/strava/api/ratelimit.rb', line 134

def fifteen_minutes
  limit? ? extract_ratelimit!(limit).first : nil
end

#fifteen_minutes_remainingNilClass, Integer

fifteen minute ratelimit remaining

Returns:

  • (NilClass)

    if no ratelimit in http headers

  • (Integer)

    representing the ratelimit



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_usageNilClass, Integer

fifteen minute ratelimit used

Returns:

  • (NilClass)

    if no ratelimit in http headers

  • (Integer)

    representing the ratelimit



154
155
156
# File 'lib/strava/api/ratelimit.rb', line 154

def fifteen_minutes_usage
  limit? ? extract_ratelimit!(usage).first : nil
end

#limitString

returns a string containing the ratelimit for 15 minutes before the comma and the daily ratelimit after the comma

Examples:

```ruby
"600,30000"
```

Returns:

  • (String)

    of the ratelimits for 15 minutes and per day separated by 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.

Returns:

  • (TrueClass)


94
95
96
# File 'lib/strava/api/ratelimit.rb', line 94

def limit?
  @headers.key?('x-ratelimit-limit')
end

#to_hHash

represents all valid ratelimits in a Hash

Returns:

  • (Hash)

    of ratelimits



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_sObject



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_dayNilClass, Integer

total day ratelimit

Returns:

  • (NilClass)

    if no ratelimit in http headers

  • (Integer)

    representing the ratelimit



144
145
146
# File 'lib/strava/api/ratelimit.rb', line 144

def total_day
  limit? ? extract_ratelimit!(limit).last : nil
end

#total_day_remainingNilClass, Integer

total day ratelimit remaining

Returns:

  • (NilClass)

    if no ratelimit in http headers

  • (Integer)

    representing the ratelimit



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_usageNilClass, Integer

total day ratelimit used

Returns:

  • (NilClass)

    if no ratelimit in http headers

  • (Integer)

    representing the ratelimit



164
165
166
# File 'lib/strava/api/ratelimit.rb', line 164

def total_day_usage
  limit? ? extract_ratelimit!(usage).last : nil
end

#usageString

returns a string containing the used ratelimit for 15 minutes before the comma and the daily ratelimit after the comma

Examples:

```ruby
"333,11337"
```

Returns:

  • (String)

    of the used ratelimits for 15 minutes and per day separated by comma



124
125
126
# File 'lib/strava/api/ratelimit.rb', line 124

def usage
  @headers['x-ratelimit-usage']
end