upc_gen 0.0.2 → 1.0.0
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 +4 -4
- data/.rubocop.yml +18 -0
- data/Gemfile +4 -3
- data/Guardfile +3 -3
- data/README.md +8 -6
- data/Rakefile +1 -1
- data/exe/upc_gen +1 -1
- data/lib/upc_gen/shell.rb +18 -15
- data/lib/upc_gen/upc_generator.rb +44 -34
- data/lib/upc_gen/version.rb +1 -1
- data/lib/upc_gen.rb +4 -4
- data/upc_gen.gemspec +12 -12
- metadata +11 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 592ae5a99b0110eac6fec211170bc5f0c77e16523832368853e4e5cb71c7bf1e
|
4
|
+
data.tar.gz: e1f5806b26bc879c00ecc35036607c4391b7b4103494a527d05965d5a83fd5e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2e8802781913349d4d3ee41229ce33cbc465b505d9de88584cadd8c1cb64c95d5bf743cc2da8a3afdf16a1951aa9a4d98dacabcbbaf7eaa46a9e3bc8d431b174
|
7
|
+
data.tar.gz: 68920cbbce0c8b7161226a97a42ec9c6e28893ae5519fc8b5dec854e694a73ac156dac892ab48b3677cf8e430bd12103d230dc93e4d31e24cd8c2d55f88d1f8f
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Layout/LineLength:
|
2
|
+
Max: 120
|
3
|
+
Style/Documentation:
|
4
|
+
Enabled: false
|
5
|
+
Style/FrozenStringLiteralComment:
|
6
|
+
Enabled: false
|
7
|
+
Style/StringLiterals:
|
8
|
+
Enabled: false
|
9
|
+
Style/BlockDelimiters:
|
10
|
+
EnforcedStyle: semantic
|
11
|
+
Layout/SpaceInsideHashLiteralBraces:
|
12
|
+
EnforcedStyle: no_space
|
13
|
+
AllCops:
|
14
|
+
TargetRubyVersion: 2.4.0
|
15
|
+
NewCops: enable
|
16
|
+
Metrics/BlockLength:
|
17
|
+
Exclude:
|
18
|
+
- 'spec/**/*_spec.rb'
|
data/Gemfile
CHANGED
@@ -3,9 +3,10 @@ source "https://rubygems.org"
|
|
3
3
|
# Specify your gem's dependencies in upc_gen.gemspec
|
4
4
|
gemspec
|
5
5
|
|
6
|
+
gem "guard-rspec"
|
7
|
+
gem "pry-byebug"
|
6
8
|
gem "rake", "~> 12.0"
|
7
|
-
gem "rspec", "~> 3.0"
|
9
|
+
gem "rspec", "~> 3.10.0"
|
8
10
|
gem "simplecov"
|
9
|
-
gem "pry-byebug"
|
10
|
-
gem "guard-rspec"
|
11
11
|
gem "solargraph"
|
12
|
+
gem "standard"
|
data/Guardfile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
guard :rspec, cmd:
|
1
|
+
guard :rspec, cmd: "rspec", failed_mode: :keep do
|
2
2
|
watch(%r{^spec/.+_spec\.rb$})
|
3
3
|
watch(%r{^spec/support/(.+)\.rb$})
|
4
|
-
watch(%r{^lib/(.+)\.rb$})
|
5
|
-
watch(
|
4
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
5
|
+
watch("spec/spec_helper.rb") { "spec" }
|
6
6
|
end
|
data/README.md
CHANGED
@@ -15,12 +15,14 @@ Run on command line like `upc_gen <optional-number-string><optional-char>`
|
|
15
15
|
- If the string of numbers is followed by a '-', a code will be found that ends with those digits, including check digit
|
16
16
|
|
17
17
|
Examples:
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
18
|
+
```
|
19
|
+
>: upc_gen 001234
|
20
|
+
2938210012347
|
21
|
+
>: upc_gen 001234.
|
22
|
+
0012348367936
|
23
|
+
>: upc_gen 001234-
|
24
|
+
4493664001234
|
25
|
+
````
|
24
26
|
|
25
27
|
## Development
|
26
28
|
|
data/Rakefile
CHANGED
data/exe/upc_gen
CHANGED
data/lib/upc_gen/shell.rb
CHANGED
@@ -1,38 +1,41 @@
|
|
1
|
-
require
|
1
|
+
require "optparse"
|
2
2
|
|
3
3
|
module UpcGen
|
4
4
|
class Shell
|
5
|
-
BANNER =
|
6
|
-
usage: #{$
|
5
|
+
BANNER = <<~"MSG".freeze
|
6
|
+
usage: #{$PROGRAM_NAME} <optional-number-string><optional-char>
|
7
7
|
|
8
|
-
Prints a random UPC code
|
8
|
+
Prints a random UPC code
|
9
9
|
|
10
|
-
If a number string is passed, the code before the check digit will end with those digits
|
11
|
-
If the string of numbers is followed by a '.', the random code will start with those digits
|
12
|
-
If the string of numbers is followed by a '-', a code will be found that ends with those digits, including check digit
|
13
|
-
|
10
|
+
If a number string is passed, the code before the check digit will end with those digits
|
11
|
+
If the string of numbers is followed by a '.', the random code will start with those digits
|
12
|
+
If the string of numbers is followed by a '-', a code will be found that ends with those digits, including check digit
|
13
|
+
MSG
|
14
14
|
|
15
|
-
def self.usage(err:
|
15
|
+
def self.usage(err: $stderr)
|
16
16
|
err.puts BANNER
|
17
17
|
exit 1
|
18
18
|
end
|
19
19
|
|
20
|
-
def self.
|
20
|
+
def self.gather_options(argv)
|
21
21
|
options = {
|
22
22
|
show_version: false
|
23
23
|
}
|
24
24
|
|
25
|
-
OptionParser.new
|
25
|
+
OptionParser.new { |parser|
|
26
26
|
parser.banner = BANNER
|
27
27
|
|
28
28
|
parser.on("-v", "--version", "Show version") do |version|
|
29
29
|
options[:version] = version
|
30
30
|
end
|
31
|
-
|
31
|
+
}.parse! argv
|
32
|
+
options
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.start(argv, out: $stdout, err: $stderr) # rubocop:disable Lint/UnusedMethodArgument
|
36
|
+
options = gather_options(argv)
|
32
37
|
|
33
|
-
if options.delete(:version)
|
34
|
-
out.puts "version: #{UpcGen::VERSION}"
|
35
|
-
end
|
38
|
+
out.puts "version: #{UpcGen::VERSION}" if options.delete(:version)
|
36
39
|
|
37
40
|
out.puts UpcGenerator.new(argv[0]).generate
|
38
41
|
end
|
@@ -1,6 +1,6 @@
|
|
1
|
-
require
|
1
|
+
require "ean13"
|
2
2
|
|
3
|
-
Encoding.default_external =
|
3
|
+
Encoding.default_external = "UTF-8"
|
4
4
|
|
5
5
|
module UpcGen
|
6
6
|
class UpcGenerator
|
@@ -8,47 +8,57 @@ module UpcGen
|
|
8
8
|
@seed = seed
|
9
9
|
end
|
10
10
|
|
11
|
-
def n_random_digits(
|
12
|
-
(1..
|
11
|
+
def n_random_digits(num)
|
12
|
+
(1..num).map { rand(10) }.join
|
13
13
|
end
|
14
14
|
|
15
|
-
def find_code_ending_on(
|
16
|
-
digits =
|
17
|
-
check =
|
18
|
-
n_random_digits(12 -
|
15
|
+
def find_code_ending_on(num)
|
16
|
+
digits = num[0...-1]
|
17
|
+
check = num[-1]
|
18
|
+
n_random_digits(12 - num.length)
|
19
19
|
candidate = EAN13.complete(n_random_digits(12 - digits.length) + digits)
|
20
|
-
while
|
21
|
-
candidate = EAN13.complete(n_random_digits(12 - digits.length) + digits)
|
22
|
-
end
|
20
|
+
candidate = EAN13.complete(n_random_digits(12 - digits.length) + digits) while candidate[-1] != check
|
23
21
|
candidate
|
24
22
|
end
|
25
23
|
|
24
|
+
def fill_number_string(number_string, random_position)
|
25
|
+
return number_string if number_string.length >= 12
|
26
|
+
|
27
|
+
case random_position
|
28
|
+
when :start
|
29
|
+
number_string + n_random_digits(12 - number_string.length)
|
30
|
+
else
|
31
|
+
n_random_digits(12 - number_string.length) + number_string
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def generate_for_seed
|
36
|
+
number_string = @seed
|
37
|
+
random_position = :end
|
38
|
+
unless /^\d+$/.match?(number_string)
|
39
|
+
random_position = :forced_end if /\d-/.match?(number_string)
|
40
|
+
random_position = :start if /\d\./.match?(number_string)
|
41
|
+
number_string = number_string.match(/^\d+/)[0]
|
42
|
+
end
|
43
|
+
|
44
|
+
return find_code_ending_on(number_string) if random_position == :forced_end
|
45
|
+
|
46
|
+
fill_number_string(number_string, random_position)
|
47
|
+
end
|
48
|
+
|
49
|
+
def complete(upc)
|
50
|
+
if [12, 13].include? upc.length
|
51
|
+
EAN13.complete upc[0...12]
|
52
|
+
else
|
53
|
+
:invalid
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
26
57
|
def generate
|
27
58
|
if @seed
|
28
|
-
|
29
|
-
random_position = :end
|
30
|
-
unless number_string =~ /^\d*$/
|
31
|
-
if number_string =~ /\d-/
|
32
|
-
random_position = :forced_end
|
33
|
-
end
|
34
|
-
if number_string =~ /\d\./
|
35
|
-
random_position = :start
|
36
|
-
end
|
37
|
-
number_string = number_string.match(/^\d+/)[0]
|
38
|
-
end
|
39
|
-
|
40
|
-
if number_string.length < 12
|
41
|
-
if random_position == :start
|
42
|
-
number_string += n_random_digits(12 - number_string.length)
|
43
|
-
elsif random_position == :forced_end
|
44
|
-
return find_code_ending_on(number_string)
|
45
|
-
else
|
46
|
-
number_string = n_random_digits(12 - number_string.length) + number_string
|
47
|
-
end
|
48
|
-
end
|
49
|
-
EAN13.complete number_string
|
59
|
+
complete generate_for_seed
|
50
60
|
else
|
51
|
-
|
61
|
+
complete n_random_digits(12)
|
52
62
|
end
|
53
63
|
end
|
54
64
|
end
|
data/lib/upc_gen/version.rb
CHANGED
data/lib/upc_gen.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
Encoding.default_external =
|
3
|
+
Encoding.default_external = "UTF-8"
|
4
4
|
|
5
5
|
require "upc_gen/version"
|
6
6
|
|
7
7
|
module UpcGen
|
8
8
|
def self.root
|
9
|
-
Pathname.new(File.absolute_path(File.join(File.dirname(__FILE__),
|
9
|
+
Pathname.new(File.absolute_path(File.join(File.dirname(__FILE__), "..")))
|
10
10
|
end
|
11
11
|
|
12
|
-
autoload :UpcGenerator
|
13
|
-
autoload :Shell
|
12
|
+
autoload :UpcGenerator, "upc_gen/upc_generator"
|
13
|
+
autoload :Shell, "upc_gen/shell"
|
14
14
|
end
|
data/upc_gen.gemspec
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "lib/upc_gen/version"
|
2
2
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
|
-
spec.name
|
5
|
-
spec.version
|
6
|
-
spec.authors
|
7
|
-
spec.email
|
4
|
+
spec.name = "upc_gen"
|
5
|
+
spec.version = UpcGen::VERSION
|
6
|
+
spec.authors = ["Tom ten Thij"]
|
7
|
+
spec.email = ["code@tomtenthij.nl"]
|
8
8
|
|
9
|
-
spec.summary
|
10
|
-
spec.required_ruby_version = Gem::Requirement.new(">= 2.
|
9
|
+
spec.summary = "Tool to generate ean13 upc barcodes, starting or ending with specific values"
|
10
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
|
11
11
|
|
12
12
|
# Specify which files should be added to the gem when it is released.
|
13
13
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
14
|
-
spec.files
|
14
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) {
|
15
15
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
-
|
17
|
-
spec.bindir
|
18
|
-
spec.executables
|
16
|
+
}
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency(
|
21
|
+
spec.add_dependency("ean13", "~> 2.0.2")
|
22
22
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: upc_gen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom ten Thij
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ean13
|
@@ -24,7 +24,7 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 2.0.2
|
27
|
-
description:
|
27
|
+
description:
|
28
28
|
email:
|
29
29
|
- code@tomtenthij.nl
|
30
30
|
executables:
|
@@ -36,6 +36,7 @@ files:
|
|
36
36
|
- ".github/workflows/ruby.yml"
|
37
37
|
- ".gitignore"
|
38
38
|
- ".rspec"
|
39
|
+
- ".rubocop.yml"
|
39
40
|
- ".travis.yml"
|
40
41
|
- CODE_OF_CONDUCT.md
|
41
42
|
- Gemfile
|
@@ -51,10 +52,10 @@ files:
|
|
51
52
|
- lib/upc_gen/upc_generator.rb
|
52
53
|
- lib/upc_gen/version.rb
|
53
54
|
- upc_gen.gemspec
|
54
|
-
homepage:
|
55
|
+
homepage:
|
55
56
|
licenses: []
|
56
57
|
metadata: {}
|
57
|
-
post_install_message:
|
58
|
+
post_install_message:
|
58
59
|
rdoc_options: []
|
59
60
|
require_paths:
|
60
61
|
- lib
|
@@ -62,15 +63,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
62
63
|
requirements:
|
63
64
|
- - ">="
|
64
65
|
- !ruby/object:Gem::Version
|
65
|
-
version: 2.
|
66
|
+
version: 2.4.0
|
66
67
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
68
|
requirements:
|
68
69
|
- - ">="
|
69
70
|
- !ruby/object:Gem::Version
|
70
71
|
version: '0'
|
71
72
|
requirements: []
|
72
|
-
rubygems_version: 3.
|
73
|
-
signing_key:
|
73
|
+
rubygems_version: 3.1.4
|
74
|
+
signing_key:
|
74
75
|
specification_version: 4
|
75
|
-
summary:
|
76
|
+
summary: Tool to generate ean13 upc barcodes, starting or ending with specific values
|
76
77
|
test_files: []
|