Class: Labkit::RateLimit::Config::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/labkit/rate_limit/config/document.rb

Overview

Document is what Loader#load returns: the merged contract and the rules of the rate limits file, per limiter, as Rule objects a Limiter can use. It is immutable; a host builds a new one to reload.

Defined Under Namespace

Classes: Limiter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(limiters, warnings) ⇒ Document

Returns a new instance of Document.

Parameters:

  • limiters (Hash{String => Limiter})
  • warnings (Array<Hash>) —

    with :step, :limiter, :rule, :message



22
23
24
25
# File 'lib/labkit/rate_limit/config/document.rb', line 22

def initialize(limiters, warnings)
  @limiters = limiters.freeze
  @warnings = warnings.freeze
end

Instance Attribute Details

#warnings ⇒ Object (readonly)

Returns the value of attribute warnings.



13
14
15
# File 'lib/labkit/rate_limit/config/document.rb', line 13

def warnings
  @warnings
end

Class Method Details

.empty ⇒ Object

A document with no limiters: every host default applies unchanged.



16
17
18
# File 'lib/labkit/rate_limit/config/document.rb', line 16

def self.empty
  new({}, [])
end

Instance Method Details

#available_properties(name) ⇒ Array<Symbol>

Returns the properties a rule of this limiter may use.

Returns:

  • (Array<Symbol>) —

    the properties a rule of this limiter may use



37
38
39
# File 'lib/labkit/rate_limit/config/document.rb', line 37

def available_properties(name)
  slice(name)&.properties || []
end

#default_rules(name) ⇒ Array<Rule>

Returns the contract's default rules, document order.

Returns:

  • (Array<Rule>) —

    the contract's default rules, document order



47
48
49
# File 'lib/labkit/rate_limit/config/document.rb', line 47

def default_rules(name)
  slice(name)&.default_rules || []
end

#limiter?(name) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/labkit/rate_limit/config/document.rb', line 27

def limiter?(name)
  @limiters.key?(name.to_s)
end

#limiters ⇒ Array<String>

Returns every limiter the contract declares.

Returns:

  • (Array<String>) —

    every limiter the contract declares



32
33
34
# File 'lib/labkit/rate_limit/config/document.rb', line 32

def limiters
  @limiters.keys
end

#override_rules(name) ⇒ Array<Rule>

Returns the file rules for this limiter, document order.

Returns:

  • (Array<Rule>) —

    the file rules for this limiter, document order



42
43
44
# File 'lib/labkit/rate_limit/config/document.rb', line 42

def override_rules(name)
  slice(name)&.override_rules || []
end

#protected_rules(name) ⇒ Array<String>

Returns default rule names no override may replace.

Returns:

  • (Array<String>) —

    default rule names no override may replace



52
53
54
# File 'lib/labkit/rate_limit/config/document.rb', line 52

def protected_rules(name)
  slice(name)&.protected_rules || []
end