Class: Strava::Api::Ratelimit

Inherits:
Object
  • Object
show all
Defined in:
lib/strava/api/ratelimit.rb

Instance Method Summary collapse

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

#exceededObject



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

Returns:

  • (Boolean)


22
23
24
# File 'lib/strava/api/ratelimit.rb', line 22

def exceeded?
  !!exceeded
end

#fifteen_minutesNilClass, Integer

fifteen minute ratelimit

Returns:

  • (NilClass)

    if no ratelimit in http headers

  • (Integer)

    representing the ratelimit



100
101
102
# File 'lib/strava/api/ratelimit.rb', line 100

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



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

fifteen minute ratelimit used

Returns:

  • (NilClass)

    if no ratelimit in http headers

  • (Integer)

    representing the ratelimit



120
121
122
# File 'lib/strava/api/ratelimit.rb', line 120

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



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.

Returns:

  • (TrueClass)


60
61
62
# File 'lib/strava/api/ratelimit.rb', line 60

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

#to_hHash

represents all valid ratelimits in a Hash

Returns:

  • (Hash)

    of ratelimits



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_sObject



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

total day ratelimit

Returns:

  • (NilClass)

    if no ratelimit in http headers

  • (Integer)

    representing the ratelimit



110
111
112
# File 'lib/strava/api/ratelimit.rb', line 110

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



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

total day ratelimit used

Returns:

  • (NilClass)

    if no ratelimit in http headers

  • (Integer)

    representing the ratelimit



130
131
132
# File 'lib/strava/api/ratelimit.rb', line 130

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



90
91
92
# File 'lib/strava/api/ratelimit.rb', line 90

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