voucher_code 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5c62dae152ad5180eafaa98d79fe3bf685c4cfec7a0cbe076ee0c49dc9672420
4
+ data.tar.gz: e0c33794ed26111b5e3c031dd16cd2b8d713e9b3d4330f5f81d2169385751c98
5
+ SHA512:
6
+ metadata.gz: cedc166133946adaa6e0295dc2aa01bca797410477c0ae2cbd6cc32a0fa68ac5f6e97c9b8ded3e82df96052be6edf960e4803ddb4980495012a05b7b280878ee
7
+ data.tar.gz: bb9f38bd9688ef9ce4ed4a88da0141177baf14f5ffd0532e53143755cec811cb5c067f246f22255233f699a229e7b37fd3075e43e8671bcf1d036ea640214610
data/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # Coupon/Voucher Code Generator
2
+
3
+ Generate unique, random, and hard to guess coupon/voucher codes. Use cases: promo codes, loyalty coupons, gift vouchers, in-app purchases, referral links
4
+
5
+ This project was inspired from https://github.com/rspective/voucher-code-generator-js
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'voucher_code'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```sh
18
+ $ bundle install
19
+ ```
20
+
21
+ ## API Reference
22
+
23
+ ### Generate voucher code
24
+
25
+ Add this snippet to your base code
26
+
27
+ ```ruby
28
+ VoucherCode.generate
29
+ ```
30
+
31
+ If you would like to use custom configuration, there are several attributes you have to understand first.
32
+
33
+ **Count**
34
+ It used for returning {:count} generated voucher codes
35
+ By default, it sets to 1
36
+
37
+ **Length**
38
+
39
+ It used for returning generated voucher codes based on length configuration
40
+ By default, it sets to 8
41
+
42
+ **Charset**
43
+
44
+ It used for generating voucher code
45
+ Use this three options:
46
+ config.charset = '0123456789'
47
+ config.charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
48
+ config.charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
49
+ You can also use your own custom charset
50
+ By default, if sets to '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
51
+
52
+ **Prefix**
53
+
54
+ It used for append prefix before generated voucher code
55
+ For example:
56
+ config.prefix = '2020-'
57
+ It generates 2020-${voucher_code}
58
+ By default, it sets to empty string/''
59
+
60
+ **Postfix**
61
+
62
+ It used for append postfix after generated voucher code
63
+ For example:
64
+ config.postfix = '-2020'
65
+ It generates ${voucher_code}-2020
66
+ By default, it sets to empty string/''
67
+
68
+ **Pattern**
69
+
70
+ It used for generate voucher code based on your customize pattern
71
+ For example:
72
+ config.pattern = '####-####'
73
+ It generates 'ABHs-12Lo'
74
+ By default, it sets to '########' where it depends on config.length
75
+
76
+ ```ruby
77
+ configuration = {
78
+ count: 1,
79
+ length: 8,
80
+ charset: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
81
+ prefix: 'abcd-',
82
+ postfix: '-2020',
83
+ pattern: '########'
84
+ }
85
+
86
+ VoucherCode.generate(configuration)
87
+ ```
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'voucher_code'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require 'pry'
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,59 @@
1
+ # VoucherCode Global Configuration
2
+
3
+ VoucherCode.defaults do |config|
4
+ # ## Count
5
+ #
6
+ # It used for returning {:count} generated voucher codes
7
+ # By default, it sets to 1
8
+ #
9
+ # config.count = 1
10
+
11
+ # ## Length
12
+ #
13
+ # It used for returning generated voucher codes based on length configuration
14
+ # By default, it sets to 8
15
+ #
16
+ # config.length = 8
17
+
18
+ # ## Charset
19
+ #
20
+ # It used for generating voucher code
21
+ # Use this three options:
22
+ # config.charset = '0123456789'
23
+ # config.charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
24
+ # config.charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
25
+ # You can also use your own custom charset
26
+ # By default, if sets to '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
27
+ #
28
+ # config.charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
29
+
30
+ # ## Prefix
31
+ #
32
+ # It used for append prefix before generated voucher code
33
+ # For example:
34
+ # config.prefix = '2020-'
35
+ # It generates 2020-${voucher_code}
36
+ # By default, it sets to empty string/''
37
+ #
38
+ # config.prefix = ''
39
+
40
+ # ## Postfix
41
+ #
42
+ # It used for append postfix after generated voucher code
43
+ # For example:
44
+ # config.postfix = '-2020'
45
+ # It generates ${voucher_code}-2020
46
+ # By default, it sets to empty string/''
47
+ #
48
+ # config.postfix = ''
49
+
50
+ # ## Pattern
51
+ #
52
+ # It used for generate voucher code based on your customize pattern
53
+ # For example:
54
+ # config.pattern = '####-####'
55
+ # It generates 'ABHs-12Lo'
56
+ # By default, it sets to '########' where it depends on config.length
57
+ #
58
+ # config.pattern = ''
59
+ end
@@ -0,0 +1,11 @@
1
+ require 'rails/generators'
2
+
3
+ class VoucherCodeGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+ source_root File.expand_path('templates', __dir__)
6
+ desc 'Preparing required file for voucher code integration'
7
+
8
+ def copy_initializer
9
+ template 'initializer.rb', 'config/initializers/voucher_code.rb'
10
+ end
11
+ end
@@ -0,0 +1,82 @@
1
+ require_relative 'resources/charset'
2
+ require_relative 'helpers/randomize'
3
+
4
+ module VoucherCode
5
+ # Voucher code configuration
6
+ class Config
7
+ attr_reader :config,
8
+ :count,
9
+ :length,
10
+ :charset,
11
+ :prefix,
12
+ :postfix,
13
+ :pattern
14
+
15
+ def initialize(config = {})
16
+ @config = config || {}
17
+ @count = config[:count] || 1
18
+ @length = config[:length] || 8
19
+ @charset = config[:charset] || select_charset('alphanumeric')
20
+ @prefix = config[:prefix] || ''
21
+ @postfix = config[:postfix] || ''
22
+
23
+ @pattern = if config[:pattern].nil? || config[:pattern].empty?
24
+ looping('#', @length)
25
+ else
26
+ config[:pattern]
27
+ end
28
+ end
29
+
30
+ def generate
31
+ raise 'Not possible to generate some requested codes.' unless feasible?
32
+
33
+ codes = {}
34
+ while @count.positive?
35
+ code = generate_single_data
36
+
37
+ unless codes[code]
38
+ codes[code] = true
39
+ @count -= 1
40
+ end
41
+ end
42
+
43
+ codes.keys
44
+ end
45
+
46
+ private
47
+
48
+ def looping(str, length)
49
+ result = ''
50
+
51
+ i = 0
52
+ until i > length - 1
53
+ result += str
54
+ i += 1
55
+ end
56
+
57
+ result
58
+ end
59
+
60
+ def select_charset(name)
61
+ selected_charset = Resources::CHARSET[name.to_sym]
62
+
63
+ raise 'Invalid charset' unless selected_charset
64
+
65
+ selected_charset
66
+ end
67
+
68
+ def generate_single_data
69
+ code = @pattern.split('').map { |char|
70
+ char == '#' ?
71
+ Helpers::Randomize.random_element(@charset) :
72
+ char
73
+ }.join('')
74
+
75
+ "#{@prefix}#{code}#{@postfix}"
76
+ end
77
+
78
+ def feasible?
79
+ (@length ** (@pattern.match(/#/) || []).length) >= @count
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,15 @@
1
+ module VoucherCode
2
+ module Helpers
3
+ class Randomize
4
+ class << self
5
+ def random_int(min, max)
6
+ (rand * (max - min + 1)).floor + min
7
+ end
8
+
9
+ def random_element(array)
10
+ array[random_int(0, array.length - 1)]
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module VoucherCode
2
+ module Resources
3
+ CHARSET = {
4
+ numbers: '0123456789'.freeze,
5
+ alphabetic: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.freeze,
6
+ alphanumeric: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.freeze
7
+ }
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module VoucherCode
2
+ VERSION = '1.1.1'.freeze
3
+ end
@@ -0,0 +1,25 @@
1
+ require_relative 'voucher_code/version'
2
+ require_relative 'voucher_code/config'
3
+
4
+ # Generate voucher code
5
+ module VoucherCode
6
+ class << self
7
+ def generate(config = {})
8
+ config = @defaults if @defaults
9
+
10
+ configuration = Config.new(config)
11
+ configuration.generate
12
+ end
13
+
14
+ # Set global defaults for generating voucher code.
15
+ # @example
16
+ # VoucherCode.defaults do |config|
17
+ # config.count = 1
18
+ # ...
19
+ # end
20
+ def defaults(&block)
21
+ @defaults = block if block_given?
22
+ @defaults
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'voucher_code/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'voucher_code'
8
+ spec.version = VoucherCode::VERSION
9
+ spec.authors = ['Eka Pramudita D.']
10
+ spec.email = ['edharmowongso@gmail.com']
11
+
12
+ spec.summary = %q{A simple gem to generate unique, random, and hard to guess coupon/voucher codes}
13
+ spec.description = %q{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}
14
+ spec.homepage = 'https://github.com/edharmowongso/voucher-code'
15
+ spec.license = 'MIT'
16
+
17
+ spec.require_paths = %w(lib)
18
+ spec.required_ruby_version = '>= 2.0.0'
19
+ spec.required_rubygems_version = '>= 1.3.5'
20
+ spec.files = %w(voucher_code.gemspec) + Dir['*.md', 'bin/*', 'lib/**/*.rb', 'lib/**/**/*.rb']
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+
23
+ spec.add_development_dependency 'bundler', '~> 2.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.0'
25
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: voucher_code
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Eka Pramudita D.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-08-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ description: A simple gem to generate unique, random, and hard to guess coupon/voucher
42
+ codes. This project was inspired from https://github.com/rspective/voucher-code-generator-js
43
+ email:
44
+ - edharmowongso@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - README.md
50
+ - bin/console
51
+ - bin/setup
52
+ - lib/generators/templates/initializer.rb
53
+ - lib/generators/voucher_code_generator.rb
54
+ - lib/voucher_code.rb
55
+ - lib/voucher_code/config.rb
56
+ - lib/voucher_code/helpers/randomize.rb
57
+ - lib/voucher_code/resources/charset.rb
58
+ - lib/voucher_code/version.rb
59
+ - voucher_code.gemspec
60
+ homepage: https://github.com/edharmowongso/voucher-code
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 2.0.0
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 1.3.5
78
+ requirements: []
79
+ rubygems_version: 3.0.3
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: A simple gem to generate unique, random, and hard to guess coupon/voucher
83
+ codes
84
+ test_files: []