CouponCode gem generates and validates coupon codes that are suitable for e-commerce services and businesses.
It is a Ruby implementation of Grant's Algorithm::CouponCode. Please read the original documentation of Algorithm::CouponCode for more details.
Please be aware that some of the original CPAN module's features are not implemented yet. For example, generating codes by plaintext, auto correction, and jQuery plugin are missing in this gem.
This gem is developed for https://stripes.co.kr
Add this line to your application's Gemfile:
gem 'coupon_code'
And then execute:
$ bundle
Or install it yourself as:
$ gem install coupon_code
>> require 'coupon_code'
>> code = CouponCode.generate
=> "1K7Q-CTFM-LMTC"
>> CouponCode.validate(code)
=> "1K7Q-CTFM-LMTC"
>> CouponCode.validate('1K7Q-CTFM-LMTO') # Invalid code
=> nil
You can change the number of parts of the generated code by passing an option hash value like:
>> CouponCode.generate(parts: 4)
=> "1K7Q-CTFM-LMTC-DLGP"
>> 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"
$ bundle exec rake spec
- Grant McLean's Algorithm::CouponCode in Perl
- Andrew Chilton's NodeJS implementation in JavaScript
MIT. See LICENSE for more details.
- Fork it ( https://github.com/baxang/coupon-code/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request