Class: Strava::Api::Ratelimit
- Inherits:
-
Object
- Object
- Strava::Api::Ratelimit
- Defined in:
- lib/strava/api/ratelimit.rb
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
A new instance of Ratelimit.
-
#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
Returns a new instance of Ratelimit.
6 7 8 9 10 |
# File 'lib/strava/api/ratelimit.rb', line 6 def initialize(response) @response = response @headers = response.headers @body = response.body end |
Instance Method Details
#exceeded ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/strava/api/ratelimit.rb', line 12 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
22 23 24 |
# File 'lib/strava/api/ratelimit.rb', line 22 def exceeded? !!exceeded end |
#fifteen_minutes ⇒ NilClass, Integer
fifteen minute ratelimit
100 101 102 |
# File 'lib/strava/api/ratelimit.rb', line 100 def fifteen_minutes limit? ? extract_ratelimit!(limit).first : nil end |
#fifteen_minutes_remaining ⇒ NilClass, Integer
fifteen minute ratelimit remaining
140 141 142 143 144 |
# File 'lib/strava/api/ratelimit.rb', line 140 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
120 121 122 |
# File 'lib/strava/api/ratelimit.rb', line 120 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
75 76 77 |
# File 'lib/strava/api/ratelimit.rb', line 75 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.
60 61 62 |
# File 'lib/strava/api/ratelimit.rb', line 60 def limit? @headers.key?('x-ratelimit-limit') end |
#to_h ⇒ Hash
represents all valid ratelimits in a Hash
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/strava/api/ratelimit.rb', line 37 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
26 27 28 29 30 |
# File 'lib/strava/api/ratelimit.rb', line 26 def to_s to_h.map do |k, v| "#{k}: #{v}" end.join(', ') end |
#total_day ⇒ NilClass, Integer
total day ratelimit
110 111 112 |
# File 'lib/strava/api/ratelimit.rb', line 110 def total_day limit? ? extract_ratelimit!(limit).last : nil end |
#total_day_remaining ⇒ NilClass, Integer
total day ratelimit remaining
152 153 154 155 156 |
# File 'lib/strava/api/ratelimit.rb', line 152 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
130 131 132 |
# File 'lib/strava/api/ratelimit.rb', line 130 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
90 91 92 |
# File 'lib/strava/api/ratelimit.rb', line 90 def usage @headers['x-ratelimit-usage'] end |