co_co_ge 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7c70cb2d58c3ae9be99a99bf619770bff2afa22c
4
+ data.tar.gz: 306ef4ee5dd360d7f8a0bb8425203cc0d4aeac2f
5
+ SHA512:
6
+ metadata.gz: 4c5dbd0e6c75abd21b1d5e4538449761157d3224f79565d10fb1f6eb3c4092f8fa1ed8c16a94631d5670766bf7e3e80537b24f5522353bb6f28de8e5908e2855
7
+ data.tar.gz: 27db9355b9530ecb87dba3710f442563443c73fa89d9e05624d02c0461ea544d6649d28ac107bf61dffb430a2a44c21a94b0e8bdcfa68d7177b79ae0b3e2963f
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ .*.sw[pon]
2
+ .AppleDouble
3
+ .DS_Store
4
+ .byebug_history
5
+ .rvmrc
6
+ Gemfile.lock
7
+ coverage
8
+ errors.lst
9
+ pkg
10
+ probe.socket
11
+ tags
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ rvm:
2
+ - 2.3.3
3
+ - 2.4.1
4
+ - ruby-head
5
+ - jruby-head
6
+ sudo: false
7
+ matrix:
8
+ allow_failures:
9
+ - rvm: ruby-head
10
+ - rvm: jruby-head
data/.utilsrc ADDED
@@ -0,0 +1,26 @@
1
+ # vim: set ft=ruby:
2
+
3
+ search do
4
+ prune_dirs /\A(\.svn|\.git|CVS|tmp|tags|coverage|pkg)\z/
5
+ skip_files /(\A\.|\.sw[pon]\z|\.(log|fnm|jpg|jpeg|png|pdf|svg)\z|tags|~\z)/i
6
+ end
7
+
8
+ discover do
9
+ prune_dirs /\A(\.svn|\.git|CVS|tmp|tags|coverage|pkg)\z/
10
+ skip_files /(\A\.|\.sw[pon]\z|\.log\z|~\z)/
11
+ binary false
12
+ end
13
+
14
+ strip_spaces do
15
+ prune_dirs /\A(\..*|CVS|pkg)\z/
16
+ skip_files /(\A\.|\.sw[pon]\z|\.log\z|~\z)/
17
+ end
18
+
19
+ probe do
20
+ test_framework :rspec
21
+ #include_dirs 'features'
22
+ end
23
+
24
+ ssh_tunnel do
25
+ terminal_multiplexer :tmux
26
+ end
data/COPYING ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [2017] [Florian Frank]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # vim: set filetype=ruby et sw=2 ts=2:
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # CoCoGe - Coupon Code Generator
2
+
3
+ ## Description
4
+
5
+ This library can be used to generate various readable or unreadable coupon
6
+ codes.
7
+
8
+ ## Installation
9
+
10
+ # gem install co_co_ge
11
+
12
+ ## Usage
13
+
14
+ require 'open-uri'
15
+ require 'co_co_ge'
16
+
17
+ symbols = CoCoGe::NgramsSymbolGenerator.new(
18
+ corpus: open('http://www.gutenberg.org/cache/epub/10/pg10.txt').read,
19
+ post_process: -> word { word.upcase.delete '^A-Z' },
20
+ length: 3
21
+ ).compute
22
+ g = CoCoGe::CodeGenerator.new(symbols: symbols, length: 6, parts_per_word: 2, separator: ?-)
23
+ g.generate # => "ILLAIN-RTHARD-ORECOR"
24
+
25
+ ## Changes
26
+
27
+
28
+ ## Download
29
+
30
+ The homepage of this library is located at
31
+
32
+ * https://github.com/flori/co\_co\_ge
33
+
34
+ ## Author
35
+
36
+ [Florian Frank](mailto:flori@ping.de)
37
+
38
+ ## License
39
+
40
+ This software is licensed under the Apache 2.0 license.
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ # vim: set filetype=ruby et sw=2 ts=2:
2
+
3
+ require 'gem_hadar'
4
+
5
+ GemHadar do
6
+ name 'co_co_ge'
7
+ author 'Florian Frank'
8
+ email 'flori@ping.de'
9
+ homepage "http://flori.github.com/#{name}"
10
+ summary 'Create a (un-)readable coupon code'
11
+ description 'This library creates (un-)readable coupon codes.'
12
+ test_dir 'spec'
13
+ ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', 'coverage', '.rvmrc',
14
+ '.AppleDouble', 'tags', '.byebug_history', '.DS_Store', 'errors.lst',
15
+ 'probe.socket'
16
+ readme 'README.md'
17
+ title "#{name.camelize}"
18
+
19
+ required_ruby_version '>= 2.3'
20
+ development_dependency 'rake'
21
+ development_dependency 'rspec'
22
+ end
23
+
24
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
data/co_co_ge.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # stub: co_co_ge 0.0.0 ruby lib
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "co_co_ge".freeze
6
+ s.version = "0.0.0"
7
+
8
+ s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
+ s.require_paths = ["lib".freeze]
10
+ s.authors = ["Florian Frank".freeze]
11
+ s.date = "2017-07-06"
12
+ s.description = "This library creates (un-)readable coupon codes.".freeze
13
+ s.email = "flori@ping.de".freeze
14
+ s.extra_rdoc_files = ["README.md".freeze, "lib/co_co_ge.rb".freeze, "lib/co_co_ge/code_generator.rb".freeze, "lib/co_co_ge/ngrams_symbol_generator.rb".freeze, "lib/co_co_ge/version.rb".freeze, "lib/co_co_ge/words_symbol_generator.rb".freeze]
15
+ s.files = [".gitignore".freeze, ".travis.yml".freeze, ".utilsrc".freeze, "COPYING".freeze, "Gemfile".freeze, "README.md".freeze, "Rakefile".freeze, "VERSION".freeze, "co_co_ge.gemspec".freeze, "code.gemspec".freeze, "lib/co_co_ge.rb".freeze, "lib/co_co_ge/code_generator.rb".freeze, "lib/co_co_ge/ngrams_symbol_generator.rb".freeze, "lib/co_co_ge/version.rb".freeze, "lib/co_co_ge/words_symbol_generator.rb".freeze, "spec/co_co_ge/code_generator_spec.rb".freeze, "spec/co_co_ge/ngrams_symbol_generator_spec.rb".freeze, "spec/co_co_ge/words_symbol_generator_spec.rb".freeze, "spec/spec_helper.rb".freeze]
16
+ s.homepage = "http://flori.github.com/co_co_ge".freeze
17
+ s.rdoc_options = ["--title".freeze, "CoCoGe".freeze, "--main".freeze, "README.md".freeze]
18
+ s.required_ruby_version = Gem::Requirement.new(">= 2.3".freeze)
19
+ s.rubygems_version = "2.6.11".freeze
20
+ s.summary = "Create a (un-)readable coupon code".freeze
21
+ s.test_files = ["spec/co_co_ge/code_generator_spec.rb".freeze, "spec/co_co_ge/ngrams_symbol_generator_spec.rb".freeze, "spec/co_co_ge/words_symbol_generator_spec.rb".freeze, "spec/spec_helper.rb".freeze]
22
+
23
+ if s.respond_to? :specification_version then
24
+ s.specification_version = 4
25
+
26
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
+ s.add_development_dependency(%q<gem_hadar>.freeze, ["~> 1.9.1"])
28
+ s.add_development_dependency(%q<rake>.freeze, [">= 0"])
29
+ s.add_development_dependency(%q<rspec>.freeze, [">= 0"])
30
+ else
31
+ s.add_dependency(%q<gem_hadar>.freeze, ["~> 1.9.1"])
32
+ s.add_dependency(%q<rake>.freeze, [">= 0"])
33
+ s.add_dependency(%q<rspec>.freeze, [">= 0"])
34
+ end
35
+ else
36
+ s.add_dependency(%q<gem_hadar>.freeze, ["~> 1.9.1"])
37
+ s.add_dependency(%q<rake>.freeze, [">= 0"])
38
+ s.add_dependency(%q<rspec>.freeze, [">= 0"])
39
+ end
40
+ end
data/code.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # stub: co_co_ge 0.0.0 ruby lib
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "co_co_ge".freeze
6
+ s.version = "0.0.0"
7
+
8
+ s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
+ s.require_paths = ["lib".freeze]
10
+ s.authors = ["Florian Frank".freeze]
11
+ s.date = "2017-07-06"
12
+ s.description = "This library creates a readable coupon code.".freeze
13
+ s.email = "flori@ping.de".freeze
14
+ s.extra_rdoc_files = ["README.md".freeze, "lib/co_co_ge.rb".freeze, "lib/co_co_ge/generator.rb".freeze, "lib/co_co_ge/ngrams_symbol_generator.rb".freeze, "lib/co_co_ge/version.rb".freeze, "lib/co_co_ge/words_symbol_generator.rb".freeze]
15
+ s.files = [".gitignore".freeze, ".utilsrc".freeze, "COPYING".freeze, "Gemfile".freeze, "README.md".freeze, "Rakefile".freeze, "VERSION".freeze, "co_co_ge.gemspec".freeze, "lib/co_co_ge.rb".freeze, "lib/co_co_ge/generator.rb".freeze, "lib/co_co_ge/ngrams_symbol_generator.rb".freeze, "lib/co_co_ge/version.rb".freeze, "lib/co_co_ge/words_symbol_generator.rb".freeze, "spec/co_co_ge/generator_spec.rb".freeze, "spec/co_co_ge/ngrams_symbol_generator_spec.rb".freeze, "spec/co_co_ge/words_symbol_generator_spec.rb".freeze, "spec/spec_helper.rb".freeze]
16
+ s.homepage = "http://flori.github.com/co_co_ge".freeze
17
+ s.rdoc_options = ["--title".freeze, "CoCoGe".freeze, "--main".freeze, "README.md".freeze]
18
+ s.required_ruby_version = Gem::Requirement.new(">= 2.3".freeze)
19
+ s.rubygems_version = "2.6.11".freeze
20
+ s.summary = "Create a readable coupon co_co_ge".freeze
21
+ s.test_files = ["spec/co_co_ge/generator_spec.rb".freeze, "spec/co_co_ge/ngrams_symbol_generator_spec.rb".freeze, "spec/co_co_ge/words_symbol_generator_spec.rb".freeze, "spec/spec_helper.rb".freeze]
22
+
23
+ if s.respond_to? :specification_version then
24
+ s.specification_version = 4
25
+
26
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
+ s.add_development_dependency(%q<gem_hadar>.freeze, ["~> 1.9.1"])
28
+ s.add_development_dependency(%q<rake>.freeze, [">= 0"])
29
+ s.add_development_dependency(%q<rspec>.freeze, [">= 0"])
30
+ else
31
+ s.add_dependency(%q<gem_hadar>.freeze, ["~> 1.9.1"])
32
+ s.add_dependency(%q<rake>.freeze, [">= 0"])
33
+ s.add_dependency(%q<rspec>.freeze, [">= 0"])
34
+ end
35
+ else
36
+ s.add_dependency(%q<gem_hadar>.freeze, ["~> 1.9.1"])
37
+ s.add_dependency(%q<rake>.freeze, [">= 0"])
38
+ s.add_dependency(%q<rspec>.freeze, [">= 0"])
39
+ end
40
+ end
@@ -0,0 +1,86 @@
1
+ require 'securerandom'
2
+
3
+ class CoCoGe::CodeGenerator
4
+ def initialize(symbols:, bits: nil, length: nil, separator: nil, parts_per_word: nil)
5
+ @symbols = symbols.to_a
6
+ @separator = separator.to_s
7
+ @parts_per_word = parts_per_word
8
+
9
+ !!bits ^ !!length or raise ArgumentError,
10
+ "either bits or length parameter is required"
11
+ if bits
12
+ @bits = bits
13
+ @length = (Math.log(1 << @bits) / Math.log(@symbols.size)).floor
14
+ else
15
+ @length = length
16
+ @bits = (Math.log(@symbols.size ** @length) / Math.log(2)).ceil
17
+ end
18
+
19
+ check_arguments
20
+ end
21
+
22
+ attr_reader :symbols
23
+
24
+ attr_reader :bits
25
+
26
+ attr_reader :length
27
+
28
+ attr_reader :separator
29
+
30
+ attr_reader :parts_per_word
31
+
32
+ def capacity
33
+ 2 ** bits
34
+ end
35
+
36
+ def generate(bytes: generate_bytes)
37
+ parts = symbolize(bytes: bytes)
38
+ if @parts_per_word
39
+ parts = parts.each_slice(@parts_per_word).map { |p| p.join }
40
+ end
41
+ parts.join(@separator)
42
+ end
43
+
44
+ def parse(code, pack_format: nil)
45
+ code = code.delete(@separator)
46
+ bytes = []
47
+ loop do
48
+ code.empty? and break
49
+ number = parse_next_symbol(code) or return
50
+ bytes << number
51
+ end
52
+ bytes
53
+ end
54
+
55
+ def symbolize(bytes: generate_bytes)
56
+ bytes.map { |b| @symbols[b % @symbols.size] }
57
+ end
58
+
59
+ private
60
+
61
+ def parse_next_symbol(code)
62
+ symbol = @symbols.each_with_index.map { |s, i|
63
+ code =~ /\A#{s}/
64
+ l = $&&.size.to_i
65
+ l.nonzero? && [ l, i ]
66
+ }.compact.max_by(&:first)
67
+ if symbol
68
+ code[0, symbol.first] = ''
69
+ symbol.last
70
+ end
71
+ end
72
+
73
+ def check_arguments
74
+ @symbols.uniq!
75
+ @symbols.size.between?(1, 255) or raise ArgumentError,
76
+ "pass between 1 and 255 symbols, was #{@symbols.size}"
77
+ @symbols.map!(&:to_s)
78
+ !@separator.empty? && @symbols.any? { |s| s.include?(@separator) } and
79
+ raise ArgumentError,
80
+ "separator #{@separator.inspect} is included in symbols"
81
+ end
82
+
83
+ def generate_bytes
84
+ SecureRandom.random_bytes(@length).bytes
85
+ end
86
+ end
@@ -0,0 +1,20 @@
1
+ class CoCoGe::NgramsSymbolGenerator
2
+ def initialize(corpus:, length:, filter: -> word { true }, post_process: -> word { word.upcase })
3
+ @corpus = corpus
4
+ @filter = filter
5
+ @length = length
6
+ @post_process = post_process
7
+ end
8
+
9
+ def compute
10
+ words = @corpus.split(/\s+/).select(&@filter).map(&@post_process)
11
+ count = Hash.new(0)
12
+ words.each do |word|
13
+ word.each_char.each_cons(@length) do |part|
14
+ part.size < @length and break
15
+ count[part.join] += 1
16
+ end
17
+ end
18
+ count.sort_by { |c| -c.last }.first(255).map(&:first)
19
+ end
20
+ end
@@ -0,0 +1,8 @@
1
+ module CoCoGe
2
+ # CoCoGe version
3
+ VERSION = '0.0.0'
4
+ VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
5
+ VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
+ VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
7
+ VERSION_BUILD = VERSION_ARRAY[2] # :nodoc:
8
+ end
@@ -0,0 +1,16 @@
1
+ class CoCoGe::WordsSymbolGenerator
2
+ def initialize(corpus:, filter: -> word { true }, post_process: -> word { word.upcase })
3
+ @corpus = corpus
4
+ @filter = filter
5
+ @post_process = post_process
6
+ end
7
+
8
+ def compute
9
+ words = @corpus.split(/\s+/).map(&@post_process).select(&@filter)
10
+ count = Hash.new(0)
11
+ words.each do |word|
12
+ count[word] += 1
13
+ end
14
+ count.sort_by { |c| -c.last }.first(255).map(&:first)
15
+ end
16
+ end
data/lib/co_co_ge.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'digest/md5'
2
+
3
+ module CoCoGe
4
+ end
5
+
6
+ require 'co_co_ge/version'
7
+ require 'co_co_ge/code_generator'
8
+ require 'co_co_ge/ngrams_symbol_generator'
9
+ require 'co_co_ge/words_symbol_generator'
@@ -0,0 +1,139 @@
1
+ require 'spec_helper'
2
+
3
+ describe CoCoGe::CodeGenerator do
4
+ before do
5
+ allow(generator).to receive(:generate_bytes).and_return 'abdef'.bytes
6
+ end
7
+
8
+ let :generator do
9
+ described_class.new symbols: (0..9), length: 5
10
+ end
11
+
12
+ context 'with bits as argument' do
13
+ let :generator do
14
+ described_class.new symbols: (0..9), bits: 40
15
+ end
16
+
17
+ it 'has correct bits' do
18
+ expect(generator.length).to eq 12
19
+ end
20
+
21
+ it 'has correct length' do
22
+ expect(generator.bits).to eq 40
23
+ end
24
+
25
+ it 'has correct capacity' do
26
+ expect(generator.capacity).to eq 2 ** 40
27
+ end
28
+ end
29
+
30
+ context 'multicharacter symbols' do
31
+ let :symbols do
32
+ %w[ FOO BAR BAZ ]
33
+ end
34
+
35
+ describe 'bits and length' do
36
+ it 'can compute the bits that can be expressed' do
37
+ expect(generator.bits).to eq 17
38
+ end
39
+
40
+ it 'has the correct length' do
41
+ expect(generator.length).to eq 5
42
+ end
43
+
44
+ it 'has correct capacity' do
45
+ expect(generator.capacity).to eq 2 ** 17
46
+ end
47
+ end
48
+
49
+ context 'with separator' do
50
+ let :generator do
51
+ described_class.new(symbols: symbols, separator: ?-, length: 5)
52
+ end
53
+
54
+ it 'can generate' do
55
+ expect(generator.generate).to eq 'BAR-BAZ-BAR-BAZ-FOO'
56
+ end
57
+
58
+ it 'can parse' do
59
+ expect(generator.parse('BAR-BAZ-BAR-BAZ-FOO')).to eq [ 1, 2, 1, 2, 0 ]
60
+ end
61
+ end
62
+
63
+ context 'without separator' do
64
+ let :generator do
65
+ described_class.new(symbols: symbols, separator: nil, length: 5)
66
+ end
67
+
68
+ it 'can generate without separator' do
69
+ expect(generator.generate).to eq 'BARBAZBARBAZFOO'
70
+ end
71
+
72
+ it 'can parse' do
73
+ expect(generator.parse('BARBAZBARBAZFOO')).to eq [ 1, 2, 1, 2, 0 ]
74
+ end
75
+ end
76
+ end
77
+
78
+ context 'singlecharacter symbols' do
79
+ let :symbols do
80
+ ?A..?Z
81
+ end
82
+
83
+ describe 'bits and length' do
84
+ it 'can compute the bits that can be expressed' do
85
+ expect(generator.bits).to eq 17
86
+ end
87
+
88
+ it 'has the correct length' do
89
+ expect(generator.length).to eq 5
90
+ end
91
+
92
+ it 'has correct capacity' do
93
+ expect(generator.capacity).to eq 2 ** 17
94
+ end
95
+ end
96
+
97
+ context 'singlecharacter symbols with separator' do
98
+ let :generator do
99
+ described_class.new(symbols: symbols, separator: ?-, length: 5)
100
+ end
101
+
102
+ it 'can generate' do
103
+ expect(generator.generate).to eq 'T-U-W-X-Y'
104
+ end
105
+
106
+ it 'can parse' do
107
+ expect(generator.parse('T-U-W-X-Y')).to eq [ 19, 20, 22, 23, 24 ]
108
+ end
109
+ end
110
+
111
+ context 'singlecharacter symbols with separator and parts_per_word' do
112
+ let :generator do
113
+ described_class.new(symbols: symbols, separator: ?-, parts_per_word: 2, length: 5)
114
+ end
115
+
116
+ it 'can generate' do
117
+ expect(generator.generate).to eq 'TU-WX-Y'
118
+ end
119
+
120
+ it 'can parse' do
121
+ expect(generator.parse('TU-WX-Y')).to eq [ 19, 20, 22, 23, 24 ]
122
+ end
123
+ end
124
+
125
+ context 'singlecharacter symbols without separator' do
126
+ let :generator do
127
+ described_class.new(symbols: symbols, separator: nil, length: 5)
128
+ end
129
+
130
+ it 'can generate without separator' do
131
+ expect(generator.generate).to eq 'TUWXY'
132
+ end
133
+
134
+ it 'can parse' do
135
+ expect(generator.parse('TUWXY')).to eq [ 19, 20, 22, 23, 24 ]
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe CoCoGe::NgramsSymbolGenerator do
4
+ let :corpus do
5
+ <<~end
6
+ In the beginning God created the Heaven, and the Earth.
7
+ end
8
+ end
9
+
10
+ let :generator do
11
+ described_class.new(
12
+ corpus: corpus,
13
+ length: 3,
14
+ filter: -> w { w.size > 3 },
15
+ post_process: -> w { w.upcase.delete '^A-Z' }
16
+ )
17
+ end
18
+
19
+ it 'can compute symbols' do
20
+ expect(generator.compute.sort).to eq\
21
+ %w[RTH EGI GIN INN NNI NIN ING CRE REA EAT ATE TED HEA EAV AVE VEN EAR
22
+ ART BEG].sort
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe CoCoGe::WordsSymbolGenerator do
4
+ let :corpus do
5
+ <<~end
6
+ In the beginning God created the Heaven, and the Earth.
7
+ end
8
+ end
9
+
10
+ let :generator do
11
+ described_class.new(
12
+ corpus: corpus,
13
+ filter: -> w { w.size > 3 },
14
+ post_process: -> w { w.upcase.delete '^A-Z' }
15
+ )
16
+ end
17
+
18
+ it 'can compute symbols' do
19
+ expect(generator.compute.sort).to eq %w[BEGINNING CREATED HEAVEN EARTH].sort
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ if ENV['START_SIMPLECOV'].to_i == 1
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter "#{File.basename(File.dirname(__FILE__))}/"
5
+ end
6
+ end
7
+ require 'rspec'
8
+ begin
9
+ require 'byebug'
10
+ rescue LoadError
11
+ end
12
+ require 'co_co_ge'
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: co_co_ge
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Florian Frank
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gem_hadar
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.9.1
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.9.1
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: This library creates (un-)readable coupon codes.
56
+ email: flori@ping.de
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files:
60
+ - README.md
61
+ - lib/co_co_ge.rb
62
+ - lib/co_co_ge/code_generator.rb
63
+ - lib/co_co_ge/ngrams_symbol_generator.rb
64
+ - lib/co_co_ge/version.rb
65
+ - lib/co_co_ge/words_symbol_generator.rb
66
+ files:
67
+ - ".gitignore"
68
+ - ".travis.yml"
69
+ - ".utilsrc"
70
+ - COPYING
71
+ - Gemfile
72
+ - README.md
73
+ - Rakefile
74
+ - VERSION
75
+ - co_co_ge.gemspec
76
+ - code.gemspec
77
+ - lib/co_co_ge.rb
78
+ - lib/co_co_ge/code_generator.rb
79
+ - lib/co_co_ge/ngrams_symbol_generator.rb
80
+ - lib/co_co_ge/version.rb
81
+ - lib/co_co_ge/words_symbol_generator.rb
82
+ - spec/co_co_ge/code_generator_spec.rb
83
+ - spec/co_co_ge/ngrams_symbol_generator_spec.rb
84
+ - spec/co_co_ge/words_symbol_generator_spec.rb
85
+ - spec/spec_helper.rb
86
+ homepage: http://flori.github.com/co_co_ge
87
+ licenses: []
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options:
91
+ - "--title"
92
+ - CoCoGe
93
+ - "--main"
94
+ - README.md
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '2.3'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.6.11
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Create a (un-)readable coupon code
113
+ test_files:
114
+ - spec/co_co_ge/code_generator_spec.rb
115
+ - spec/co_co_ge/ngrams_symbol_generator_spec.rb
116
+ - spec/co_co_ge/words_symbol_generator_spec.rb
117
+ - spec/spec_helper.rb