Class: Pecorino::Block

Inherits:
Object
  • Object
show all
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

Class Method Details

.blocked_until(key:, adapter: Pecorino.adapter) ⇒ Time?

Returns the time until a certain block is in effect

Parameters:

  • key (String)

    the key to get the expiry time for

  • adapter (Pecorino::Adapters::BaseAdapter) (defaults to: Pecorino.adapter)

    the adapter to get the value from

Returns:

  • (Time, nil)

    the time when the block will be released



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

Parameters:

  • key (String)

    the key to set the block for

  • block_for (Float)

    the number of seconds or a time interval to block for

  • adapter (Pecorino::Adapters::BaseAdapter) (defaults to: Pecorino.adapter)

    the adapter to set the value in.

Returns:

  • (Time)

    the time when the block will be released



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