Class: RToken
- Inherits:
-
Object
- Object
- RToken
- Defined in:
- lib/rtoken.rb
Overview
Generates Random Tokens for general purposes.
Constant Summary collapse
- UPPER_ALPHA_CHARS =
('A'..'Z').to_a.freeze
- LOWER_ALPHA_CHARS =
('a'..'z').to_a.freeze
- NUMERIC_CHARS =
('0'..'9').to_a.freeze
- DEFAULT_OPTIONS =
{ :size => 8, :uppercase => false, :lowercase => false, :numeric => true, :special_chars => '' }.freeze
Class Method Summary collapse
-
.[](opts = nil) ⇒ Object
Less verbose way to call RToken.rtoken.
-
.rtoken(opts = nil) ⇒ String
Generates a token.
Instance Method Summary collapse
-
#initialize(opts = nil) ⇒ RToken
constructor
Creates an instance of RToken and handle the options for later calls It is useful when you have many settings and don’t want to repeat them.
-
#rtoken(opts = nil) ⇒ String
Generates a token with previews options.
Constructor Details
#initialize(opts = nil) ⇒ RToken
Creates an instance of RToken and handle the options for later calls It is useful when you have many settings and don’t want to repeat them
41 42 43 |
# File 'lib/rtoken.rb', line 41 def initialize(opts=nil) @options = opts || {} end |
Class Method Details
.[](opts = nil) ⇒ Object
Less verbose way to call RToken.rtoken
58 59 60 |
# File 'lib/rtoken.rb', line 58 def self.[](opts=nil) RToken.rtoken opts end |
.rtoken(opts = nil) ⇒ String
Generates a token
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rtoken.rb', line 72 def self.rtoken(opts=nil) opts = check_param(opts) = DEFAULT_OPTIONS.merge(opts || {}) size = [:size] || 8 # Merge available chars chars_array = [:numeric] ? Array.new(NUMERIC_CHARS) : [] if [:uppercase] || [:lowercase] chars_array += ([:lowercase] ? LOWER_ALPHA_CHARS : UPPER_ALPHA_CHARS) else chars_array += LOWER_ALPHA_CHARS chars_array += UPPER_ALPHA_CHARS end chars_array += [:special_chars].split('') if [:special_chars] chars_array_size = chars_array.size # creates token token_chars = Array.new(size) size.times do |i| token_chars[i] = chars_array[rand(chars_array_size)] end token_chars.join end |