berater 0.9.0 → 0.10.0

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: 476b59e3f1e27908f5c8a5097087e6c8c2384531e1284d79b9d89a73fbbb5840
4
- data.tar.gz: de50fdcab6ea7dc9520fcf1aefdd07b1f8775c0ce96a30e3f1d45b6ccfb7b00f
3
+ metadata.gz: 32de3ce22b3804c00224140d747e66fc9de887582f6bd4812c6404cfcba0056e
4
+ data.tar.gz: 16054c2814c98bd64fa0ea16ee9ca2110bff6df9cb4a17bf292759d3dcc901e7
5
5
  SHA512:
6
- metadata.gz: 2446e64f528a8ef1d37791922d37cddcebdf702f3c5beff2ef7ea9d611ce4cc6d6c40ac42e1ea826987a2eb2ceea3ddf97e420bb7369686b8239b260a1725c55
7
- data.tar.gz: 23dccad7e02cc425631ceeb1dfbc6960b94ac92c523d840a991917af02caaf39a7befc5887fe45631f565ddb2eddb4850e557348ab29427d3cbae1db89124c3c
6
+ metadata.gz: 78b027082f40a7bdc6d64e245359fc5b34f3b2738fdf683bfa28047f788378f46f1068f9c7541071b8e2e043b78bfee0219eaf70239e0ec09274238a979f7085
7
+ data.tar.gz: b63f8c4103be7c518c96eed52b98d00d094774acbc9b8df42810c945b210d4e5850a49c38ab318a38a25521da623ffb2940693191753a485779ca03be5232ea4
@@ -99,11 +99,16 @@ module Berater
99
99
  end
100
100
 
101
101
  class << self
102
- def new(*)
102
+ def new(*args, **kwargs)
103
103
  # can only call via subclass
104
104
  raise NoMethodError if self == Berater::Limiter
105
105
 
106
- super
106
+ if RUBY_VERSION < '3' && kwargs.empty?
107
+ # avoid ruby 2 problems with empty hashes
108
+ super(*args)
109
+ else
110
+ super
111
+ end
107
112
  end
108
113
 
109
114
  def cache_key(key)
@@ -116,6 +121,7 @@ module Berater
116
121
  def inherited(subclass)
117
122
  # automagically create convenience method
118
123
  name = subclass.to_s.split(':')[-1]
124
+
119
125
  Berater.define_singleton_method(name) do |*args, **opts, &block|
120
126
  Berater::Utils.convenience_fn(subclass, *args, **opts, &block)
121
127
  end
data/lib/berater/lock.rb CHANGED
@@ -20,7 +20,7 @@ module Berater
20
20
 
21
21
  @released_at = Time.now
22
22
  @release_fn&.call
23
- nil
23
+ true
24
24
  end
25
25
 
26
26
  end
@@ -6,11 +6,11 @@ module Berater
6
6
  attr_reader :source
7
7
 
8
8
  def initialize(source)
9
- @source = source
9
+ @source = source.dup.freeze
10
10
  end
11
11
 
12
12
  def sha
13
- @sha ||= Digest::SHA1.hexdigest(minify)
13
+ @sha ||= Digest::SHA1.hexdigest(minify).freeze
14
14
  end
15
15
 
16
16
  def eval(redis, *args)
@@ -44,7 +44,7 @@ module Berater
44
44
  def minify
45
45
  # trim comments (whole line and partial)
46
46
  # and whitespace (prefix and empty lines)
47
- @minify ||= source.gsub(/^\s*--.*\n|\s*--.*|^\s*|^$\n/, '').chomp
47
+ @minify ||= source.gsub(/^\s*--.*\n|\s*--.*|^\s*|^$\n/, '').chomp.freeze
48
48
  end
49
49
 
50
50
  end
data/lib/berater/utils.rb CHANGED
@@ -44,12 +44,7 @@ module Berater
44
44
 
45
45
  def convenience_fn(klass, *args, **opts, &block)
46
46
  limiter = klass.new(*args, **opts)
47
- if block_given?
48
- limiter.limit(&block)
49
- else
50
- limiter
51
- end
47
+ block ? limiter.limit(&block) : limiter
52
48
  end
53
-
54
49
  end
55
50
  end
@@ -1,3 +1,3 @@
1
1
  module Berater
2
- VERSION = '0.9.0'
2
+ VERSION = "0.10.0"
3
3
  end
data/spec/berater_spec.rb CHANGED
@@ -59,8 +59,8 @@ describe Berater do
59
59
  end
60
60
 
61
61
  it 'accepts an optional redis parameter' do
