wor-batchifier 0.0.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +327 -327
- data/.rubocop.yml +25 -25
- data/Gemfile +4 -4
- data/README.md +155 -134
- data/Rakefile +6 -6
- data/lib/wor/batchifier.rb +31 -36
- data/lib/wor/batchifier/add.rb +9 -9
- data/lib/wor/batchifier/array_merge.rb +10 -10
- data/lib/wor/batchifier/exceptions.rb +11 -10
- data/lib/wor/batchifier/maintain_unique.rb +9 -9
- data/lib/wor/batchifier/merge_params/object.rb +15 -0
- data/lib/wor/batchifier/merge_params/proc.rb +15 -0
- data/lib/wor/batchifier/merge_params/symbol.rb +24 -0
- data/lib/wor/batchifier/no_response.rb +9 -9
- data/lib/wor/batchifier/strategy.rb +20 -20
- data/lib/wor/batchifier/version.rb +5 -5
- data/wor-batchifier.gemspec +33 -33
- metadata +6 -3
data/.rubocop.yml
CHANGED
@@ -1,25 +1,25 @@
|
|
1
|
-
# This is the configuration used to check the rubocop source code.
|
2
|
-
|
3
|
-
AllCops:
|
4
|
-
Exclude:
|
5
|
-
- spec/spec_helper.rb
|
6
|
-
- db/**/*
|
7
|
-
- docs/**/*
|
8
|
-
- config/**/*
|
9
|
-
- bin/**/*
|
10
|
-
- spec/**/**
|
11
|
-
|
12
|
-
Documentation:
|
13
|
-
Enabled: false
|
14
|
-
|
15
|
-
LineLength:
|
16
|
-
Max: 99
|
17
|
-
|
18
|
-
HasAndBelongsToMany:
|
19
|
-
Enabled: false
|
20
|
-
|
21
|
-
Style/FrozenStringLiteralComment:
|
22
|
-
Enabled: false
|
23
|
-
|
24
|
-
Metrics/BlockLength:
|
25
|
-
ExcludedMethods: ['register', 'describe', 'show', 'shared_examples', 'context', 'form', 'has_many', 'inputs']
|
1
|
+
# This is the configuration used to check the rubocop source code.
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
Exclude:
|
5
|
+
- spec/spec_helper.rb
|
6
|
+
- db/**/*
|
7
|
+
- docs/**/*
|
8
|
+
- config/**/*
|
9
|
+
- bin/**/*
|
10
|
+
- spec/**/**
|
11
|
+
|
12
|
+
Documentation:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
LineLength:
|
16
|
+
Max: 99
|
17
|
+
|
18
|
+
HasAndBelongsToMany:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Style/FrozenStringLiteralComment:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
Metrics/BlockLength:
|
25
|
+
ExcludedMethods: ['register', 'describe', 'show', 'shared_examples', 'context', 'form', 'has_many', 'inputs']
|
data/Gemfile
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in wor-batchifier.gemspec
|
4
|
-
gemspec
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in wor-batchifier.gemspec
|
4
|
+
gemspec
|
data/README.md
CHANGED
@@ -1,134 +1,155 @@
|
|
1
|
-
# Wolox on Rails - Batchifier
|
2
|
-
[![Gem Version](https://badge.fury.io/rb/wor-batchifier.svg)](https://badge.fury.io/rb/wor-batchifier)
|
3
|
-
[![Dependency Status](https://gemnasium.com/badges/github.com/Wolox/wor-batchifier.svg)](https://gemnasium.com/github.com/Wolox/wor-batchifier)
|
4
|
-
[![Build Status](https://travis-ci.org/Wolox/wor-batchifier.svg)](https://travis-ci.org/Wolox/wor-batchifier)
|
5
|
-
[![Code Climate](https://codeclimate.com/github/Wolox/wor-batchifier/badges/gpa.svg)](https://codeclimate.com/github/Wolox/wor-authentication)
|
6
|
-
[![Test Coverage](https://codeclimate.com/github/Wolox/wor-batchifier/badges/coverage.svg)](https://codeclimate.com/github/Wolox/wor-batchifier/coverage)
|
7
|
-
|
8
|
-
Gem that allows you to easily divide requests that
|
9
|
-
|
10
|
-
## Installation
|
11
|
-
|
12
|
-
Add this line to your application's Gemfile:
|
13
|
-
|
14
|
-
```ruby
|
15
|
-
gem 'wor-batchifier'
|
16
|
-
```
|
17
|
-
|
18
|
-
And then execute:
|
19
|
-
|
20
|
-
$ bundle
|
21
|
-
|
22
|
-
Or install it yourself as:
|
23
|
-
|
24
|
-
$ gem install wor-batchifier
|
25
|
-
|
26
|
-
## Usage
|
27
|
-
|
28
|
-
### Basic use:
|
29
|
-
|
30
|
-
The first step is to
|
31
|
-
|
32
|
-
```ruby
|
33
|
-
class
|
34
|
-
include Wor::Batchifier
|
35
|
-
end
|
36
|
-
```
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
```
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
```ruby
|
53
|
-
def bulk_request
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
1
|
+
# Wolox on Rails - Batchifier
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/wor-batchifier.svg)](https://badge.fury.io/rb/wor-batchifier)
|
3
|
+
[![Dependency Status](https://gemnasium.com/badges/github.com/Wolox/wor-batchifier.svg)](https://gemnasium.com/github.com/Wolox/wor-batchifier)
|
4
|
+
[![Build Status](https://travis-ci.org/Wolox/wor-batchifier.svg)](https://travis-ci.org/Wolox/wor-batchifier)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/Wolox/wor-batchifier/badges/gpa.svg)](https://codeclimate.com/github/Wolox/wor-authentication)
|
6
|
+
[![Test Coverage](https://codeclimate.com/github/Wolox/wor-batchifier/badges/coverage.svg)](https://codeclimate.com/github/Wolox/wor-batchifier/coverage)
|
7
|
+
|
8
|
+
Gem that allows you to easily divide processing or requests that work with a lot of information into several batches with smaller chunks of data, taking care of the result from each one and providing it joint together based on different strategies for parsing said response.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'wor-batchifier'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install wor-batchifier
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
### Basic use:
|
29
|
+
|
30
|
+
The first step is to include the Wor::Batchifier in the Class or Module you intend to use it:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
class MyClass
|
34
|
+
include Wor::Batchifier
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
If you're going to use the gem in your controllers, a good practice would be to define a parent controller from which all other controllers will have to extend to have access to the batchifier's methods. So, let's do that in our `ApplicationController.rb`:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
class ApplicationController < ActionController::Base
|
42
|
+
include Wor::Batchifier
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
You could also include the batchifier just in the controllers you intend to use it in.
|
47
|
+
|
48
|
+
The final step, is to find any request or process you wish to perform with smaller chunks of data and utilize the batchifier's methods to divide it into smaller tasks.
|
49
|
+
|
50
|
+
For example, let's pretend we have an endpoint called bulk_request that communicates with a third API and sends a lot of information to be utilized.
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
def bulk_request
|
54
|
+
ThirdAPI.bulk_request(params[:information])
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
Now we will partition that request into chunks using the batchifier as to not overburden the ThirdAPI:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
def bulk_request
|
62
|
+
execute_in_batches(params[:information], batch_size: 100, strategy: :add) do |chunks|
|
63
|
+
ThirdAPI.bulk_request(chunks)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
The batchifier will take three parameters, the first one being the information that needs to be partitioned, then the batch_size we wish to utilize and finally the symbol of the strategy that will be implemented on the response of each batch.
|
69
|
+
|
70
|
+
### Available strategies
|
71
|
+
|
72
|
+
- Add: For each request, it joins together each response no matter the result.
|
73
|
+
- Maintain-Unique: It will only add the results that are not present already in the response.
|
74
|
+
- No-Response: It will not provide any response whatsoever.
|
75
|
+
|
76
|
+
### Adding new strategies
|
77
|
+
|
78
|
+
Should you desire to add new strategies, it's as simple as creating a new class and defining a method called `merge_strategy` which will hold the logic that will be implemented to parse and merge the results from each batch. Let's look at an example:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
module Wor
|
82
|
+
module Batchifier
|
83
|
+
class MaintainUnique < Strategy
|
84
|
+
def merge_strategy(response,memo)
|
85
|
+
return response.merge(memo) { |_, v1, _| v1 }
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
```
|
91
|
+
|
92
|
+
The `merge_strategy` will receive two parameters, the first being "response" which is the total response which will be returned from `execute_in_batches`, and "memo" is the recursive response from each batch that will be added to response in each iteration. If you want to merge or do something else entirely, you have the option to do so.
|
93
|
+
|
94
|
+
All strategies have a `base_case` which by default is an empty hash `{}` but if you wish to override it, you can define your own in your strategy by simply adding a method called `base_case` which should return the value you desire for your own personal needs.
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
def base_case
|
98
|
+
# An initial step or value where the responses will be merged to.
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
The new class that will hold the method `merge_strategy` should inherit the class `Strategy`. If the strategy doesn't define the method, an exception will be raised when trying to utilize it
|
103
|
+
warning that it does not respect the contract set by the `Strategy` Interface.
|
104
|
+
|
105
|
+
You can also define a merge strategy via `Proc`, without the need of creating a new class. The `Proc` should receive two parameters: the first being "response" and the second one being "memo", both of which work the same way as they do when you create a class and define its `merge strategy`. All `Procs` have `{}` as their base case which cannot be changed. Let's look at an example:
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
merge_strategy = Proc.new do |response, memo|
|
109
|
+
memo = [] if memo.empty?
|
110
|
+
memo + response
|
111
|
+
end
|
112
|
+
|
113
|
+
execute_in_batches(collection, batch_size: 10, strategy: merge_strategy) do |chunk|
|
114
|
+
...
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
118
|
+
## Contributing
|
119
|
+
|
120
|
+
1. Fork it
|
121
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
122
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
123
|
+
4. Run rubocop lint (`rubocop -R --format simple`)
|
124
|
+
5. Run rspec tests (`bundle exec rspec`)
|
125
|
+
6. Push your branch (`git push origin my-new-feature`)
|
126
|
+
7. Create a new Pull Request
|
127
|
+
|
128
|
+
## About ##
|
129
|
+
|
130
|
+
This project is maintained by [Diego Raffo](https://github.com/enanodr) along with [Pedro Jara](https://github.com/redwarewolf) and it was written by [Wolox](http://www.wolox.com.ar).
|
131
|
+
![Wolox](https://raw.githubusercontent.com/Wolox/press-kit/master/logos/logo_banner.png)
|
132
|
+
|
133
|
+
## License
|
134
|
+
|
135
|
+
**wor-batchifier** is available under the MIT [license](https://raw.githubusercontent.com/Wolox/wor-batchifier/master/LICENSE.md).
|
136
|
+
|
137
|
+
Copyright (c) 2017 Wolox
|
138
|
+
|
139
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
140
|
+
of this software and associated documentation files (the "Software"), to deal
|
141
|
+
in the Software without restriction, including without limitation the rights
|
142
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
143
|
+
copies of the Software, and to permit persons to whom the Software is
|
144
|
+
furnished to do so, subject to the following conditions:
|
145
|
+
|
146
|
+
The above copyright notice and this permission notice shall be included in
|
147
|
+
all copies or substantial portions of the Software.
|
148
|
+
|
149
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
150
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
151
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
152
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
153
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
154
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
155
|
+
THE SOFTWARE.
|
data/Rakefile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
require 'bundler/gem_tasks'
|
2
|
-
require 'rspec/core/rake_task'
|
3
|
-
|
4
|
-
RSpec::Core::RakeTask.new(:spec)
|
5
|
-
|
6
|
-
task default: :spec
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
task default: :spec
|
data/lib/wor/batchifier.rb
CHANGED
@@ -1,36 +1,31 @@
|
|
1
|
-
require_relative 'batchifier/exceptions'
|
2
|
-
require_relative 'batchifier/strategy'
|
3
|
-
require_relative 'batchifier/add'
|
4
|
-
require_relative 'batchifier/no_response'
|
5
|
-
require_relative 'batchifier/array_merge'
|
6
|
-
require_relative 'batchifier/maintain_unique'
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
#
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
rescue NameError => e
|
33
|
-
raise Wor::Batchifier::Exceptions::StrategyNotFound
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
1
|
+
require_relative 'batchifier/exceptions'
|
2
|
+
require_relative 'batchifier/strategy'
|
3
|
+
require_relative 'batchifier/add'
|
4
|
+
require_relative 'batchifier/no_response'
|
5
|
+
require_relative 'batchifier/array_merge'
|
6
|
+
require_relative 'batchifier/maintain_unique'
|
7
|
+
require_relative 'batchifier/merge_params/object'
|
8
|
+
require_relative 'batchifier/merge_params/symbol'
|
9
|
+
require_relative 'batchifier/merge_params/proc'
|
10
|
+
|
11
|
+
module Wor
|
12
|
+
module Batchifier
|
13
|
+
using MergeParams
|
14
|
+
# This module exports the function execute_in_batches, that needs a collections and
|
15
|
+
# => optionaly a batch_size and a merge strategy. It will slice the collection and
|
16
|
+
# => apply the chozen strategy to all chunks and merge the results. It expects the responses
|
17
|
+
# => to be Hash. It can ignore them if the given strategy is no_response
|
18
|
+
def execute_in_batches(collection, batch_size: 100, strategy: :add)
|
19
|
+
collection.lazy.each_slice(batch_size).inject(strategy.merge_base_case) do |memo, chunk|
|
20
|
+
response = yield(chunk)
|
21
|
+
merge(strategy.merge_method, response, memo)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def merge(merge_method, response, memo)
|
28
|
+
merge_method.call(response, memo)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/wor/batchifier/add.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
module Wor
|
2
|
-
module Batchifier
|
3
|
-
class Add < Strategy
|
4
|
-
def merge_strategy(response,
|
5
|
-
return response.merge(
|
6
|
-
end
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
1
|
+
module Wor
|
2
|
+
module Batchifier
|
3
|
+
class Add < Strategy
|
4
|
+
def merge_strategy(response,memo)
|
5
|
+
return response.merge(memo) { |_, v1, v2| v1 + v2 }
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|