-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
100 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,28 @@ | ||
require_relative "client_error" | ||
require_relative "../rate_limit" | ||
|
||
module X | ||
class TooManyRequests < ClientError | ||
def limit | ||
response["x-rate-limit-limit"].to_i | ||
end | ||
|
||
def remaining | ||
response["x-rate-limit-remaining"].to_i | ||
def rate_limits | ||
@rate_limits ||= limit_names.to_h do |limit| | ||
[limit, RateLimit.new(limit, response)] | ||
end | ||
end | ||
|
||
def reset_at | ||
Time.at(response["x-rate-limit-reset"].to_i) | ||
rate_limits.values.select { |limit| limit.remaining.zero? }.map(&:reset_at).max || Time.at(0) | ||
end | ||
|
||
def reset_in | ||
[(reset_at - Time.now).ceil, 0].max | ||
end | ||
|
||
def all_limits | ||
@all_limits ||= limits.to_h do |limit| | ||
[ | ||
limit, | ||
{ | ||
"limit" => response["x-#{limit}-limit"].to_i, | ||
"remaining" => response["x-#{limit}-remaining"].to_i, | ||
"reset_at" => Time.at(response["x-#{limit}-reset"].to_i) | ||
} | ||
] | ||
end | ||
end | ||
|
||
def next_available_at | ||
all_limits.values.select { |v| (v["remaining"]).zero? }.map { |v| v["reset_at"] }.max | ||
end | ||
|
||
def next_available_in | ||
[(next_available_at - Time.now).ceil, 0].max | ||
end | ||
|
||
alias_method :retry_after, :next_available_in | ||
alias_method :retry_after, :reset_in | ||
|
||
private | ||
|
||
def limits | ||
@limits ||= response.to_hash.keys.filter_map { |k| k.match(/x-(.*-limit.*)-limit/)&.captures&.first } | ||
def limit_names | ||
@limit_names ||= response.to_hash.keys.filter_map { |k| k.match(/x-(.*-limit.*)-(\w+)/)&.captures&.uniq&.first } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module X | ||
class RateLimit | ||
attr_accessor :type, :response | ||
|
||
def initialize(type, response) | ||
@type = type | ||
@response = response | ||
end | ||
|
||
def limit | ||
response["x-#{type}-limit"].to_i | ||
end | ||
|
||
def remaining | ||
response["x-#{type}-remaining"].to_i | ||
end | ||
|
||
def reset_at | ||
Time.at(response["x-#{type}-reset"].to_i) | ||
end | ||
|
||
def reset_in | ||
[(reset_at - Time.now).ceil, 0].max | ||
end | ||
|
||
alias_method :retry_after, :reset_in | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require_relative "../test_helper" | ||
|
||
module X | ||
class RateLimitTest < Minitest::Test | ||
cover RateLimit | ||
|
||
def setup | ||
Time.stub :now, Time.utc(1983, 11, 24) do | ||
response = { | ||
"x-rate-limit-limit" => "100", | ||
"x-rate-limit-remaining" => "0", | ||
"x-rate-limit-reset" => (Time.now.to_i + 60).to_s | ||
} | ||
@rate_limit = RateLimit.new("rate-limit", response) | ||
end | ||
end | ||
|
||
def test_reset_at | ||
Time.stub :now, Time.utc(1983, 11, 24) do | ||
assert_equal Time.at(Time.now.to_i + 60), @rate_limit.reset_at | ||
end | ||
end | ||
|
||
def test_reset_in | ||
Time.stub :now, Time.utc(1983, 11, 24) do | ||
assert_equal 60, @rate_limit.reset_in | ||
end | ||
end | ||
|
||
def test_reset_in_minimum_value | ||
Time.stub :now, Time.utc(1983, 11, 24) do | ||
@rate_limit.response["x-rate-limit-reset"] = (Time.now.to_i - 60).to_s | ||
|
||
assert_equal 0, @rate_limit.reset_in | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters