Class: Prop
Defined Under Namespace
Classes: RateLimitExceededError
Constant Summary
collapse
- VERSION =
"0.6.5"
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
Returns the value of attribute handles.
26
27
28
|
# File 'lib/prop.rb', line 26
def handles
@handles
end
|
Returns the value of attribute reader.
26
27
28
|
# File 'lib/prop.rb', line 26
def reader
@reader
end
|
Returns the value of attribute writer.
26
27
28
|
# File 'lib/prop.rb', line 26
def writer
@writer
end
|
Class Method Details
36
37
38
39
40
41
42
|
# File 'lib/prop.rb', line 36
def configure(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
|
.disabled(&block) ⇒ Object
44
45
46
47
48
49
|
# File 'lib/prop.rb', line 44
def disabled(&block)
@disabled = true
yield
ensure
@disabled = false
end
|
.disabled? ⇒ Boolean
51
52
53
|
# File 'lib/prop.rb', line 51
def disabled?
!!@disabled
end
|
.query(handle, key = nil, options = {}) ⇒ Object
Also known as:
count
83
84
85
86
87
88
|
# File 'lib/prop.rb', line 83
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
28
29
30
|
# File 'lib/prop.rb', line 28
def read(&blk)
self.reader = blk
end
|
.reset(handle, key = nil, options = {}) ⇒ Object
76
77
78
79
80
81
|
# File 'lib/prop.rb', line 76
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
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/prop.rb', line 55
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
return counter if disabled?
if counter >= options[:threshold]
raise Prop::RateLimitExceededError.create(handle, normalize_cache_key(key), options[:threshold], options[:description])
else
writer.call(cache_key, counter + [ 1, options[:increment].to_i ].max)
end
end
|
.throttled?(handle, key = nil, options = {}) ⇒ Boolean
69
70
71
72
73
74
|
# File 'lib/prop.rb', line 69
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
32
33
34
|
# File 'lib/prop.rb', line 32
def write(&blk)
self.writer = blk
end
|