discourse/lib/rate_limiter/limit_exceeded.rb

33 lines
903 B
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
class RateLimiter
2013-02-26 00:42:20 +08:00
# A rate limit has been exceeded.
class LimitExceeded < StandardError
2016-04-01 05:33:25 +08:00
attr_reader :type
def initialize(available_in, type=nil)
2013-02-06 03:16:51 +08:00
@available_in = available_in
2016-04-01 05:33:25 +08:00
@type = type
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
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)
2013-02-06 03:16:51 +08:00
end
end
2013-02-26 00:42:20 +08:00
end