leakybucket 0.0.2
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 +7 -0
- data/.gitignore +21 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +27 -0
- data/LICENSE.txt +22 -0
- data/README.md +36 -0
- data/Rakefile +5 -0
- data/leakybucket.gemspec +24 -0
- data/lib/leakybucket.rb +5 -0
- data/lib/leakybucket/bucket.rb +35 -0
- data/lib/leakybucket/version.rb +3 -0
- data/spec/leakybucket_spec.rb +56 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7fcc79980b6028227e6bb9da5cdbad37a08611e0
|
4
|
+
data.tar.gz: b3b788992af595d277bc4c4c9c83be4f9624c50d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b919b59f338b30ac0842f441276efabdaa1d4e8d9b0d5150aa9a989ef79713a7847e4db82caf4f2aeeab6898106711466e064bc1bd57b5cd266d5473ff9eae64
|
7
|
+
data.tar.gz: 8de1698298a0ca5ecd23f1109f4477bc4b6b9fcbd5a2c8fa2cf04cb46bc19ca1aa21b973cc7f392886819120a22d24a4fa464500aa3469c75146f00d71256af7
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
leakybucket (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.2.3)
|
10
|
+
rake (10.0.4)
|
11
|
+
rspec (2.13.0)
|
12
|
+
rspec-core (~> 2.13.0)
|
13
|
+
rspec-expectations (~> 2.13.0)
|
14
|
+
rspec-mocks (~> 2.13.0)
|
15
|
+
rspec-core (2.13.1)
|
16
|
+
rspec-expectations (2.13.0)
|
17
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
18
|
+
rspec-mocks (2.13.1)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
bundler (~> 1.3)
|
25
|
+
leakybucket!
|
26
|
+
rake
|
27
|
+
rspec (~> 2.6)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Lukas Elmer
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Leakybucket
|
2
|
+
|
3
|
+
[](https://travis-ci.org/lukaselmer/leakybucket)
|
4
|
+
[](https://gemnasium.com/lukaselmer/leakybucket)
|
5
|
+
[](https://codeclimate.com/github/lukaselmer/leakybucket)
|
6
|
+
|
7
|
+
Provides an API to manage leaky buckets.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'leakybucket'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install leakybucket
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
https://github.com/lukaselmer/leakybucket
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
36
|
+
|
data/Rakefile
ADDED
data/leakybucket.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'leakybucket/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "leakybucket"
|
8
|
+
spec.version = Leakybucket::VERSION
|
9
|
+
spec.authors = ["Lukas Elmer"]
|
10
|
+
spec.email = ["lukas.elmer@gmail.com"]
|
11
|
+
spec.description = %q{Leaky bucket API.}
|
12
|
+
spec.summary = %q{Provides an API to manage leaky buckets.}
|
13
|
+
spec.homepage = "https://github.com/lukaselmer/leakybucket"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec", "~> 2.6"
|
24
|
+
end
|
data/lib/leakybucket.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
class Leakybucket::Bucket
|
2
|
+
|
3
|
+
attr_accessor :limit, :value, :leaking_callback
|
4
|
+
|
5
|
+
def initialize(options = {})
|
6
|
+
self.limit = options[:limit] || default_options[:limit]
|
7
|
+
self.value = limit
|
8
|
+
end
|
9
|
+
|
10
|
+
def default_options
|
11
|
+
{limit: 3}
|
12
|
+
end
|
13
|
+
|
14
|
+
def decrement
|
15
|
+
self.value -= 1
|
16
|
+
leaking! if leaking?
|
17
|
+
end
|
18
|
+
|
19
|
+
def increment
|
20
|
+
return if value >= limit
|
21
|
+
self.value += 1
|
22
|
+
end
|
23
|
+
|
24
|
+
def leaking?
|
25
|
+
value < 0
|
26
|
+
end
|
27
|
+
|
28
|
+
def leaking!
|
29
|
+
leaking_callback.(value) if leaking_callback.respond_to?(:call)
|
30
|
+
end
|
31
|
+
|
32
|
+
def reset
|
33
|
+
self.value = limit
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'leakybucket'
|
2
|
+
|
3
|
+
describe Leakybucket::Bucket do
|
4
|
+
it 'should creates leaky bucket' do
|
5
|
+
l1 = Leakybucket::Bucket.new(limit: 3)
|
6
|
+
l1.value.should eql(3)
|
7
|
+
l2 = Leakybucket::Bucket.new(limit: 5)
|
8
|
+
l2.value.should eql(5)
|
9
|
+
l3 = Leakybucket::Bucket.new
|
10
|
+
l3.value.should eql(3)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should decrement leaky bucket' do
|
14
|
+
l = Leakybucket::Bucket.new(limit: 3)
|
15
|
+
l.decrement
|
16
|
+
l.value.should eql(2)
|
17
|
+
l.decrement
|
18
|
+
l.value.should eql(1)
|
19
|
+
l.decrement
|
20
|
+
l.value.should eql(0)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should leak on -1' do
|
24
|
+
l = Leakybucket::Bucket.new(limit: 3) # value is 3
|
25
|
+
l.decrement # value is 2
|
26
|
+
l.decrement # value is 1
|
27
|
+
l.decrement # value is 0
|
28
|
+
l.leaking?.should eql(false)
|
29
|
+
l.decrement # value is -1
|
30
|
+
l.leaking?.should eql(true)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should reset to limit' do
|
34
|
+
l = Leakybucket::Bucket.new(limit: 3) # value is 3
|
35
|
+
l.decrement # value is 1
|
36
|
+
l.reset
|
37
|
+
l.value.should eql(3)
|
38
|
+
l.limit = 10
|
39
|
+
l.value.should eql(3)
|
40
|
+
l.reset
|
41
|
+
l.value.should eql(10)
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should call leaking method when leaking on decrement' do
|
45
|
+
l = Leakybucket::Bucket.new(limit: 1)
|
46
|
+
out = ''
|
47
|
+
l.leaking_callback = ->(value){out = "leaking with value #{value}"}
|
48
|
+
out.should eql('')
|
49
|
+
l.decrement
|
50
|
+
out.should eql('')
|
51
|
+
l.decrement
|
52
|
+
out.should eql('leaking with value -1')
|
53
|
+
l.decrement
|
54
|
+
out.should eql('leaking with value -2')
|
55
|
+
end
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: leakybucket
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lukas Elmer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-04-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.6'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.6'
|
55
|
+
description: Leaky bucket API.
|
56
|
+
email:
|
57
|
+
- lukas.elmer@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .travis.yml
|
64
|
+
- Gemfile
|
65
|
+
- Gemfile.lock
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- leakybucket.gemspec
|
70
|
+
- lib/leakybucket.rb
|
71
|
+
- lib/leakybucket/bucket.rb
|
72
|
+
- lib/leakybucket/version.rb
|
73
|
+
- spec/leakybucket_spec.rb
|
74
|
+
homepage: https://github.com/lukaselmer/leakybucket
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.0.0
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Provides an API to manage leaky buckets.
|
98
|
+
test_files:
|
99
|
+
- spec/leakybucket_spec.rb
|