discourse/lib/rate_limiter/limit_exceeded.rb

35 lines
972 B
Ruby
Raw Normal View History

2013-02-05 13:16:51 -06:00
class RateLimiter
2013-02-25 10:42:20 -06:00
# A rate limit has been exceeded.
class LimitExceeded < StandardError
2017-12-04 04:44:16 -06:00
attr_reader :type, :available_in
2017-07-27 20:20:09 -05:00
def initialize(available_in, type = nil)
2013-02-05 13:16:51 -06:00
@available_in = available_in
@type = type
2013-02-05 13:16:51 -06:00
end
def description
time_left =
if @available_in <= 3
I18n.t("rate_limiter.short_time")
elsif @available_in < 1.minute.to_i
I18n.t("rate_limiter.seconds", count: @available_in)
elsif @available_in < 1.hour.to_i
I18n.t("rate_limiter.minutes", count: (@available_in / 1.minute.to_i))
else
I18n.t("rate_limiter.hours", count: (@available_in / 1.hour.to_i))
end
if @type.present?
type_key = @type.tr("-", "_")
msg = I18n.t("rate_limiter.by_type.#{type_key}", time_left: time_left, default: "")
return msg if msg.present?
end
I18n.t("rate_limiter.too_many_requests", time_left: time_left)
end
2013-02-05 13:16:51 -06:00
end
2013-02-25 10:42:20 -06:00
end