2013-02-05 14:16:51 -05:00
|
|
|
class RateLimiter
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
# A rate limit has been exceeded.
|
2015-03-22 21:20:50 -04:00
|
|
|
class LimitExceeded < StandardError
|
2016-03-15 16:06:50 -04:00
|
|
|
attr_reader :type
|
2015-09-24 12:25:09 -04:00
|
|
|
|
2015-09-24 13:52:32 -04:00
|
|
|
def initialize(available_in, type=nil)
|
2013-02-05 14:16:51 -05:00
|
|
|
@available_in = available_in
|
2015-09-24 13:52:32 -04:00
|
|
|
@type = type
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2015-09-24 12:25:09 -04:00
|
|
|
|
|
|
|
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
|
2015-09-24 13:52:32 -04:00
|
|
|
|
|
|
|
if @type.present?
|
2016-03-07 10:56:17 -05:00
|
|
|
type_key = @type.tr("-", "_")
|
2015-09-24 13:52:32 -04:00
|
|
|
msg = I18n.t("rate_limiter.by_type.#{type_key}", time_left: time_left, default: "")
|
|
|
|
return msg if msg.present?
|
|
|
|
end
|
|
|
|
|
2015-09-24 12:25:09 -04:00
|
|
|
I18n.t("rate_limiter.too_many_requests", time_left: time_left)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|