Module: Football::Butler::Tier

Included in:
Football::Butler
Defined in:
lib/football/butler/tier.rb

Constant Summary collapse

COUNTER_RESET_SAFE_SECONDS =
5

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.available_minuteObject

Returns the value of attribute available_minute.



9
10
11
# File 'lib/football/butler/tier.rb', line 9

def available_minute
  @available_minute
end

.counter_resetObject

Returns the value of attribute counter_reset.



9
10
11
# File 'lib/football/butler/tier.rb', line 9

def counter_reset
  @counter_reset
end

.last_requestObject

Returns the value of attribute last_request.



9
10
11
# File 'lib/football/butler/tier.rb', line 9

def last_request
  @last_request
end

.sleep_secondsObject

Returns the value of attribute sleep_seconds.



9
10
11
# File 'lib/football/butler/tier.rb', line 9

def sleep_seconds
  @sleep_seconds
end

.total_requestsObject

Returns the value of attribute total_requests.



9
10
11
# File 'lib/football/butler/tier.rb', line 9

def total_requests
  @total_requests
end

Class Method Details

.available_minute?(response, key_remaining_minute) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/football/butler/tier.rb', line 41

def available_minute?(response, key_remaining_minute)
  response&.headers&.dig(key_remaining_minute)&.present?
end

.count_requestObject



67
68
69
# File 'lib/football/butler/tier.rb', line 67

def count_request
  @total_requests = @total_requests.is_a?(Integer) ? @total_requests + 1 : 1
end

.counter_reset?(response, key_reset) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/football/butler/tier.rb', line 45

def counter_reset?(response, key_reset)
  response&.headers&.dig(key_reset)&.present?
end

.get_sleep_secondsObject



49
50
51
52
53
54
55
56
57
# File 'lib/football/butler/tier.rb', line 49

def get_sleep_seconds
  seconds = @counter_reset.is_a?(Integer) ? @counter_reset : 60
  result  = COUNTER_RESET_SAFE_SECONDS + seconds

  @sleep_seconds = @sleep_seconds.is_a?(Integer) ?
                     @sleep_seconds + result : result

  result
end

.limit_exceeded?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/football/butler/tier.rb', line 59

def limit_exceeded?
  @available_minute == 0 && @last_request >= 1.minute.ago
end

.reset_sleep_secondsObject



71
72
73
# File 'lib/football/butler/tier.rb', line 71

def reset_sleep_seconds
  @sleep_seconds = 0
end

.reset_total_requestsObject



63
64
65
# File 'lib/football/butler/tier.rb', line 63

def reset_total_requests
  @total_requests = 0
end

.set_from_response_headers(response) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/football/butler/tier.rb', line 11

def set_from_response_headers(response)
  case Configuration.api_name
  when :apifootball_com
    # n/a
  when :football_data_org
    if available_minute?(response, 'x-requests-available-minute') &&
      counter_reset?(response, 'x-requestcounter-reset')
      set_tier_from_response(
        response.headers['x-requests-available-minute'],
        response.headers['x-requestcounter-reset']
      )
    end
  when :api_football_com
    if available_minute?(response, 'x-ratelimit-remaining')
      set_tier_from_response(
        response.headers['x-ratelimit-remaining'],
        '60'
      )
    end
  end
end

.set_tier_from_response(available_minute, counter_reset) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/football/butler/tier.rb', line 33

def set_tier_from_response(available_minute, counter_reset)
  @available_minute = available_minute.to_i
  @counter_reset    = counter_reset.to_i
  @last_request     = Time.current

  true
end