-
Notifications
You must be signed in to change notification settings - Fork 15
Add configuration #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
baxang
wants to merge
4
commits into
master
Choose a base branch
from
add-configuration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,13 @@ | ||
| require 'coupon_code/version' | ||
| require 'securerandom' | ||
| require 'digest/sha1' | ||
| require 'coupon_code/version' | ||
| require 'coupon_code/generator' | ||
| require 'coupon_code/configuration' | ||
| require 'coupon_code/validator' | ||
|
|
||
| module CouponCode | ||
| SYMBOL = '0123456789ABCDEFGHJKLMNPQRTUVWXY' | ||
| PARTS = 3 | ||
| LENGTH = 4 | ||
|
|
||
| def self.generate(options = { parts: PARTS }) | ||
| num_parts = options.delete(:parts) | ||
| parts = [] | ||
| (1..num_parts).each do |i| | ||
| part = '' | ||
| (1...LENGTH).each { part << random_symbol } | ||
| part << checkdigit_alg_1(part, i) | ||
| parts << part | ||
| end | ||
| parts.join('-') | ||
| end | ||
|
|
||
| def self.validate(orig, num_parts = PARTS) | ||
| code = orig.upcase | ||
| code.gsub!(/[^0-9A-Z]+/, '') | ||
| parts = code.scan(/[0-9A-Z]{#{LENGTH}}/) | ||
| return if parts.length != num_parts | ||
| parts.each_with_index do |part, i| | ||
| data = part[0...(LENGTH - 1)] | ||
| check = part[-1] | ||
| return if check != checkdigit_alg_1(data, i + 1) | ||
| end | ||
| parts.join('-') | ||
| end | ||
|
|
||
| def self.checkdigit_alg_1(orig, check) | ||
| orig.split('').each_with_index do |c, _| | ||
| k = SYMBOL.index(c) | ||
| check = check * 19 + k | ||
| end | ||
| SYMBOL[check % 31] | ||
| end | ||
|
|
||
| def self.random_symbol | ||
| SYMBOL[rand(SYMBOL.length)] | ||
| end | ||
| SEPARATOR = '-' | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| module CouponCode | ||
| class << self | ||
| attr_accessor :configuration | ||
| end | ||
|
|
||
| def self.config | ||
| @config ||= Configuration.new | ||
| end | ||
|
|
||
| def self.configure | ||
| yield(config) | ||
| end | ||
|
|
||
| def self.reset | ||
| @config = Configuration.new | ||
| end | ||
|
|
||
| class Configuration | ||
|
|
||
| attr_accessor :separator, :part_length, :num_parts | ||
|
|
||
| def initialize | ||
| @separator = '-' | ||
| @part_length = 4 | ||
| @num_parts = 3 | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| module CouponCode | ||
| def self.generate(options = { parts: PARTS }) | ||
| num_parts = options.delete(:parts) | ||
| parts = [] | ||
| (1..num_parts).each do |i| | ||
| part = '' | ||
| (1...LENGTH).each { part << random_symbol } | ||
| part << checkdigit_alg_1(part, i) | ||
| parts << part | ||
| end | ||
| parts.join('-') | ||
| end | ||
|
|
||
| def self.random_symbol | ||
| SYMBOL[rand(SYMBOL.length)] | ||
| end | ||
| private_class_method :random_symbol | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| module CouponCode | ||
| def self.validate(orig, num_parts = PARTS) | ||
| code = orig.upcase | ||
| code.gsub!(/[^0-9A-Z]+/, '') | ||
| parts = code.scan(/[0-9A-Z]{#{LENGTH}}/) | ||
| return if parts.length != num_parts | ||
| parts.each_with_index do |part, i| | ||
| data = part[0...(LENGTH - 1)] | ||
| check = part[-1] | ||
| return if check != checkdigit_alg_1(data, i + 1) | ||
| end | ||
| parts.join('-') | ||
| end | ||
|
|
||
| def self.checkdigit_alg_1(orig, check) | ||
| orig.split('').each_with_index do |c, _| | ||
| k = SYMBOL.index(c) | ||
| check = check * 19 + k | ||
| end | ||
| SYMBOL[check % 31] | ||
| end | ||
| private_class_method :checkdigit_alg_1 | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| require 'test_helper' | ||
|
|
||
| describe 'Configuration' do | ||
| it 'separator' do | ||
| config = CouponCode::Configuration.new | ||
| config.separator = '*' | ||
| config.separator.must_equal('*') | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| require 'test_helper' | ||
|
|
||
| describe CouponCode do | ||
| def generate(*args) | ||
| CouponCode.generate(*args) | ||
| end | ||
|
|
||
| it 'should be loaded.' do | ||
| CouponCode.must_respond_to(:generate) | ||
| end | ||
|
|
||
| it 'should generate a code' do | ||
| generate.wont_be_empty | ||
| end | ||
|
|
||
| it 'should only contain uppercase letters, digits, and dashes.' do | ||
| generate.must_match(/^[0-9A-Z-]+$/) | ||
| end | ||
|
|
||
| it 'should look like XXXX-XXXX-XXXX.' do | ||
| generate.must_match(/^\w{4}-\w{4}-\w{4}$/) | ||
| end | ||
|
|
||
| it 'should generate different codes.' do | ||
| code1 = generate | ||
| code2 = generate | ||
| code1.wont_equal(code2) | ||
| end | ||
|
|
||
| it 'should generate an arbitrary number of parts.' do | ||
| generate(parts: 2).must_match(/^\w{4}-\w{4}$/) | ||
| generate(parts: 5).must_match(/^\w{4}-\w{4}-\w{4}-\w{4}-\w{4}$/) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| require 'test_helper' | ||
|
|
||
| describe Validator do | ||
| it 'should be loaded.' do | ||
| CouponCode.must_respond_to(:validate) | ||
| end | ||
|
|
||
| it 'should fail to validate invalid code.' do | ||
| CouponCode.validate('').must_equal(nil) | ||
| end | ||
|
|
||
| it 'should accept a valid code.' do | ||
| CouponCode.validate('1K7Q-CTFM-LMTC').wont_be_nil | ||
| end | ||
|
|
||
| it 'should reject a short code.' do | ||
| CouponCode.validate('1K7Q-CTFM').must_be_nil | ||
| end | ||
|
|
||
| it 'should accept a short code with correct parts.' do | ||
| CouponCode.validate('1K7Q-CTFM', 2).wont_be_nil | ||
| end | ||
|
|
||
| it 'should reject a short code with wrong parts.' do | ||
| CouponCode.validate('CTFM-1K7Q', 2).must_be_nil | ||
| end | ||
|
|
||
| it 'should fix and validate a lowercase code.' do | ||
| code = '1k7q-ctfm-lmtc' | ||
| CouponCode.validate(code.downcase).must_equal(code.upcase) | ||
| end | ||
|
|
||
| it 'should validate alternative separators.' do | ||
| code = '1k7q/ctfm/lmtc' | ||
| CouponCode.validate(code).must_equal('1K7Q-CTFM-LMTC') | ||
|
|
||
| code = '1k7q ctfm lmtc' | ||
| CouponCode.validate(code).must_equal('1K7Q-CTFM-LMTC') | ||
|
|
||
| code = '1k7qctfmlmtc' | ||
| CouponCode.validate(code).must_equal('1K7Q-CTFM-LMTC') | ||
| end | ||
|
|
||
| it 'should valid code-pretest.' do | ||
| CouponCode.validate('1K7Q', 1).wont_be_nil | ||
| CouponCode.validate('1K7C', 1).must_be_nil | ||
|
|
||
| CouponCode.validate('1K7Q-CTFM', 2).wont_be_nil | ||
| CouponCode.validate('1K7Q-CTFW', 2).must_be_nil | ||
|
|
||
| CouponCode.validate('1K7Q-CTFM-LMTC', 3).wont_be_nil | ||
| CouponCode.validate('1K7Q-CTFM-LMT1', 3).must_be_nil | ||
|
|
||
| CouponCode.validate('7YQH-1FU7-E1HX-0BG9', 4).wont_be_nil | ||
| CouponCode.validate('7YQH-1FU7-E1HX-0BGP', 4).must_be_nil | ||
|
|
||
| CouponCode.validate('YENH-UPJK-PTE0-20U6-QYME', 5).wont_be_nil | ||
| CouponCode.validate('YENH-UPJK-PTE0-20U6-QYMT', 5).must_be_nil | ||
|
|
||
| CouponCode.validate('YENH-UPJK-PTE0-20U6-QYME-RBK1', 6).wont_be_nil | ||
| CouponCode.validate('YENH-UPJK-PTE0-20U6-QYME-RBK2', 6).must_be_nil | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary spacing detected.