Class: Prop

Inherits:
Object show all
Defined in:
lib/prop.rb

Defined Under Namespace

Classes: RateLimitExceededError

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.handlesObject

Returns the value of attribute handles.



22
23
24
# File 'lib/prop.rb', line 22

def handles
  @handles
end

.readerObject

Returns the value of attribute reader.



22
23
24
# File 'lib/prop.rb', line 22

def reader
  @reader
end

.writerObject

Returns the value of attribute writer.



22
23
24
# File 'lib/prop.rb', line 22

def writer
  @writer
end

Class Method Details

.defaults(handle, defaults) ⇒ Object

Raises:

  • (RuntimeError)


45
46
47
48
49
50
51
# File 'lib/prop.rb', line 45

def defaults(handle, defaults)
  raise RuntimeError.new("Invalid threshold setting") unless defaults[:threshold].to_i > 0
  raise RuntimeError.new("Invalid interval setting")  unless defaults[:interval].to_i > 0

  self.handles ||= {}
  self.handles[handle] = defaults
end

.increment(&blk) ⇒ Object



32
33
34
# File 'lib/prop.rb', line 32

def increment(&blk)
  self.incrementer = blk
end

.incrementerObject



40
41
42
43
# File 'lib/prop.rb', line 40

def incrementer
  @incrementer ? Proc.new { |key, inc| @incrementer.call(key, inc) } :
    Proc.new { |key, inc| self.writer.call(key, (self.reader.call(key) || 0).to_i + inc) }
end

.incrementer=(value) ⇒ Object



36
37
38
# File 'lib/prop.rb', line 36

def incrementer=(value)
  @incrementer = value
end

.query(handle, key = nil, options = {}) ⇒ Object



79
80
81
82
83
84
# File 'lib/prop.rb', line 79

def query(handle, key = nil, options = {})
  options   = sanitized_prop_options(handle, key, options)
  cache_key = sanitized_prop_key(key, options[:interval])

  reader.call(cache_key).to_i
end

.read(&blk) ⇒ Object



24
25
26
# File 'lib/prop.rb', line 24

def read(&blk)
  self.reader = blk
end

.reset(handle, key = nil, options = {}) ⇒ Object



72
73
74
75
76
77
# File 'lib/prop.rb', line 72

def reset(handle, key = nil, options = {})
  options   = sanitized_prop_options(handle, key, options)
  cache_key = sanitized_prop_key(key, options[:interval])

  writer.call(cache_key, 0)
end

.throttle!(handle, key = nil, options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/prop.rb', line 53

def throttle!(handle, key = nil, options = {})
  options   = sanitized_prop_options(handle, key, options)
  cache_key = sanitized_prop_key(key, options[:interval])
  counter   = reader.call(cache_key).to_i

  incrementer.call(cache_key, [ 1, options[:increment].to_i ].max)

  if counter >= options[:threshold]
    raise Prop::RateLimitExceededError.create(handle, normalize_cache_key(key), options[:threshold])
  end
end

.throttled?(handle, key = nil, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
# File 'lib/prop.rb', line 65

def throttled?(handle, key = nil, options = {})
  options   = sanitized_prop_options(handle, key, options)
  cache_key = sanitized_prop_key(key, options[:interval])

  reader.call(cache_key).to_i >= options[:threshold]
end

.write(&blk) ⇒ Object



28
29
30
# File 'lib/prop.rb', line 28

def write(&blk)
  self.writer = blk
end