Move descriptions for rate limiting errors into the exception

This commit is contained in:
Robin Ward
2015-09-24 12:25:09 -04:00
parent a1877e8292
commit 3620c8c85e
2 changed files with 14 additions and 12 deletions

View File

@@ -2,10 +2,22 @@ class RateLimiter
# A rate limit has been exceeded.
class LimitExceeded < StandardError
attr_accessor :available_in
def initialize(available_in)
@available_in = available_in
end
def description
time_left = ""
if @available_in < 1.minute.to_i
time_left = I18n.t("rate_limiter.seconds", count: @available_in)
elsif @available_in < 1.hour.to_i
time_left = I18n.t("rate_limiter.minutes", count: (@available_in / 1.minute.to_i))
else
time_left = I18n.t("rate_limiter.hours", count: (@available_in / 1.hour.to_i))
end
I18n.t("rate_limiter.too_many_requests", time_left: time_left)
end
end
end