Better product keys with Flexkey

If you've ever built an e-commerce or SAAS application, there's a good chance you've provided your customers or users with randomly-generated product keys, invoice numbers, redemption codes, order numbers, etc. Flexkey is a Ruby gem to assist in this process.

Consider a standard "Microsoft-style" product key: QPFLS-3OM4I-QQWXS-3QWX0-YQGSP. With Flexkey, generating keys in this format is a simple two-liner:

keygen = 'ccccc-ccccc-ccccc-ccccc-ccccc',
  char_pool: { 'c' => { alpha_upper: 0.75, numeric: 0.25 } })
# => "9PLRN-IHJ8R-6SYO1-3KWSG-ILA39"

Although it's certainly easy to write one-off code to do the same, I've found myself writing this same code repeatedly, so instead I've created this gem. Other use cases for the gem might be an e-commerce app where different products need identifying strings with different formats, or perhaps a multi-tenant platform where users or administrators need to generate keys in a format of their choice. Flexkey is even versatile enough to function as a password generator.

The gem includes the following features:

  • Straightforward key format specification
  • Ability to generate a single key or many unique keys
  • Built-in character types (alphabetical, numeric, symbolic, etc.)
  • Proportional character sampling (e.g. selecting from a pool of 75% letters and 25% numerals as seen above)
  • Custom character pools

Installation instructions, documentation, and additional examples are available at the Flexkey GitHub repo or go straight to RubyGems to install.