Class: TrafficJam::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/traffic_jam/configuration.rb

Overview

Configuration for TrafficJam library.

See Also:

  • #configure

Constant Summary collapse

OPTIONS =
%i( key_prefix hash_length redis )

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



17
18
19
20
21
# File 'lib/traffic_jam/configuration.rb', line 17

def initialize(options = {})
  OPTIONS.each do |option|
    self.send("#{option}=", options[option])
  end
end

Instance Method Details

#limits(action) ⇒ Hash

Get registered limit parameters for an action.

Parameters:

  • action (Symbol)

    action name

Returns:

  • (Hash)

    max and period parameters in a hash

Raises:

See Also:



56
57
58
59
60
61
# File 'lib/traffic_jam/configuration.rb', line 56

def limits(action)
  @limits ||= {}
  limits = @limits[action.to_sym]
  raise TrafficJam::LimitNotFound.new(action) if limits.nil?
  limits
end

#max(action) ⇒ Integer

Get the limit cap registered to an action.

Returns:

  • (Integer)

    limit cap

See Also:



38
39
40
# File 'lib/traffic_jam/configuration.rb', line 38

def max(action)
  limits(action)[:max]
end

#period(action) ⇒ Integer

Get the limit period registered to an action.

Returns:

  • (Integer)

    limit period in seconds

See Also:



46
47
48
# File 'lib/traffic_jam/configuration.rb', line 46

def period(action)
  limits(action)[:period]
end

#register(action, max, period) ⇒ Object

Register a default cap and period with an action name. For use with TrafficJam.limit.

Parameters:

  • action (Symbol)

    action name

  • max (Integer)

    limit cap

  • period (Fixnum)

    limit period in seconds



29
30
31
32
# File 'lib/traffic_jam/configuration.rb', line 29

def register(action, max, period)
  @limits ||= {}
  @limits[action.to_sym] = { max: max, period: period }
end