Skip to content
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

Closed
wants to merge 9 commits into from
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,19 @@ Or install it yourself as:

You can change the number of parts of the generated code by passing an option hash value like:

>> CouponCode.generate(parts: 4)
>> code = CouponCode.generate(parts: 4)
=> "1K7Q-CTFM-LMTC-DLGP"
>> CouponeCode.validate(code, parts: 4)
=> "1K7Q-CTFM-LMTC-DLGP"

You can also set the default number of parts you are working with so that you don't have to continually send the parts value as an option:

>> CouponCode.default_parts(5)
=> 5
>> code = CouponCode.generate
=> "CNYF-K4AH-L5PF-0RU7-5L0Q"
>> CouponCode.validate(code)
=> "CNYF-K4AH-L5PF-0RU7-5L0Q"

## Testing

Expand Down Expand Up @@ -74,4 +85,4 @@ MIT. See [LICENSE][license] for more details.
[node-couponcode]: https://github.com/chilts/node-coupon-code
[license]: https://github.com/baxang/coupon-code/blob/master/LICENSE.txt
[README-kr]: https://github.com/baxang/coupon-code/blob/master/README-ko.md
[rubygems]: https://rubygems.org/gems/coupon_code
[rubygems]: https://rubygems.org/gems/coupon_code
24 changes: 16 additions & 8 deletions lib/coupon_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
require 'digest/sha1'

module CouponCode
SYMBOL = '0123456789ABCDEFGHJKLMNPQRTUVWXY'
PARTS = 3
LENGTH = 4
SYMBOL = '0123456789ABCDEFGHJKLMNPQRTUVWXY'

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.

LENGTH = 4

Choose a reason for hiding this comment

The 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|
Expand All @@ -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]+/, '')

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.

parts = code.scan(/[0-9A-Z]{#{LENGTH}}/)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary spacing detected.

Choose a reason for hiding this comment

The 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)
Expand Down
2 changes: 1 addition & 1 deletion lib/coupon_code/version.rb
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
38 changes: 24 additions & 14 deletions test/coupon_code_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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
Expand All @@ -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

Choose a reason for hiding this comment

The 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