Class: Whoosh::App::RateLimitBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/whoosh/app.rb

Instance Method Summary collapse

Constructor Details

#initializeRateLimitBuilder

Returns a new instance of RateLimitBuilder.



715
716
717
718
719
720
721
# File 'lib/whoosh/app.rb', line 715

def initialize
  @default_limit = 60
  @default_period = 60
  @rules = []
  @tiers = []
  @on_store_failure = :fail_open
end

Instance Method Details

#buildObject



740
741
742
743
744
745
746
747
748
749
# File 'lib/whoosh/app.rb', line 740

def build
  limiter = Auth::RateLimiter.new(
    default_limit: @default_limit,
    default_period: @default_period,
    on_store_failure: @on_store_failure
  )
  @rules.each { |r| limiter.rule(r[:path], limit: r[:limit], period: r[:period]) }
  @tiers.each { |t| limiter.tier(t[:name], limit: t[:limit], period: t[:period], unlimited: t[:unlimited]) }
  limiter
end

#default(limit:, period:) ⇒ Object



723
724
725
726
# File 'lib/whoosh/app.rb', line 723

def default(limit:, period:)
  @default_limit = limit
  @default_period = period
end

#on_store_failure(strategy) ⇒ Object



736
737
738
# File 'lib/whoosh/app.rb', line 736

def on_store_failure(strategy)
  @on_store_failure = strategy
end

#rule(path, limit:, period:) ⇒ Object



728
729
730
# File 'lib/whoosh/app.rb', line 728

def rule(path, limit:, period:)
  @rules << { path: path, limit: limit, period: period }
end

#tier(name, limit: nil, period: nil, unlimited: false) ⇒ Object



732
733
734
# File 'lib/whoosh/app.rb', line 732

def tier(name, limit: nil, period: nil, unlimited: false)
  @tiers << { name: name, limit: limit, period: period, unlimited: unlimited }
end