prop 2.7.0 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f8d392029c77c453e5fc0d6a82fcb8401dc3ebabb01586b15bca7ab961df2d8f
4
- data.tar.gz: ba93851562644df3cfea26c2d41d4e49fe52e4f7d5ca614313ab6cbd911c1293
3
+ metadata.gz: cbbc31d7770e04a3a7142fe8b7cb300cd2b3aeac66eb4e779be6030490e6563d
4
+ data.tar.gz: c6278f0489ab81f3e0feb100334faca7042552a008b7381ef9b95043093f34b3
5
5
  SHA512:
6
- metadata.gz: d41e52e89027cba93e67df82d5b65f654921c8c8c1b109e6eb6653f01d9a20fe493a9975fec89d817b886a25a3871a468a83ea3270fc94c94e2503905a04e772
7
- data.tar.gz: f4024c8ae24cbca19eeade5491532e26ee270428a048a135c62ff43ffcbe7df4770bb1babb6424fc5b2c92634326162fa732be7aa548867dfcf66773a992ad63
6
+ metadata.gz: 15ebfffc8652e339fcb9a419d843ac18f9bfaa3e804d7289231d22edd944de25d23025d147dabf303f45b8787c14877b6c6019e5ed5351b10d462b2572f153d0
7
+ data.tar.gz: df16b882ca625eeccd2c11b67574eee8f905996199dee963b05e188c292e92f0a6d8361293698ab65f5bfadbe190882ed2f105172df5184da050276cba775325
data/README.md CHANGED
@@ -31,6 +31,17 @@ Prop.before_throttle do |handle, key, threshold, interval|
31
31
  end
32
32
  ```
33
33
 
34
+ ## Setting an After Evaluated Callback
35
+
36
+ You can define an optional callback that is invoked when a rate limit is checked. The callback will be invoked regardless
37
+ of the result of the evaluation.
38
+
39
+ ```ruby
40
+ Prop.after_evaluated do |handle, counter, options|
41
+ Rails.logger.info "Prop #{handle} has just been check. current value: #{counter}"
42
+ end
43
+ ````
44
+
34
45
  ## Defining thresholds
35
46
 
36
47
  Example: Limit on accepted emails per hour from a given user, by defining a threshold and interval (in seconds):
@@ -43,7 +54,7 @@ Prop.configure(:mails_per_hour, threshold: 0, interval: 1.hour, description: "Al
43
54
  ```
44
55
 
45
56
  ```ruby
46
- # Throws Prop::RateLimitExceededError if the threshold/interval has been reached
57
+ # Throws Prop::RateLimited if the threshold/interval has been reached
47
58
  Prop.throttle!(:mails_per_hour)
48
59
 
49
60
  # Prop can be used to guard a block of code
@@ -10,7 +10,7 @@ module Prop
10
10
  end
11
11
 
12
12
  def counter(cache_key, options)
13
- cache.read(cache_key).to_i
13
+ cache.read(cache_key, raw: true).to_i
14
14
  end
15
15
 
16
16
  # options argument is kept for api consistency for all strategies
data/lib/prop/limiter.rb CHANGED
@@ -9,7 +9,7 @@ module Prop
9
9
  class Limiter
10
10
 
11
11
  class << self
12
- attr_accessor :handles, :before_throttle_callback, :cache
12
+ attr_accessor :handles, :before_throttle_callback, :cache, :after_evaluated_callback
13
13
 
14
14
  def read(&blk)
15
15
  raise "Use .cache = "
@@ -39,6 +39,10 @@ module Prop
39
39
  self.before_throttle_callback = blk
40
40
  end
41
41
 
42
+ def after_evaluated(&blk)
43
+ self.after_evaluated_callback = blk
44
+ end
45
+
42
46
  # Public: Registers a handle for rate limiting
43
47
  #
44
48
  # handle - the name of the handle you wish to use in your code, e.g. :login_attempt
@@ -150,13 +154,18 @@ module Prop
150
154
  return [false, strategy.zero_counter] if disabled?
151
155
 
152
156
  if leaky_bucket_strategy?(strategy)
153
- return Prop::LeakyBucketStrategy._throttle_leaky_bucket(handle, key, cache_key, options)
157
+ is_over_limit, bucket_info_hash = Prop::LeakyBucketStrategy._throttle_leaky_bucket(handle, key, cache_key, options)
158
+ bucket_counter = bucket_info_hash.fetch(:bucket)
159
+ after_evaluated_callback.call(handle, bucket_counter, options.merge(bucket_info_hash)) if after_evaluated_callback
160
+ return [is_over_limit, bucket_info_hash]
154
161
  end
155
162
 
156
163
  counter = options.key?(:decrement) ?
157
164
  strategy.decrement(cache_key, options.fetch(:decrement), options) :
158
165
  strategy.increment(cache_key, options.fetch(:increment, 1), options)
159
166
 
167
+ after_evaluated_callback.call(handle, counter, options) if after_evaluated_callback
168
+
160
169
  if strategy.compare_threshold?(counter, :>, options)
161
170
  before_throttle_callback &&
162
171
  before_throttle_callback.call(handle, key, options[:threshold], options[:interval])
data/lib/prop.rb CHANGED
@@ -3,12 +3,12 @@ require "prop/limiter"
3
3
  require "forwardable"
4
4
 
5
5
  module Prop
6
- VERSION = "2.7.0"
6
+ VERSION = "2.9.0"
7
7
 
8
8
  # Short hand for accessing Prop::Limiter methods
9
9
  class << self
10
10
  extend Forwardable
11
11
  def_delegators :"Prop::Limiter", :read, :write, :cache, :cache=, :configure, :configurations, :disabled, :before_throttle
12
- def_delegators :"Prop::Limiter", :throttle, :throttle!, :throttled?, :count, :query, :reset
12
+ def_delegators :"Prop::Limiter", :throttle, :throttle!, :throttled?, :count, :query, :reset, :after_evaluated
13
13
  end
14
14
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prop
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.0
4
+ version: 2.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Morten Primdahl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-14 00:00:00.000000000 Z
11
+ date: 2024-02-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: primdahl@me.com
@@ -38,14 +38,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - ">="
40
40
  - !ruby/object:Gem::Version
41
- version: '2.2'
41
+ version: '2.7'
42
42
  required_rubygems_version: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
46
  version: '0'
47
47
  requirements: []
48
- rubygems_version: 3.0.3.1
48
+ rubygems_version: 3.5.3
49
49
  signing_key:
50
50
  specification_version: 4
51
51
  summary: Gem for implementing rate limits.