-
Notifications
You must be signed in to change notification settings - Fork 15
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
Multi part validation #5
Changes from 5 commits
531089a
f68369a
f230e1c
5f67082
8adf2cf
9e18c76
e1adfa5
1db32a9
c7736e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,11 @@ | |
require 'digest/sha1' | ||
|
||
module CouponCode | ||
SYMBOL = '0123456789ABCDEFGHJKLMNPQRTUVWXY' | ||
PARTS = 3 | ||
LENGTH = 4 | ||
SYMBOL = '0123456789ABCDEFGHJKLMNPQRTUVWXY' | ||
LENGTH = 4 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary spacing detected. |
||
@@parts = 3 | ||
|
||
def self.generate(options = { parts: PARTS }) | ||
def self.generate(options = { parts: @@parts }) | ||
num_parts = options.delete(:parts) | ||
parts = [] | ||
(1..num_parts).each do |i| | ||
|
@@ -19,19 +19,27 @@ def self.generate(options = { parts: PARTS }) | |
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}}/) | ||
def self.validate(orig, options = { parts: @@parts }) | ||
num_parts = options.delete(:parts) | ||
code = orig.upcase.gsub(/[^0-9A-Z]+/, '') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary spacing detected. |
||
parts = code.scan(/[0-9A-Z]{#{LENGTH}}/) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary spacing detected. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary spacing detected. |
||
|
||
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.default_parts(parts) | ||
@@parts = parts | ||
end | ||
|
||
def self.checkdigit_alg_1(orig, check) | ||
orig.split('').each_with_index do |c, _| | ||
k = SYMBOL.index(c) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module CouponCode | ||
VERSION = '0.0.1' | ||
VERSION = "0.1.0" | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,11 +52,11 @@ def generate(*args) | |
end | ||
|
||
it 'should accept a short code with correct parts.' do | ||
CouponCode.validate('1K7Q-CTFM', 2).wont_be_nil | ||
CouponCode.validate('1K7Q-CTFM', parts: 2).wont_be_nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
end | ||
|
||
it 'should reject a short code with wrong parts.' do | ||
CouponCode.validate('CTFM-1K7Q', 2).must_be_nil | ||
CouponCode.validate('CTFM-1K7Q', parts: 2).must_be_nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
end | ||
|
||
it 'should fix and validate a lowercase code.' do | ||
|
@@ -76,23 +76,33 @@ def generate(*args) | |
end | ||
|
||
it 'should valid code-pretest.' do | ||
CouponCode.validate('1K7Q', 1).wont_be_nil | ||
CouponCode.validate('1K7C', 1).must_be_nil | ||
CouponCode.validate("1K7Q", parts: 1).wont_be_nil | ||
CouponCode.validate("1K7C", parts: 1).must_be_nil | ||
|
||
CouponCode.validate('1K7Q-CTFM', 2).wont_be_nil | ||
CouponCode.validate('1K7Q-CTFW', 2).must_be_nil | ||
CouponCode.validate("1K7Q-CTFM", parts: 2).wont_be_nil | ||
CouponCode.validate('1K7Q-CTFW', parts: 2).must_be_nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
|
||
CouponCode.validate('1K7Q-CTFM-LMTC', 3).wont_be_nil | ||
CouponCode.validate('1K7Q-CTFM-LMT1', 3).must_be_nil | ||
CouponCode.validate("1K7Q-CTFM-LMTC", parts: 3).wont_be_nil | ||
CouponCode.validate("1K7Q-CTFM-LMT1", parts: 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("7YQH-1FU7-E1HX-0BG9", parts: 4).wont_be_nil | ||
CouponCode.validate("7YQH-1FU7-E1HX-0BGP", parts: 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", parts: 5).wont_be_nil | ||
CouponCode.validate("YENH-UPJK-PTE0-20U6-QYMT", parts: 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 | ||
CouponCode.validate("YENH-UPJK-PTE0-20U6-QYME-RBK1", parts: 6).wont_be_nil | ||
CouponCode.validate("YENH-UPJK-PTE0-20U6-QYME-RBK2", parts: 6).must_be_nil | ||
end | ||
|
||
it "should allow a default part length to be set" do | ||
CouponCode.default_parts(5) | ||
|
||
code = CouponCode.generate | ||
code.split("-").length.must_equal 5 | ||
CouponCode.validate(code).must_equal code | ||
|
||
CouponCode.default_parts(3) # Set it back for the rest of the tests | ||
end | ||
end | ||
end |
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.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.