seshbot-packing 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +21 -0
- data/README.md +40 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/seshbot/packing.rb +3 -0
- data/lib/seshbot/packing/package.rb +94 -0
- data/lib/seshbot/packing/recipe.rb +102 -0
- data/lib/seshbot/packing/version.rb +5 -0
- data/seshbot-packing.gemspec +27 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fb6f487a85ab3bea24e16e9d12c02ae511a58e5e79b2cf76e647cc0c064e8db0
|
4
|
+
data.tar.gz: 347038a58ec75af02cd61551cc05210257f49fee6ef2841c359a01cdc388ccc7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 62b68bf0c4ec15a201d60a33d0528bef7d7357178be258743eb6e1fe0f026c2bf03800ddd4310ca9b9dcad2bc7cd52b999bbfe4c00b02012a95f5df3c014c7d0
|
7
|
+
data.tar.gz: 372ff6ce363c20a1d82aea75c0bfd460acdd04b67c2d37967f4a51468b72bad2b8e585bd512a594537688e60ccbbef5a2f3a2c057998ac166689dcc78f4ab758
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at storkyd@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
seshbot-packing (0.2.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
minitest (5.14.2)
|
10
|
+
rake (12.3.3)
|
11
|
+
|
12
|
+
PLATFORMS
|
13
|
+
ruby
|
14
|
+
|
15
|
+
DEPENDENCIES
|
16
|
+
minitest (~> 5.0)
|
17
|
+
rake (~> 12.0)
|
18
|
+
seshbot-packing!
|
19
|
+
|
20
|
+
BUNDLED WITH
|
21
|
+
2.1.4
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# Seshbot::Packing
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/seshbot/packing`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'seshbot-packing'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install seshbot-packing
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/seshbot-packing. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/seshbot-packing/blob/master/CODE_OF_CONDUCT.md).
|
36
|
+
|
37
|
+
|
38
|
+
## Code of Conduct
|
39
|
+
|
40
|
+
Everyone interacting in the Seshbot::Packing project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/seshbot-packing/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "seshbot/packing"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require "date"
|
2
|
+
module Seshbot
|
3
|
+
module Packing
|
4
|
+
class Error < StandardError; end
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def count_product_types(items)
|
8
|
+
result = Hash.new(0)
|
9
|
+
# create a hash were the keys are sku_fragment, and the values are the summed quantities
|
10
|
+
items.each do |item|
|
11
|
+
size = packaged_size(item["sku"])
|
12
|
+
result[size] += item["quantity"]
|
13
|
+
end
|
14
|
+
result
|
15
|
+
end
|
16
|
+
|
17
|
+
def packaged_size(sku)
|
18
|
+
# Takes an sku and returns the size
|
19
|
+
result = sku[/(L\d{2}|U\d{2}|B\d{3}|C\d{3}|M\d{3}|P\d{2})/]
|
20
|
+
if result == "B502"
|
21
|
+
result = "B306"
|
22
|
+
end
|
23
|
+
if result == "B504"
|
24
|
+
result = "B312"
|
25
|
+
end
|
26
|
+
result.gsub!(/^C/, 'B')
|
27
|
+
result
|
28
|
+
end
|
29
|
+
|
30
|
+
def bundle_items(items, fulfilled_at: nil)
|
31
|
+
if fulfilled_at
|
32
|
+
bundling_start_date = DateTime.new(2020, 1, 14, 8, 0, 0, Time.new.zone)
|
33
|
+
result = items.map { |item| { "sku" => item.sku, "quantity" => item.quantity, "price" => item.price } }
|
34
|
+
return result if fulfilled_at < bundling_start_date # TODO remove bundling start date method
|
35
|
+
end
|
36
|
+
|
37
|
+
product_type_prices = Hash.new(0)
|
38
|
+
items.each do |item|
|
39
|
+
size = packaged_size(item["sku"])
|
40
|
+
product_type_prices[size] += item["price"]
|
41
|
+
end
|
42
|
+
|
43
|
+
# get a hash of {pack_6: 5, pack_12: 1}
|
44
|
+
product_type_counts = count_product_types(items)
|
45
|
+
|
46
|
+
# dismantle packages into individual units (e.g., {pack_6: 7})
|
47
|
+
product_type_counts = unpack(product_type_counts)
|
48
|
+
|
49
|
+
# repackage into the 'best' packaging we can figure out (e.g., {pack_12: 2})
|
50
|
+
product_type_counts = pack(product_type_counts)
|
51
|
+
|
52
|
+
new_line_items = product_type_counts.map { |k,v| { "sku" => "BUND-#{k}", "quantity" => v, "price" => 0} }
|
53
|
+
new_line_items
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
def unpack(product_type_counts)
|
58
|
+
result = pack_single_step(product_type_counts, unpacking: true)
|
59
|
+
result
|
60
|
+
end
|
61
|
+
|
62
|
+
def pack_single_step(product_type_counts, unpacking:)
|
63
|
+
result = Hash.new(0)
|
64
|
+
product_type_counts.each do |sku_fragment, qty|
|
65
|
+
recipe = Recipe::find_best_recipe(sku_fragment, qty, unpacking: unpacking)
|
66
|
+
if recipe.nil?
|
67
|
+
result[sku_fragment] += qty
|
68
|
+
next
|
69
|
+
end
|
70
|
+
|
71
|
+
recipe_quantities = Recipe::apply_recipe(recipe, qty)
|
72
|
+
result[recipe["input_fragment"]] += recipe_quantities[:input_quantity]
|
73
|
+
result[recipe["output_fragment"]] += recipe_quantities[:output_quantity]
|
74
|
+
end
|
75
|
+
result.delete_if { |k,v| v == 0}
|
76
|
+
result
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
def pack(product_type_counts)
|
81
|
+
final_result = product_type_counts
|
82
|
+
|
83
|
+
# keep trying to 'pack' until it stabilises (e.g., 6x6pack => 1x24pack+2x6pack => 1x24pack+1x12pack)
|
84
|
+
while true
|
85
|
+
new_result = pack_single_step(final_result, unpacking: false)
|
86
|
+
# no changes, break out
|
87
|
+
break if new_result == final_result
|
88
|
+
final_result = new_result
|
89
|
+
end
|
90
|
+
final_result
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
module Seshbot
|
2
|
+
module Packing
|
3
|
+
module Recipe
|
4
|
+
class << self
|
5
|
+
def get_recipes
|
6
|
+
{
|
7
|
+
"twenty_four_pack_4"=>{
|
8
|
+
"input_fragment"=>"B306",
|
9
|
+
"input_quantity"=>4,
|
10
|
+
"output_fragment"=>"B324",
|
11
|
+
"output_quantity"=>1
|
12
|
+
},
|
13
|
+
"twenty_four_pack_3"=>{
|
14
|
+
"input_fragment"=>"B306",
|
15
|
+
"input_quantity"=>3,
|
16
|
+
"output_fragment"=>"B318",
|
17
|
+
"output_quantity"=>1
|
18
|
+
},
|
19
|
+
"twelve_pack"=>{
|
20
|
+
"input_fragment"=>"B306",
|
21
|
+
"input_quantity"=>2,
|
22
|
+
"output_fragment"=>"B312",
|
23
|
+
"output_quantity"=>1
|
24
|
+
},
|
25
|
+
"cans_twenty_four_pack_4"=>{
|
26
|
+
"input_fragment"=>"C306",
|
27
|
+
"input_quantity"=>4,
|
28
|
+
"output_fragment"=>"C324",
|
29
|
+
"output_quantity"=>1
|
30
|
+
},
|
31
|
+
"cans_twenty_four_pack_3"=>{
|
32
|
+
"input_fragment"=>"C306",
|
33
|
+
"input_quantity"=>3,
|
34
|
+
"output_fragment"=>"C318",
|
35
|
+
"output_quantity"=>1
|
36
|
+
},
|
37
|
+
"cans_twelve_pack"=>{
|
38
|
+
"input_fragment"=>"C306",
|
39
|
+
"input_quantity"=>2,
|
40
|
+
"output_fragment"=>"C312",
|
41
|
+
"output_quantity"=>1
|
42
|
+
}
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
def find_best_recipe(sku_fragment, qty, unpacking: false)
|
47
|
+
recipes = find_recipes(sku_fragment, qty, unpacking: unpacking)
|
48
|
+
# we want the recipe that is 'best' (packaging into as few items as possible, or unpacaging into as many as possible)
|
49
|
+
best_recipe_quantities = nil
|
50
|
+
recipes.each do |name, recipe|
|
51
|
+
recipe_quantities = apply_recipe(recipe, qty)
|
52
|
+
|
53
|
+
# if there was no 'best' until now, just set it (makes the next step a bit simpler)
|
54
|
+
best_recipe_quantities ||= recipe_quantities
|
55
|
+
|
56
|
+
# is this recipe the 'best'? (if packaging, best is the one that creates fewest quantity)
|
57
|
+
is_best = unpacking ?
|
58
|
+
recipe_quantities[:output_quantity] > best_recipe_quantities[:output_quantity] :
|
59
|
+
recipe_quantities[:output_quantity] < best_recipe_quantities[:output_quantity]
|
60
|
+
|
61
|
+
if is_best
|
62
|
+
best_recipe_quantities = recipe_quantities
|
63
|
+
end
|
64
|
+
end
|
65
|
+
best_recipe_quantities[:recipe] if best_recipe_quantities
|
66
|
+
end
|
67
|
+
|
68
|
+
def find_recipes(sku_fragment, qty, unpacking: false)
|
69
|
+
recipes = get_recipes
|
70
|
+
recipes = reverse_recipes(recipes) if unpacking
|
71
|
+
recipes.select do |name, recipe|
|
72
|
+
recipe["input_fragment"] == sku_fragment && qty >= recipe["input_quantity"]
|
73
|
+
end
|
74
|
+
# recipes.select { |name, recipe| recipe[:input_fragment] == sku_fragment && qty >= recipe[:input_quantity] }
|
75
|
+
end
|
76
|
+
|
77
|
+
def reverse_recipes(recipes)
|
78
|
+
results = recipes.map do |recipe_name, recipe_details|
|
79
|
+
new_recipe_details = {
|
80
|
+
"input_fragment" => recipe_details["output_fragment"],
|
81
|
+
"input_quantity" => recipe_details["output_quantity"],
|
82
|
+
"output_fragment" => recipe_details["input_fragment"],
|
83
|
+
"output_quantity" => recipe_details["input_quantity"]
|
84
|
+
}
|
85
|
+
[recipe_name, new_recipe_details]
|
86
|
+
end
|
87
|
+
results.to_h
|
88
|
+
end
|
89
|
+
|
90
|
+
def apply_recipe(recipe, input_quantity)
|
91
|
+
multiplier, remainder = input_quantity.divmod(recipe["input_quantity"])
|
92
|
+
{
|
93
|
+
recipe: recipe,
|
94
|
+
input_quantity: remainder,
|
95
|
+
output_quantity: multiplier * recipe["output_quantity"]
|
96
|
+
}
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative 'lib/seshbot/packing/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "seshbot-packing"
|
5
|
+
spec.version = Seshbot::Packing::VERSION
|
6
|
+
spec.authors = ["Shaun"]
|
7
|
+
spec.email = ["shaun@brewkeeper.com.au"]
|
8
|
+
|
9
|
+
spec.summary = %q{Receives, calculates, returns skus with new quantities}
|
10
|
+
spec.homepage = "https://gitlab.com/brewkeeper/seshbot-packing"
|
11
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
12
|
+
|
13
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = "https://gitlab.com/brewkeeper/seshbot-packing"
|
17
|
+
spec.metadata["changelog_uri"] = "https://gitlab.com/brewkeeper/seshbot-packing/~blob/master/CHANGELOG.md"
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: seshbot-packing
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shaun
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-11-15 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- shaun@brewkeeper.com.au
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".travis.yml"
|
22
|
+
- CODE_OF_CONDUCT.md
|
23
|
+
- Gemfile
|
24
|
+
- Gemfile.lock
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- bin/console
|
28
|
+
- bin/setup
|
29
|
+
- lib/seshbot/packing.rb
|
30
|
+
- lib/seshbot/packing/package.rb
|
31
|
+
- lib/seshbot/packing/recipe.rb
|
32
|
+
- lib/seshbot/packing/version.rb
|
33
|
+
- seshbot-packing.gemspec
|
34
|
+
homepage: https://gitlab.com/brewkeeper/seshbot-packing
|
35
|
+
licenses: []
|
36
|
+
metadata:
|
37
|
+
homepage_uri: https://gitlab.com/brewkeeper/seshbot-packing
|
38
|
+
source_code_uri: https://gitlab.com/brewkeeper/seshbot-packing
|
39
|
+
changelog_uri: https://gitlab.com/brewkeeper/seshbot-packing/~blob/master/CHANGELOG.md
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 2.3.0
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubygems_version: 3.1.4
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: Receives, calculates, returns skus with new quantities
|
59
|
+
test_files: []
|