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.



694
695
696
697
698
699
700
# File 'lib/whoosh/app.rb', line 694

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

Instance Method Details

#buildObject



719
720
721
722
723
724
725
726
727
728
# File 'lib/whoosh/app.rb', line 719

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



702
703
704
705
# File 'lib/whoosh/app.rb', line 702

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

#on_store_failure(strategy) ⇒ Object



715
716
717
# File 'lib/whoosh/app.rb', line 715

def on_store_failure(strategy)
  @on_store_failure = strategy
end

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



707
708
709
# File 'lib/whoosh/app.rb', line 707

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

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



711
712
713
# File 'lib/whoosh/app.rb', line 711

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