rails-action_throttling 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0fdff74259b724951350ac06ccfc81553ca6db375e96c5b8a9b77087e7785275
4
- data.tar.gz: b50b7feb2ff1e4ccf8df7bebcd1217387aca28593cd2b6511f41428b2d74a334
3
+ metadata.gz: 160f1ac8a6f56f24b75028edd56a27877bb8dc5d42750a86f6202dd0c142890d
4
+ data.tar.gz: 77c4d7913576a41054fce1e026753fd35af8fdce3407b6d345a0ed1a6df42b3d
5
5
  SHA512:
6
- metadata.gz: bc3385aad2cc23603ef63bac816e91d03f118444b5e14719367f3560ffb35c9717f4c8e29ea2d9f2c7756dfaeb6faa400d739b56077e7cfc3160d44425322c92
7
- data.tar.gz: 81b49da9623ca3ff30ce9e08229b4864f5b37a34fcb81430e83bbdbb9bb939a2d24fabd388df7516447c503171fdc291c602e55e92a7b73857d8f2b5888a1eda
6
+ metadata.gz: 0ac62c6c1e7d5c2f50fd97de965175003b0f60fba58b1cd8f223c37fb54930e91756aaefbe953ea976552a0a69dd1addb21be8896e15827b795e7356bb9cc93c
7
+ data.tar.gz: 82b31893a480cdbfabcc36250e2293cdf4f87eb95ec7a20c3f416d1f8f1032b3185090cba3e627e22423681ff1b62f12b07a0c870f5226233c736a2ef8d1c4f6
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails-action_throttling (0.1.0)
4
+ rails-action_throttling (0.1.1)
5
5
  activesupport (~> 5.2)
6
6
  http-errors (~> 0.1)
7
7
  redis (~> 4.0)
data/README.md CHANGED
@@ -11,13 +11,13 @@ Configure your bucket. There are no default values for this, since we can't know
11
11
  ```ruby
12
12
  ActionThrottling.configure do |config|
13
13
  # The bucket_key is evaluated inside your application context
14
- config.bucket_key = Proc.new { current_user.bucket }
14
+ config.bucket_key = Proc.new { current_user.id }
15
15
 
16
16
  # Set the interval in which the bucket is regenerated
17
- config.regenerate_interval = 10.minutes
17
+ config.regenerate_interval = Proc.new { current_user.regenerate_interval }
18
18
 
19
- # Sets the number of tokens to be pub back into the bucket
20
- config.regenerate_amount = 100
19
+ # Sets the number of tokens to be put back into the bucket
20
+ config.regenerate_amount = Proc.new { current_user.regenerate_amount }
21
21
 
22
22
  # (optional) If you're not running on a completely vanilla redis connection,
23
23
  # you can supply your own redis instance here.
@@ -33,7 +33,7 @@ def show
33
33
  # Based on the configuration above, this will allow the user to call this
34
34
  # endpoint 100 times every minute.
35
35
  #
36
- # This will call the `deduct(1)` method on the configured `config.bucket` (see
36
+ # This will call the `deduct(1)` method on the configured `config.bucket_key` (see
37
37
  # above)
38
38
  cost 1
39
39
  # ...
@@ -44,7 +44,7 @@ If you have something like a create method, or some complex method that's costly
44
44
 
45
45
  ```ruby
46
46
  def complex_method
47
- # This will call `deduct(25)` on the `config.bucket` (see above)
47
+ # This will call `deduct(25)` on the `config.bucket_key` (see above)
48
48
  cost 25
49
49
  # ...
50
50
  end
@@ -11,7 +11,7 @@ module ActionThrottling
11
11
  def cost(price)
12
12
  # If last time the bucket was called is older than the regeneration limit
13
13
  # regenerate the bucket.
14
- regenerate if last_called < regeneration_time
14
+ regenerate if expired?
15
15
 
16
16
  # Deduct the price from the bucket. If it's below zero we raiss a
17
17
  # HttpError::ToManyRequests exception/
@@ -20,17 +20,29 @@ module ActionThrottling
20
20
  end
21
21
 
22
22
  # Store when the cost was last called so we can calculate regeneration
23
- redis.set last_call_key, Time.now.httpdate
23
+ update_call_time
24
24
  end
25
25
 
26
26
  private
27
27
 
28
+ def update_call_time
29
+ redis.set last_call_key, Time.now.httpdate
30
+ end
31
+
32
+ def expired?
33
+ last_called < regeneration_time
34
+ end
35
+
28
36
  def last_call_key
29
37
  "#{bucket_key}-last-call"
30
38
  end
31
39
 
32
40
  def regenerate
33
- redis.set bucket_key, ActionThrottling.configuration.regenerate_amount
41
+ redis.set bucket_key, regenerate_amount
42
+ end
43
+
44
+ def regenerate_amount
45
+ instance_eval &ActionThrottling.configuration.regenerate_amount
34
46
  end
35
47
 
36
48
  def last_called
@@ -47,7 +59,7 @@ module ActionThrottling
47
59
  end
48
60
 
49
61
  def regeneration_time
50
- ActionThrottling.configuration.regenerate_interval.ago
62
+ instance_eval(&ActionThrottling.configuration.regenerate_interval).ago
51
63
  end
52
64
 
53
65
  def redis
@@ -1,3 +1,3 @@
1
1
  module ActionThrottling
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-action_throttling
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emil Kampp