prop 2.2.5 → 2.3.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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/prop.rb +1 -1
  3. data/lib/prop/limiter.rb +20 -20
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d749e4483b80fc4abe3ab44ba034c349bafaf521dc00c4ad059e3e9bc683e88a
4
- data.tar.gz: d00a838d4b8f14cf9eb5a440e6a6fce475f6fb09d9937e2ebf6c0d66701ef82e
3
+ metadata.gz: db081ee0a96819c4b270c57e0205527b122d93b214b2c42af7d50159cfb140d1
4
+ data.tar.gz: f8ef4abcc3cb6762b70a993fed0446321585a14e685cbb91a1e37f355fe507e1
5
5
  SHA512:
6
- metadata.gz: 36474a7d324d76381193d6e39928e1a9e07ba6e15e2bbec050db8bff9d3d2f0eeb4fb713f3882864fbc4ef3c65bb5343a7d99ae6ecf588326c62e1672da28274
7
- data.tar.gz: f68780e2c3cc1196a10e24e14949b8ee9b348d72c321bf4aa98e918031b423bf467ccefdda0cbd77dfdb9090403603540358b3a3561bbd17e6ce46d705cf7b72
6
+ metadata.gz: 7ff690ae0b93ebe4f15d05bb5e8b2b7f5f983607a56a9c9d1ad04c19a9de1aaa21b38b70d5763b091b5db6cd10c9890b3eb33bd5ca910b055f682eb0b5b23b07
7
+ data.tar.gz: 268563b7482c713097888855260875a19777cdd20982496bec4644dfd1b494dde6666d09b10789617fd8d905d9a18b98265dfebcf1609b20734c46ee56e1a24b
@@ -3,7 +3,7 @@ require "prop/limiter"
3
3
  require "forwardable"
4
4
 
5
5
  module Prop
6
- VERSION = "2.2.5"
6
+ VERSION = "2.3.0"
7
7
 
8
8
  # Short hand for accessing Prop::Limiter methods
9
9
  class << self
@@ -72,8 +72,8 @@ module Prop
72
72
  #
73
73
  # Returns true if the threshold for this handle has been reached, else returns false
74
74
  def throttle(handle, key = nil, options = {})
75
- options, cache_key = prepare(handle, key, options)
76
- throttled = _throttle(handle, key, cache_key, options).first
75
+ options, cache_key, strategy = prepare(handle, key, options)
76
+ throttled = _throttle(strategy, handle, key, cache_key, options).first
77
77
  block_given? && !throttled ? yield : throttled
78
78
  end
79
79
 
@@ -87,8 +87,8 @@ module Prop
87
87
  # Raises Prop::RateLimited if the threshold for this handle has been reached
88
88
  # Returns the value of the block if given a such, otherwise the current count of the throttle
89
89
  def throttle!(handle, key = nil, options = {}, &block)
90
- options, cache_key = prepare(handle, key, options)
91
- throttled, counter = _throttle(handle, key, cache_key, options)
90
+ options, cache_key, strategy = prepare(handle, key, options)
91
+ throttled, counter = _throttle(strategy, handle, key, cache_key, options)
92
92
 
93
93
  if throttled
94
94
  raise Prop::RateLimited.new(options.merge(
@@ -108,9 +108,9 @@ module Prop
108
108
  #
109
109
  # Returns true if a call to `throttle!` with same parameters would raise, otherwise false
110
110
  def throttled?(handle, key = nil, options = {})
111
- options, cache_key = prepare(handle, key, options)
112
- counter = @strategy.counter(cache_key, options)
113
- @strategy.compare_threshold?(counter, :>=, options)
111
+ options, cache_key, strategy = prepare(handle, key, options)
112
+ counter = strategy.counter(cache_key, options)
113
+ strategy.compare_threshold?(counter, :>=, options)
114
114
  end
115
115
 
116
116
  # Public: Resets a specific throttle
@@ -120,8 +120,8 @@ module Prop
120
120
  #
121
121
  # Returns nothing
122
122
  def reset(handle, key = nil, options = {})
123
- _options, cache_key = prepare(handle, key, options)
124
- @strategy.reset(cache_key)
123
+ _options, cache_key, strategy = prepare(handle, key, options)
124
+ strategy.reset(cache_key)
125
125
  end
126
126
 
127
127
  # Public: Counts the number of times the given handle/key combination has been hit in the current window
@@ -131,8 +131,8 @@ module Prop
131
131
  #
132
132
  # Returns a count of hits in the current window
133
133
  def count(handle, key = nil, options = {})
134
- options, cache_key = prepare(handle, key, options)
135
- @strategy.counter(cache_key, options)
134
+ options, cache_key, strategy = prepare(handle, key, options)
135
+ strategy.counter(cache_key, options)
136
136
  end
137
137
  alias :query :count
138
138
 
@@ -143,18 +143,18 @@ module Prop
143
143
 
144
144
  private
145
145
 
146
- def _throttle(handle, key, cache_key, options)
147
- return [false, @strategy.zero_counter] if disabled?
146
+ def _throttle(strategy, handle, key, cache_key, options)
147
+ return [false, strategy.zero_counter] if disabled?
148
148
 
149
149
  counter = options.key?(:decrement) ?
150
- @strategy.decrement(cache_key, options.fetch(:decrement), options) :
151
- @strategy.increment(cache_key, options.fetch(:increment, 1), options)
150
+ strategy.decrement(cache_key, options.fetch(:decrement), options) :
151
+ strategy.increment(cache_key, options.fetch(:increment, 1), options)
152
152
 
153
- if @strategy.compare_threshold?(counter, :>, options)
153
+ if strategy.compare_threshold?(counter, :>, options)
154
154
  before_throttle_callback &&
155
155
  before_throttle_callback.call(handle, key, options[:threshold], options[:interval])
156
156
 
157
- result = if options[:first_throttled] && @strategy.first_throttled?(counter, options)
157
+ result = if options[:first_throttled] && strategy.first_throttled?(counter, options)
158
158
  :first_throttled
159
159
  else
160
160
  true
@@ -177,11 +177,11 @@ module Prop
177
177
 
178
178
  options = Prop::Options.build(key: key, params: params, defaults: defaults)
179
179
 
180
- @strategy = options.fetch(:strategy)
180
+ strategy = options.fetch(:strategy)
181
181
 
182
- cache_key = @strategy.build(key: key, handle: handle, interval: options[:interval])
182
+ cache_key = strategy.build(key: key, handle: handle, interval: options[:interval])
183
183
 
184
- [ options, cache_key ]
184
+ [ options, cache_key, strategy ]
185
185
  end
186
186
  end
187
187
  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.2.5
4
+ version: 2.3.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: 2018-05-07 00:00:00.000000000 Z
11
+ date: 2018-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake