Class: Pecorino::Block
- Inherits:
-
Object
- Object
- Pecorino::Block
- Defined in:
- lib/pecorino/block.rb
Overview
Provides access to Pecorino blocks - same blocks which get set when a throttle triggers. The blocks are just keys in the data store which have an expiry value. This can be useful if you want to restrict access to a resource for an arbitrary timespan.
Class Method Summary collapse
-
.blocked_until(key:, adapter: Pecorino.adapter) ⇒ Time?
Returns the time until a certain block is in effect.
-
.set!(key:, block_for:, adapter: Pecorino.adapter) ⇒ Time
Sets a block for the given key.
Class Method Details
.blocked_until(key:, adapter: Pecorino.adapter) ⇒ Time?
Returns the time until a certain block is in effect
25 26 27 28 |
# File 'lib/pecorino/block.rb', line 25 def self.blocked_until(key:, adapter: Pecorino.adapter) t = adapter.blocked_until(key: key) (t && t > Time.now) ? t : nil end |
.set!(key:, block_for:, adapter: Pecorino.adapter) ⇒ Time
Sets a block for the given key. The block will also be seen by the Pecorino::Throttle with the same key
13 14 15 16 17 18 |
# File 'lib/pecorino/block.rb', line 13 def self.set!(key:, block_for:, adapter: Pecorino.adapter) adapter.set_block(key: key, block_for: block_for) Time.now + block_for rescue ArgumentError # negative block nil end |