coupon_code 0.0.2 → 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 +5 -5
- data/.github/dependabot.yml +8 -0
- data/.github/workflows/ruby.yml +36 -0
- data/.ruby-version +1 -0
- data/.standard.yml +2 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +1 -3
- data/README-ko.md +1 -1
- data/README.md +8 -1
- data/Rakefile +3 -3
- data/coupon_code.gemspec +19 -20
- data/lib/coupon_code/version.rb +1 -1
- data/lib/coupon_code.rb +18 -16
- data/spec/coupon_code_spec.rb +32 -25
- data/spec/spec_helper.rb +52 -54
- metadata +26 -14
- data/.hound.yml +0 -3
- data/.rubocop.yml +0 -5
- data/.travis.yml +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 76d25c8d75ec9f94df915aed8a41ff6141cea7e20d2342c09412763916d81869
|
4
|
+
data.tar.gz: f1ffd10712e8e966276d4b54b466aea30ffc377c76e8044d8d437a7f1efff275
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62f908f274676f03fb33adf0c8f3243171619df9a636bd00016cd2ab8641468436fdb638ca690cac63059d9ec2880fddde832efe2a6c3afa885c5e83af012128
|
7
|
+
data.tar.gz: 8abdcecb97a48f026eafda0b794149594a29425458da24e1308827506aaa63cb2e30ae36254b8e190f8cb665eddae71842bdd8193b84075d9a3ab079ddb5a107
|
@@ -0,0 +1,36 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- '*'
|
7
|
+
pull_request:
|
8
|
+
branches:
|
9
|
+
- '*'
|
10
|
+
|
11
|
+
jobs:
|
12
|
+
test:
|
13
|
+
runs-on: ubuntu-latest
|
14
|
+
|
15
|
+
strategy:
|
16
|
+
matrix:
|
17
|
+
ruby-version: [ '2.6', '2.7', '3.0', '3.1', '3.2', '3.3', '3.4' ]
|
18
|
+
|
19
|
+
steps:
|
20
|
+
- name: Checkout code
|
21
|
+
uses: actions/checkout@v2
|
22
|
+
|
23
|
+
- name: Set up Ruby
|
24
|
+
uses: ruby/setup-ruby@v1
|
25
|
+
with:
|
26
|
+
ruby-version: ${{ matrix.ruby-version }}
|
27
|
+
bundler-cache: true
|
28
|
+
|
29
|
+
- name: Install dependencies
|
30
|
+
run: bundle install
|
31
|
+
|
32
|
+
- name: Run standardrb
|
33
|
+
run: bundle exec standardrb
|
34
|
+
|
35
|
+
- name: Run tests
|
36
|
+
run: bundle exec rspec
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.3.4
|
data/.standard.yml
ADDED
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/README-ko.md
CHANGED
data/README.md
CHANGED
@@ -48,10 +48,17 @@ You can change the number of parts of the generated code by passing an option ha
|
|
48
48
|
>> CouponCode.validate("1K7Q-CTFM-LMTC-DLGP", 4)
|
49
49
|
=> "1K7Q-CTFM-LMTC-DLGP"
|
50
50
|
|
51
|
+
You can also change the length of each part like:
|
52
|
+
|
53
|
+
>> CouponCode.generate(parts: 3, part_length: 5)
|
54
|
+
=> "GRG65-X0PF4-KP7TJ"
|
55
|
+
>> CouponCode.validate("1K7Q-CTFM-LMTC-DLGP", 3, 5)
|
56
|
+
=> "1K7Q-CTFM-LMTC-DLGP"
|
57
|
+
|
51
58
|
## Testing
|
52
59
|
|
53
60
|
```ruby
|
54
|
-
$ bundle exec rake
|
61
|
+
$ bundle exec rake spec
|
55
62
|
```
|
56
63
|
|
57
64
|
## Thanks to
|
data/Rakefile
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
3
|
|
4
4
|
RSpec::Core::RakeTask.new(:spec)
|
5
5
|
|
6
6
|
task default: :spec
|
7
7
|
|
8
8
|
task :console do
|
9
|
-
exec
|
9
|
+
exec "irb -r coupon_code -I ./lib"
|
10
10
|
end
|
data/coupon_code.gemspec
CHANGED
@@ -1,27 +1,26 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
3
|
+
require "coupon_code/version"
|
5
4
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name
|
8
|
-
spec.version
|
9
|
-
spec.authors
|
10
|
-
spec.email
|
11
|
-
spec.summary
|
12
|
-
spec.description
|
13
|
-
|
14
|
-
spec.homepage
|
15
|
-
spec.license
|
6
|
+
spec.name = "coupon_code"
|
7
|
+
spec.version = CouponCode::VERSION
|
8
|
+
spec.authors = ["Sanghyun Park"]
|
9
|
+
spec.email = ["sh@baxang.com"]
|
10
|
+
spec.summary = "Generate and validate coupon codes."
|
11
|
+
spec.description = "A Ruby implementation of Perl's" \
|
12
|
+
" Algorithm::CouponCode CPAN module."
|
13
|
+
spec.homepage = "https://github.com/baxang/coupon-code"
|
14
|
+
spec.license = "MIT"
|
16
15
|
|
17
|
-
spec.files
|
18
|
-
spec.executables
|
19
|
-
spec.
|
20
|
-
spec.require_paths = ['lib']
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
21
19
|
|
22
|
-
spec.required_ruby_version =
|
20
|
+
spec.required_ruby_version = ">= 2.6.0"
|
23
21
|
|
24
|
-
spec.add_development_dependency
|
25
|
-
spec.add_development_dependency
|
26
|
-
spec.add_development_dependency
|
22
|
+
spec.add_development_dependency "bundler"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.4"
|
25
|
+
spec.add_development_dependency "standardrb"
|
27
26
|
end
|
data/lib/coupon_code/version.rb
CHANGED
data/lib/coupon_code.rb
CHANGED
@@ -1,39 +1,41 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require "coupon_code/version"
|
2
|
+
require "securerandom"
|
3
|
+
require "digest/sha1"
|
4
4
|
|
5
5
|
module CouponCode
|
6
|
-
SYMBOL =
|
7
|
-
PARTS
|
6
|
+
SYMBOL = "0123456789ABCDEFGHJKLMNPQRTUVWXY"
|
7
|
+
PARTS = 3
|
8
8
|
LENGTH = 4
|
9
9
|
|
10
|
-
def self.generate(options = {
|
10
|
+
def self.generate(options = {parts: PARTS, part_length: LENGTH})
|
11
11
|
num_parts = options.delete(:parts)
|
12
|
+
length_of_parts = options.delete(:part_length) || LENGTH
|
13
|
+
|
12
14
|
parts = []
|
13
15
|
(1..num_parts).each do |i|
|
14
|
-
part =
|
15
|
-
(1...
|
16
|
+
part = ""
|
17
|
+
(1...length_of_parts).each { part << random_symbol }
|
16
18
|
part << checkdigit_alg_1(part, i)
|
17
19
|
parts << part
|
18
20
|
end
|
19
|
-
parts.join(
|
21
|
+
parts.join("-")
|
20
22
|
end
|
21
23
|
|
22
|
-
def self.validate(orig, num_parts = PARTS)
|
24
|
+
def self.validate(orig, num_parts = PARTS, part_length = LENGTH)
|
23
25
|
code = orig.upcase
|
24
|
-
code.gsub!(/[^#{SYMBOL}]
|
25
|
-
parts = code.scan(/[#{SYMBOL}]{#{
|
26
|
+
code.gsub!(/[^#{SYMBOL}]+/o, "")
|
27
|
+
parts = code.scan(/[#{SYMBOL}]{#{part_length}}/)
|
26
28
|
return if parts.length != num_parts
|
27
29
|
parts.each_with_index do |part, i|
|
28
|
-
data
|
30
|
+
data = part[0...(part_length - 1)]
|
29
31
|
check = part[-1]
|
30
|
-
|
32
|
+
break if check != checkdigit_alg_1(data, i + 1)
|
31
33
|
end
|
32
|
-
parts.join(
|
34
|
+
parts.join("-")
|
33
35
|
end
|
34
36
|
|
35
37
|
def self.checkdigit_alg_1(orig, check)
|
36
|
-
orig.
|
38
|
+
orig.chars.each_with_index do |c, _|
|
37
39
|
k = SYMBOL.index(c)
|
38
40
|
check = check * 19 + k
|
39
41
|
end
|
data/spec/coupon_code_spec.rb
CHANGED
@@ -1,56 +1,63 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
RSpec.describe CouponCode do
|
4
|
-
|
5
|
-
describe '.generate' do
|
4
|
+
describe ".generate" do
|
6
5
|
subject { described_class.generate }
|
7
6
|
it { is_expected.not_to be_nil }
|
8
7
|
it { is_expected.to match(/^[0-9A-Z-]+$/) }
|
9
8
|
it { is_expected.to match(/^\w{4}-\w{4}-\w{4}$/) }
|
10
|
-
it
|
9
|
+
it "generates a different code" do
|
11
10
|
code2 = described_class.generate
|
12
11
|
is_expected.not_to eq(code2)
|
13
12
|
end
|
14
13
|
|
15
|
-
context
|
14
|
+
context "2 parts" do
|
16
15
|
subject { described_class.generate(parts: 2) }
|
17
16
|
it { is_expected.to match(/^\w{4}-\w{4}$/) }
|
18
17
|
end
|
18
|
+
|
19
|
+
context "when passed part_length" do
|
20
|
+
subject { described_class.generate(part_length: 4, parts: 1) }
|
21
|
+
it "allows part length to be set" do
|
22
|
+
expect(subject.length).to equal(4)
|
23
|
+
end
|
24
|
+
end
|
19
25
|
end
|
20
26
|
|
21
|
-
describe
|
27
|
+
describe ".validate" do
|
28
|
+
it "validates a good code" do
|
29
|
+
expect(described_class.validate("1K7Q-CTFM-LMTC")).to eq("1K7Q-CTFM-LMTC")
|
30
|
+
end
|
22
31
|
|
23
|
-
it
|
24
|
-
expect(described_class.validate(
|
32
|
+
it "validates with a custom length" do
|
33
|
+
expect(described_class.validate("GRG65-X0PF4-KP7TJ", 3, 5)).to eq("GRG65-X0PF4-KP7TJ")
|
25
34
|
end
|
26
35
|
|
27
|
-
it
|
28
|
-
expect(described_class.validate(
|
36
|
+
it "validates and returns the code in uppercase letters" do
|
37
|
+
expect(described_class.validate("1K7Q-ctfm-LMTC")).to eq("1K7Q-CTFM-LMTC")
|
29
38
|
end
|
30
39
|
|
31
|
-
it
|
32
|
-
expect(described_class.validate(
|
40
|
+
it "returns nil for an invalid code" do
|
41
|
+
expect(described_class.validate("1K7Q-CTFM")).to be_nil
|
33
42
|
end
|
34
43
|
|
35
|
-
it
|
36
|
-
expect(described_class.validate(
|
44
|
+
it "handles invalid characters" do
|
45
|
+
expect(described_class.validate("OK7Q-CTFM-LMTC")).to be_nil
|
37
46
|
end
|
38
47
|
|
39
|
-
context
|
48
|
+
context "valid cases: lowercase, different separator and parts" do
|
40
49
|
[
|
41
|
-
[
|
42
|
-
[
|
43
|
-
[
|
44
|
-
[
|
45
|
-
[
|
46
|
-
[
|
47
|
-
[
|
48
|
-
[
|
50
|
+
["1k7q-ctfm-lmtc"],
|
51
|
+
["1K7Q/CTFM/LMTC"],
|
52
|
+
["1K7Q CTFM LMTC"],
|
53
|
+
["1k7qctfmlmtc"],
|
54
|
+
["1K7Q-CTFM", 2],
|
55
|
+
["7YQH-1FU7-E1HX-0BG9", 4],
|
56
|
+
["YENH-UPJK-PTE0-20U6-QYME", 5],
|
57
|
+
["YENH-UPJK-PTE0-20U6-QYME-RBK1", 6]
|
49
58
|
].each do |args|
|
50
59
|
it { expect(described_class.validate(*args)).not_to be_nil }
|
51
60
|
end
|
52
61
|
end
|
53
|
-
|
54
62
|
end
|
55
|
-
|
56
63
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "coupon_code"
|
2
2
|
# This file was generated by the `rspec --init` command. Conventionally, all
|
3
3
|
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
4
4
|
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
@@ -41,57 +41,55 @@ RSpec.configure do |config|
|
|
41
41
|
mocks.verify_partial_doubles = true
|
42
42
|
end
|
43
43
|
|
44
|
-
# The settings below are suggested to provide a good initial experience
|
45
|
-
# with RSpec, but feel free to customize to your heart's content.
|
46
|
-
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
config.
|
52
|
-
|
53
|
-
|
54
|
-
#
|
55
|
-
#
|
56
|
-
#
|
57
|
-
|
58
|
-
|
59
|
-
#
|
60
|
-
#
|
61
|
-
# - http://
|
62
|
-
# - http://
|
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
|
-
Kernel.srand config.seed
|
96
|
-
=end
|
44
|
+
# The settings below are suggested to provide a good initial experience
|
45
|
+
# with RSpec, but feel free to customize to your heart's content.
|
46
|
+
# # These two settings work together to allow you to limit a spec run
|
47
|
+
# # to individual examples or groups you care about by tagging them with
|
48
|
+
# # `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
49
|
+
# # get run.
|
50
|
+
# config.filter_run :focus
|
51
|
+
# config.run_all_when_everything_filtered = true
|
52
|
+
#
|
53
|
+
# # Allows RSpec to persist some state between runs in order to support
|
54
|
+
# # the `--only-failures` and `--next-failure` CLI options. We recommend
|
55
|
+
# # you configure your source control system to ignore this file.
|
56
|
+
# config.example_status_persistence_file_path = "spec/examples.txt"
|
57
|
+
#
|
58
|
+
# # Limits the available syntax to the non-monkey patched syntax that is
|
59
|
+
# # recommended. For more details, see:
|
60
|
+
# # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
61
|
+
# # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
62
|
+
# # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
63
|
+
# config.disable_monkey_patching!
|
64
|
+
#
|
65
|
+
# # This setting enables warnings. It's recommended, but in some cases may
|
66
|
+
# # be too noisy due to issues in dependencies.
|
67
|
+
# config.warnings = true
|
68
|
+
#
|
69
|
+
# # Many RSpec users commonly either run the entire suite or an individual
|
70
|
+
# # file, and it's useful to allow more verbose output when running an
|
71
|
+
# # individual spec file.
|
72
|
+
# if config.files_to_run.one?
|
73
|
+
# # Use the documentation formatter for detailed output,
|
74
|
+
# # unless a formatter has already been configured
|
75
|
+
# # (e.g. via a command-line flag).
|
76
|
+
# config.default_formatter = 'doc'
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
# # Print the 10 slowest examples and example groups at the
|
80
|
+
# # end of the spec run, to help surface which specs are running
|
81
|
+
# # particularly slow.
|
82
|
+
# config.profile_examples = 10
|
83
|
+
#
|
84
|
+
# # Run specs in random order to surface order dependencies. If you find an
|
85
|
+
# # order dependency and want to debug it, you can fix the order by providing
|
86
|
+
# # the seed, which is printed after each run.
|
87
|
+
# # --seed 1234
|
88
|
+
# config.order = :random
|
89
|
+
#
|
90
|
+
# # Seed global randomization in this process using the `--seed` CLI option.
|
91
|
+
# # Setting this allows you to use `--seed` to deterministically reproduce
|
92
|
+
# # test failures related to randomization by passing the same `--seed` value
|
93
|
+
# # as the one that triggered the failure.
|
94
|
+
# Kernel.srand config.seed
|
97
95
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coupon_code
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sanghyun Park
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.4'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: standardrb
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description: A Ruby implementation of Perl's Algorithm::CouponCode CPAN module.
|
56
70
|
email:
|
57
71
|
- sh@baxang.com
|
@@ -60,10 +74,11 @@ executables:
|
|
60
74
|
extensions: []
|
61
75
|
extra_rdoc_files: []
|
62
76
|
files:
|
77
|
+
- ".github/dependabot.yml"
|
78
|
+
- ".github/workflows/ruby.yml"
|
63
79
|
- ".gitignore"
|
64
|
-
- ".
|
65
|
-
- ".
|
66
|
-
- ".travis.yml"
|
80
|
+
- ".ruby-version"
|
81
|
+
- ".standard.yml"
|
67
82
|
- CHANGELOG.md
|
68
83
|
- Gemfile
|
69
84
|
- LICENSE.txt
|
@@ -80,7 +95,7 @@ homepage: https://github.com/baxang/coupon-code
|
|
80
95
|
licenses:
|
81
96
|
- MIT
|
82
97
|
metadata: {}
|
83
|
-
post_install_message:
|
98
|
+
post_install_message:
|
84
99
|
rdoc_options: []
|
85
100
|
require_paths:
|
86
101
|
- lib
|
@@ -88,18 +103,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
103
|
requirements:
|
89
104
|
- - ">="
|
90
105
|
- !ruby/object:Gem::Version
|
91
|
-
version:
|
106
|
+
version: 2.6.0
|
92
107
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
108
|
requirements:
|
94
109
|
- - ">="
|
95
110
|
- !ruby/object:Gem::Version
|
96
111
|
version: '0'
|
97
112
|
requirements: []
|
98
|
-
|
99
|
-
|
100
|
-
signing_key:
|
113
|
+
rubygems_version: 3.5.11
|
114
|
+
signing_key:
|
101
115
|
specification_version: 4
|
102
116
|
summary: Generate and validate coupon codes.
|
103
|
-
test_files:
|
104
|
-
- spec/coupon_code_spec.rb
|
105
|
-
- spec/spec_helper.rb
|
117
|
+
test_files: []
|
data/.hound.yml
DELETED
data/.rubocop.yml
DELETED
data/.travis.yml
DELETED