Class: RateLimitedService::RateLimiterScopedAndKeyed

Inherits:
Object
  • Object
show all
Defined in:
app/services/concerns/rate_limited_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, opts:, rate_limiter:) ⇒ RateLimiterScopedAndKeyed

Returns a new instance of RateLimiterScopedAndKeyed.



31
32
33
34
35
# File 'app/services/concerns/rate_limited_service.rb', line 31

def initialize(key:, opts:, rate_limiter:)
  @key = key
  @opts = opts
  @rate_limiter = rate_limiter
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



29
30
31
# File 'app/services/concerns/rate_limited_service.rb', line 29

def key
  @key
end

#optsObject (readonly)

Returns the value of attribute opts.



29
30
31
# File 'app/services/concerns/rate_limited_service.rb', line 29

def opts
  @opts
end

#rate_limiterObject (readonly)

Returns the value of attribute rate_limiter.



29
30
31
# File 'app/services/concerns/rate_limited_service.rb', line 29

def rate_limiter
  @rate_limiter
end

Instance Method Details

#rate_limit!(service) ⇒ Object



37
38
39
40
41
42
43
# File 'app/services/concerns/rate_limited_service.rb', line 37

def rate_limit!(service)
  evaluated_scope = evaluated_scope_for(service)

  if rate_limiter.throttled?(key, **opts.merge(scope: evaluated_scope.values, users_allowlist: users_allowlist))
    raise RateLimitedError.new(key: key, rate_limiter: rate_limiter), _('This endpoint has been requested too many times. Try again later.')
  end
end