12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/dubs/token.rb', line 12
def generate(type: :numeric, length: 4, token_case: :lower, seed: nil)
return "" if type.to_sym == :none || length <= 0
raw = case type.to_sym
when :hex
if seed
seeded_chars(HEX_CHARS, length, seed)
else
SecureRandom.hex(length)[0, length]
end
when :numeric
if seed
seeded_numeric(length, seed)
else
SecureRandom.random_number(10**length).to_s.rjust(length, "0")
end
when :alpha
if seed
seeded_chars(ALPHA_CHARS, length, seed)
else
Array.new(length) { ALPHA_CHARS.sample }.join
end
else
raise Error, "Unknown token type: #{type}. Use :hex, :numeric, :alpha, or :none"
end
token_case.to_sym == :upper ? raw.upcase : raw.downcase
end
|