62
- redis = double('Redis')
63
- limiter = Berater.new(:key, capacity, opts.merge(redis: redis))
62
+ redis = double(Redis)
63
+ limiter = Berater.new(:key, capacity, **opts.merge(redis: redis))
64
64
  expect(limiter.redis).to be redis
65
65
  end
66
66
  end
data/spec/limiter_spec.rb CHANGED
@@ -79,6 +79,16 @@ describe Berater::Limiter do
79
79
  expect { limiter.limit }.to raise_error(RuntimeError)
80
80
  end
81
81
  end
82
+
83
+ it 'releases the lock even when limited code raises an error' do
84
+ lock = Berater::Lock.new(Float::INFINITY, 0)
85
+ expect(subject).to receive(:acquire_lock).and_return(lock)
86
+ expect(lock).to receive(:release)
87
+
88
+ expect {
89
+ subject.limit { raise 'fail' }
90
+ }.to raise_error(RuntimeError)
91
+ end
82
92
  end
83
93
 
84
94
  describe '#==' do
@@ -93,5 +93,4 @@ describe Berater::LuaScript do
93
93
  it { subject }
94
94
  end
95
95
  end
96
-
97
96
  end
@@ -1,5 +1,5 @@
1
1
  class Meddler
2
- def call(*)
2
+ def call(*args, **kwargs)
3
3
  yield
4
4
  end
5
5
  end
@@ -73,18 +73,18 @@ describe 'Berater.middleware' do
73
73
 
74
74
  context 'when middleware meddles' do
75
75
  it 'can change the capacity' do
76
- expect(middleware).to receive(:call) do |limiter, opts, &block|
76
+ expect(middleware).to receive(:call) do |limiter, **opts, &block|
77
77
  opts[:capacity] = 0
78
- block.call
78
+ block.call(limiter, **opts)
79
79
  end
80
80
 
81
81
  expect { limiter.limit }.to be_overloaded
82
82
  end
83
83
 
84
84
  it 'can change the cost' do
85
- expect(middleware).to receive(:call) do |limiter, opts, &block|
85
+ expect(middleware).to receive(:call) do |limiter, **opts, &block|
86
86
  opts[:cost] = 2
87
- block.call
87
+ block.call(limiter, **opts)
88
88
  end
89
89
 
90
90
  expect { limiter.limit }.to be_overloaded
@@ -93,8 +93,8 @@ describe 'Berater.middleware' do
93
93
  it 'can change the limiter' do
94
94
  other_limiter = Berater::Inhibitor.new
95
95
 
96
- expect(middleware).to receive(:call) do |limiter, opts, &block|
97
- block.call other_limiter, opts
96
+ expect(middleware).to receive(:call) do |limiter, **opts, &block|
97
+ block.call(other_limiter, **opts)
98
98
  end
99
99
  expect(other_limiter).to receive(:acquire_lock).and_call_original
100
100
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: berater
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Pepper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-09 00:00:00.000000000 Z
11
+ date: 2021-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: meddleware
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '0.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '0.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: redis
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '3'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: benchmark
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -193,24 +193,24 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
193
  - !ruby/object:Gem::Version
194
194
  version: '0'
195
195
  requirements: []
196
- rubygems_version: 3.0.8
196
+ rubygems_version: 3.2.3
197
197
  signing_key:
198
198
  specification_version: 4
199
199
  summary: Berater
200
200
  test_files:
201
- - spec/rate_limiter_spec.rb
202
- - spec/matchers_spec.rb
201
+ - spec/berater_spec.rb
202
+ - spec/concurrency_limiter_spec.rb
203
203
  - spec/dsl_refinement_spec.rb
204
- - spec/test_mode_spec.rb
205
- - spec/middleware_spec.rb
206
204
  - spec/dsl_spec.rb
207
- - spec/lua_script_spec.rb
208
- - spec/concurrency_limiter_spec.rb
209
- - spec/riddle_spec.rb
205
+ - spec/inhibitor_spec.rb
210
206
  - spec/limiter_set_spec.rb
211
- - spec/utils_spec.rb
212
- - spec/berater_spec.rb
213
207
  - spec/limiter_spec.rb
208
+ - spec/lua_script_spec.rb
209
+ - spec/matchers_spec.rb
210
+ - spec/middleware_spec.rb
211
+ - spec/rate_limiter_spec.rb
212
+ - spec/riddle_spec.rb
214
213
  - spec/static_limiter_spec.rb
215
- - spec/inhibitor_spec.rb
214
+ - spec/test_mode_spec.rb
216
215
  - spec/unlimiter_spec.rb
216
+ - spec/utils_spec.rb