Module: Throttling
- Defined in:
- lib/throttling.rb,
lib/throttling/base.rb,
lib/throttling/version.rb,
lib/throttling/indifferent_access.rb
Overview
Simple throttling library to limit number of actions in time.
Defined Under Namespace
Classes: Base
Constant Summary collapse
- VERSION =
'0.3.1'
Class Method Summary collapse
-
.disable! ⇒ Object
Disables throttling.
-
.enable! ⇒ Object
Enables throttling.
-
.enabled=(enabled) ⇒ Object
Sets the value indicating whether throttling is enabled.
-
.enabled? ⇒ Boolean
(also: enabled)
Get the value indicating whether throttling is enabled.
-
.for(action) ⇒ Object
Returns a Throttling::Base instance for a given action.
-
.limits ⇒ Object
Gets a Hash with current throttling limits.
-
.limits=(limits) ⇒ Object
Sets current throttling limits.
-
.limits_config ⇒ Object
Gets a throttling limits config file path.
-
.limits_config=(path) ⇒ Object
Sets the configuration file path containing throttling limits.
-
.logger ⇒ Object
Gets the logger used to output errors or warnings.
-
.logger=(logger) ⇒ Object
Sets the logger used to output errors or warnings.
-
.reset_defaults! ⇒ Object
Resets all values to their default state (mostly for testing purpose).
-
.storage ⇒ Object
Gets current Throttling storage.
-
.storage=(storage) ⇒ Object
Sets a storage instance to store Throttling information in.
Class Method Details
.disable! ⇒ Object
Disables throttling.
75 76 77 |
# File 'lib/throttling.rb', line 75 def disable! @@enabled = false end |
.enable! ⇒ Object
Enables throttling.
70 71 72 |
# File 'lib/throttling.rb', line 70 def enable! @@enabled = true end |
.enabled=(enabled) ⇒ Object
Sets the value indicating whether throttling is enabled.
65 66 67 |
# File 'lib/throttling.rb', line 65 def enabled=(enabled) @@enabled = !!enabled end |
.enabled? ⇒ Boolean Also known as: enabled
Get the value indicating whether throttling is enabled.
59 60 61 |
# File 'lib/throttling.rb', line 59 def enabled? !!@@enabled end |
.for(action) ⇒ Object
Returns a Throttling::Base instance for a given action.
80 81 82 |
# File 'lib/throttling.rb', line 80 def for(action) @@instances[action.to_s] ||= Base.new(action.to_s) end |
.limits ⇒ Object
Gets a Hash with current throttling limits.
49 50 51 |
# File 'lib/throttling.rb', line 49 def limits @@limits ||= load_config(limits_config) end |
.limits=(limits) ⇒ Object
Sets current throttling limits.
54 55 56 |
# File 'lib/throttling.rb', line 54 def limits=(limits) @@limits = limits && limits.with_indifferent_access end |
.limits_config ⇒ Object
Gets a throttling limits config file path.
37 38 39 40 |
# File 'lib/throttling.rb', line 37 def limits_config root = (defined?(Rails) && Rails.respond_to?(:root) && Rails.root) || Dir.pwd @@limits_config ||= "#{root}/config/throttling.yml" end |
.limits_config=(path) ⇒ Object
Sets the configuration file path containing throttling limits.
43 44 45 46 |
# File 'lib/throttling.rb', line 43 def limits_config=(path) @@limits = nil @@limits_config = path end |
.logger ⇒ Object
Gets the logger used to output errors or warnings.
27 28 29 |
# File 'lib/throttling.rb', line 27 def logger @@logger ||= (defined?(Rails) && Rails.respond_to(:logger) && Rails.logger) || Logger.new(STDOUT) end |
.logger=(logger) ⇒ Object
Sets the logger used to output errors or warnings.
32 33 34 |
# File 'lib/throttling.rb', line 32 def logger=(logger) @@logger = logger end |
.reset_defaults! ⇒ Object
Resets all values to their default state (mostly for testing purpose).
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/throttling.rb', line 85 def reset_defaults! @@enabled = true @@logger = nil @@storage = nil @@limits_config = nil # Internal variables @@instances = {} @@config = nil end |
.storage ⇒ Object
Gets current Throttling storage. By default returns Rails.cache (if it is a Rails application).
9 10 11 12 13 |
# File 'lib/throttling.rb', line 9 def storage @@storage ||= (defined?(Rails) && Rails.respond_to?(:cache) && Rails.cache) || nil raise ArgumentError, 'Throttling.storage is not specified' unless @@storage @@storage end |
.storage=(storage) ⇒ Object
Sets a storage instance to store Throttling information in. Should implement to to methods:
def fetch(key, options = {}, &block)
def increment(key)
Rails.cache is one of the storages conforming this interface.
22 23 24 |
# File 'lib/throttling.rb', line 22 def storage=(storage) @@storage = storage end |