Class: Strait
- Inherits:
-
Object
- Object
- Strait
- Defined in:
- lib/strait.rb,
lib/strait/dsl.rb,
lib/strait/rule.rb,
lib/strait/version.rb,
lib/strait/configuration.rb,
lib/strait/rate_limit_exceeded.rb
Overview
A rate-limiter which provides natural defenses for your nation-state. Or your API.
Defined Under Namespace
Classes: Configuration, DSL, RateLimitExceeded, Rule
Constant Summary collapse
- VERSION =
Build 3
'1.2.0'
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(name, rules: [], **config, &block) ⇒ Strait
constructor
A new instance of Strait.
- #limit!(user) ⇒ Object
Constructor Details
#initialize(name, rules: [], **config, &block) ⇒ Strait
Returns a new instance of Strait.
13 14 15 16 17 18 19 |
# File 'lib/strait.rb', line 13 def initialize(name, rules: [], **config, &block) @name = name @raw_rules = rules @config = Strait::Configuration.default.merge(config) @raw_rules += Strait::DSL.new(&block).rules unless block.nil? end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
11 12 13 |
# File 'lib/strait.rb', line 11 def config @config end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/strait.rb', line 11 def name @name end |
Class Method Details
.configuration=(config) ⇒ Object
33 34 35 |
# File 'lib/strait.rb', line 33 def self.configuration=(config) Strait::Configuration.default = Strait::Configuration.new(config) end |
Instance Method Details
#limit!(user) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/strait.rb', line 21 def limit!(user) # Build a hash of { rule => acceptable } structure results = rules.map { |rule| [rule, rule.call(user)] }.to_h return if results.values.all?(true) # Raise an exception for the first rate limit hit results.each do |rule, acceptable| raise Strait::RateLimitExceeded.new(**rule.to_h) unless acceptable end end |