Skip to content

Commit

Permalink
feat: add ability to change length of each segment (#15)
Browse files Browse the repository at this point in the history
Recreated from and closes #12

---------

Co-authored-by: Sterling Cobb <[email protected]>
  • Loading branch information
baxang and fourcolors authored Oct 22, 2024
1 parent c3f3a14 commit 0a109e9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ name: Ruby
on:
push:
branches:
- main
- chore/upgrade-ruby-ci
- '*'
pull_request:
branches:
- main
- chore/upgrade-ruby-ci
- '*'

jobs:
test:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@

### 0.0.2
- Bug fix `.validate` can't handle invalid characters and produces an error. (mrclmrvn)

### 0.0.2
- add ability to specify length #15 (fourcolors)
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ You can change the number of parts of the generated code by passing an option ha
>> CouponCode.validate("1K7Q-CTFM-LMTC-DLGP", 4)
=> "1K7Q-CTFM-LMTC-DLGP"

You can also change the length of each part like:

>> CouponCode.generate(parts: 3, part_length: 5)
=> "GRG65-X0PF4-KP7TJ"
>> CouponCode.validate("1K7Q-CTFM-LMTC-DLGP", 3, 5)
=> "1K7Q-CTFM-LMTC-DLGP"

## Testing

```ruby
Expand Down
12 changes: 7 additions & 5 deletions lib/coupon_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ module CouponCode
PARTS = 3
LENGTH = 4

def self.generate(options = {parts: PARTS})
def self.generate(options = {parts: PARTS, part_length: LENGTH})
num_parts = options.delete(:parts)
length_of_parts = options.delete(:part_length) || LENGTH

parts = []
(1..num_parts).each do |i|
part = ""
(1...LENGTH).each { part << random_symbol }
(1...length_of_parts).each { part << random_symbol }
part << checkdigit_alg_1(part, i)
parts << part
end
parts.join("-")
end

def self.validate(orig, num_parts = PARTS)
def self.validate(orig, num_parts = PARTS, part_length = LENGTH)
code = orig.upcase
code.gsub!(/[^#{SYMBOL}]+/o, "")
parts = code.scan(/[#{SYMBOL}]{#{LENGTH}}/o)
parts = code.scan(/[#{SYMBOL}]{#{part_length}}/)
return if parts.length != num_parts
parts.each_with_index do |part, i|
data = part[0...(LENGTH - 1)]
data = part[0...(part_length - 1)]
check = part[-1]
break if check != checkdigit_alg_1(data, i + 1)
end
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.2".freeze
VERSION = "0.0.3".freeze
end
11 changes: 11 additions & 0 deletions spec/coupon_code_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,24 @@
subject { described_class.generate(parts: 2) }
it { is_expected.to match(/^\w{4}-\w{4}$/) }
end

context "when passed part_length" do
subject { described_class.generate(part_length: 4, parts: 1) }
it "allows part length to be set" do
expect(subject.length).to equal(4)
end
end
end

describe ".validate" do
it "validates a good code" do
expect(described_class.validate("1K7Q-CTFM-LMTC")).to eq("1K7Q-CTFM-LMTC")
end

it "validates with a custom length" do
expect(described_class.validate("GRG65-X0PF4-KP7TJ", 3, 5)).to eq("GRG65-X0PF4-KP7TJ")
end

it "validates and returns the code in uppercase letters" do
expect(described_class.validate("1K7Q-ctfm-LMTC")).to eq("1K7Q-CTFM-LMTC")
end
Expand Down

0 comments on commit 0a109e9

Please sign in to comment.