Class: NoPassword::RandomCodeGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/nopassword/random_code_generator.rb

Constant Summary collapse

CODE_LENGTH =

6 digit random code by default.

6
NUMERIC_CHARACTERS =

Numeric to make input a tad easier with a number pad.

[*'0'..'9']
ALPHANUMERIC_CHARACTERS =

Alphanumeric, which excludes lowercase because people would typo that.

[*'A'..'Z', *'0'..'9']

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(length:, characters:) ⇒ RandomCodeGenerator

Returns a new instance of RandomCodeGenerator.



11
12
13
14
# File 'lib/nopassword/random_code_generator.rb', line 11

def initialize(length:, characters:)
  @length = length
  @characters = characters
end

Class Method Details

.alphanumericObject



35
36
37
# File 'lib/nopassword/random_code_generator.rb', line 35

def alphanumeric
  new length: CODE_LENGTH, characters: ALPHANUMERIC_CHARACTERS
end

.generate_alphanumeric_codeObject



39
40
41
# File 'lib/nopassword/random_code_generator.rb', line 39

def generate_alphanumeric_code
  alphanumeric.generate
end

.generate_numeric_codeObject



31
32
33
# File 'lib/nopassword/random_code_generator.rb', line 31

def generate_numeric_code
  numeric.generate
end

.numericObject



27
28
29
# File 'lib/nopassword/random_code_generator.rb', line 27

def numeric
  new length: CODE_LENGTH, characters: NUMERIC_CHARACTERS
end

Instance Method Details

#generateObject



16
17
18
19
20
21
22
23
# File 'lib/nopassword/random_code_generator.rb', line 16

def generate
  # Why not `SecureRandom#rand`? I don't actually want a number; I want a code, with
  # leading zeros, that's a string. This is the easiest way to generate that and pad it.
  #
  # This really should be a public API, but alas, its not, so I have to call
  # it privately and pass it the characters I want this to generate for the code.
  SecureRandom.send :choose, @characters, @length
end