roundrobin 0.0.3
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 +15 -0
- data/.rspec +2 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +2 -0
- data/lib/roundrobin.rb +30 -0
- data/lib/roundrobin/version.rb +3 -0
- data/roundrobin.gemspec +27 -0
- data/spec/roundrobin_spec.rb +38 -0
- data/spec/spec_helper.rb +10 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9f0002e86d6baa2e66a1f102d6085088d7c2d0a7
|
4
|
+
data.tar.gz: 4a16a3a024e757b43036a014b55fbe3c8477057b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 795d752ec3f6eb30e872982ed51aee6eeb5a7c5438b53dc272bb42384d94398794dab312b40c725131de9c25b7463cd667d7593308ec5fce9b62ba6740582424
|
7
|
+
data.tar.gz: 48bac9c4331dec2628c411856242188b788376cce85df464370bad4ad08d12772e97c7cdf316cd0142fc39c4fb49064ec45fca238f221788ed800b47e11633c8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Sergio Márquez
|
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,54 @@
|
|
1
|
+
# RoundRobin
|
2
|
+
|
3
|
+
Add a Round-Robin based process to your flow.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'roundrobin'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install roundrobin
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```rb
|
24
|
+
>> Roundrobin.next(%w(foo bar))
|
25
|
+
=> "foo"
|
26
|
+
>> Roundrobin.next(%w(foo bar))
|
27
|
+
=> "bar"
|
28
|
+
>> Roundrobin.next(%w(foo bar))
|
29
|
+
=> "foo"
|
30
|
+
|
31
|
+
>> candidates = [{name: 'John', email: 'john@example.com'}, {name: 'Sara', email: 'sara@example.com'}])
|
32
|
+
>> Roundrobin.next(candidates)
|
33
|
+
=> {name: 'John', email: 'john@example.com'}
|
34
|
+
>> Roundrobin.next(candidates)
|
35
|
+
=> {name: 'Sara', email: 'sara@example.com'}
|
36
|
+
>> Roundrobin.next(candidates)
|
37
|
+
=> {name: 'John', email: 'john@example.com'}
|
38
|
+
```
|
39
|
+
|
40
|
+
All the data will be persisted to your DB, so you can restart your server, or do this in multiple threads and the results will be the same: you will be using the Round Robin algorith.
|
41
|
+
|
42
|
+
## Development
|
43
|
+
|
44
|
+
$ cd roundrobin
|
45
|
+
$ gem build roundrobin.gemspec
|
46
|
+
$ gem install ./roundrobin-XXX.gem
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it ( https://github.com/[my-github-username]/roundrobin/fork )
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/roundrobin.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require "roundrobin/version"
|
2
|
+
require 'digest/sha1'
|
3
|
+
require 'redis'
|
4
|
+
|
5
|
+
class Roundrobin
|
6
|
+
|
7
|
+
def initialize(redis_conn = nil)
|
8
|
+
@redis = redis_conn.nil? ? Redis.new : Redis.new(url: redis_conn)
|
9
|
+
end
|
10
|
+
|
11
|
+
def next(candidates)
|
12
|
+
return nil unless candidates.is_a?(Array)
|
13
|
+
identifier = get_hash(candidates)
|
14
|
+
iterator = @redis.get(identifier)
|
15
|
+
if iterator.nil?
|
16
|
+
iterator = -1
|
17
|
+
else
|
18
|
+
iterator = iterator.to_i
|
19
|
+
end
|
20
|
+
iterator += 1
|
21
|
+
iterator = 0 if iterator >= candidates.length
|
22
|
+
@redis.set(identifier, iterator)
|
23
|
+
candidates[iterator]
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def get_hash(candidates)
|
28
|
+
Digest::SHA1.hexdigest candidates.to_s
|
29
|
+
end
|
30
|
+
end
|
data/roundrobin.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'roundrobin/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "roundrobin"
|
8
|
+
spec.version = Roundrobin::VERSION
|
9
|
+
spec.authors = ["Gustavo Garcia", "Sergio Márquez"]
|
10
|
+
spec.email = ["dev@archdaily.com"]
|
11
|
+
spec.summary = "Iterates over an array with a persisted Round-Robin algorithm"
|
12
|
+
spec.description = "Set the array in a yml file and then just ask for the next item"
|
13
|
+
spec.homepage = "http://github.com/pnet/roundrobin"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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_dependency "redis"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", ">= 1.3.5"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "fakeredis"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Roundrobin, :type => :model do
|
4
|
+
let(:rr) {Roundrobin.new}
|
5
|
+
let(:candidates) {['candidate1','candidate2', 'candidate3']}
|
6
|
+
|
7
|
+
describe "next" do
|
8
|
+
it "returns nil when the candidates are not a valid array" do
|
9
|
+
expect(rr.next('123')).to be_nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns the first candidate the first time" do
|
13
|
+
expect(rr.next(candidates)).to eq("candidate1")
|
14
|
+
end
|
15
|
+
|
16
|
+
it "returns the second one after the first one" do
|
17
|
+
rr.next(candidates)
|
18
|
+
expect(rr.next(candidates)).to eq('candidate2')
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns the first candidate after returning the last one" do
|
22
|
+
rr.next(candidates)
|
23
|
+
rr.next(candidates)
|
24
|
+
rr.next(candidates)
|
25
|
+
expect(rr.next(candidates)).to eq('candidate1')
|
26
|
+
end
|
27
|
+
|
28
|
+
it "works with hashs" do
|
29
|
+
candidates_hash = [{name: 'frank', email: 'frank@example.com'},{name: 'alex', email: 'alex@example.com'}]
|
30
|
+
expect(rr.next(candidates_hash)[:name]).to eq('frank')
|
31
|
+
expect(rr.next(candidates_hash)[:name]).to eq('alex')
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'works with integers' do
|
35
|
+
expect(rr.next([1,2,3])).to eq(1)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: roundrobin
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gustavo Garcia
|
8
|
+
- Sergio Márquez
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-08-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: redis
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - '>='
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - '>='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: bundler
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.3.5
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 1.3.5
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ~>
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '10.0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '10.0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: fakeredis
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rspec
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
description: Set the array in a yml file and then just ask for the next item
|
85
|
+
email:
|
86
|
+
- dev@archdaily.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- .gitignore
|
92
|
+
- .rspec
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- lib/roundrobin.rb
|
98
|
+
- lib/roundrobin/version.rb
|
99
|
+
- roundrobin.gemspec
|
100
|
+
- spec/roundrobin_spec.rb
|
101
|
+
- spec/spec_helper.rb
|
102
|
+
homepage: http://github.com/pnet/roundrobin
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.0.14
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Iterates over an array with a persisted Round-Robin algorithm
|
126
|
+
test_files:
|
127
|
+
- spec/roundrobin_spec.rb
|
128
|
+
- spec/spec_helper.rb
|