0.0
No commit activity in last 3 years
No release in over 3 years
A simple gem to generate unique, random, and hard to guess coupon/voucher codes. This project was inspired from https://github.com/rspective/voucher-code-generator-js
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
~> 3.0
 Project Readme

Coupon/Voucher Code Generator

Generate unique, random, and hard to guess coupon/voucher codes. Use cases: promo codes, loyalty coupons, gift vouchers, in-app purchases, referral links

This project was inspired from https://github.com/rspective/voucher-code-generator-js

Installation

Add this line to your application's Gemfile:

gem 'voucher_code'

And then execute:

$ bundle install

API Reference

Generate voucher code

Add this snippet to your base code

VoucherCode.generate

If you would like to use custom configuration, there are several attributes you have to understand first.

Count

  • It used for returning {:count} generated voucher codes
  • By default, it sets to 1

Length

  • It used for returning generated voucher codes based on length configuration
  • By default, it sets to 8

Charset

  • It used for generating voucher code. Use this three options:
config.charset = '0123456789'
config.charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
config.charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  • You can also use your own custom charset. By default, if sets to '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

Prefix

  • It used for append prefix before generated voucher code. For example:
config.prefix = '2020-'
  • It will generates 2020-${voucher_code}. By default, it sets to empty string / ''.

Postfix

  • It used for append postfix after generated voucher code. For example:
config.postfix = '-2020'
  • It will generates ${voucher_code}-2020. By default, it sets to empty string / ''.

Pattern

  • It used for generate voucher code based on your customize pattern. For example:
config.pattern = '####-####'
  • It will generates 'ABHs-12Lo'. By default, it sets to '########' where it depends on config.length
configuration = {
  count: 1,
  length: 8,
  charset: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  prefix: 'abcd-',
  postfix: '-2020',
  pattern: '########'
}

VoucherCode.generate(configuration)