Class: RToken

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

Overview

Generates Random Tokens for general purposes.

Examples:

Generate 8 length token

RToken.rtoken #=> Random sequence 8 chars

Generate 10 length token

RToken.rtoken(:size => 10) #=> Random sequence 10 chars
RTolen.rtoken(10) #=> Less verbose fashion

Generate 16 length token with special chars

RToken.rtoken(:size => 16, :special_chars => '!@#$%%&')

Generate 32 length token with uppercase chars

RToken.rtoken(:size => 32, :uppercase => true)

See Also:

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

Instance Method Summary collapse

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

Examples:

Generates tokens with special chars

rtkn = RToken.new(:special_chars => '+-*/')
# All next calls keeps the same options
rtkn.rtoken #=> Same as Rtoken.rtoken(:special_chars => '+-*/')

Parameters:

  • opts (Hash) (defaults to: nil)

    same as rtoken



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

See Also:



58
59
60
# File 'lib/rtoken.rb', line 58

def self.[](opts=nil)
  RToken.rtoken opts
end

.rtoken(opts = nil) ⇒ String

Generates a token

Parameters:

  • opts (Integer) (defaults to: nil)

    Sames as :size => n

  • opts (Hash) (defaults to: nil)

    The options to configure the token generator

  • opts (Fixnum) (defaults to: nil)

    :size The size of token string. Default 8

  • opts (defaults to: nil)

    :uppercase if true all chars will be replaced by uppercase. Default false

  • opts (defaults to: nil)

    :lowercase if true all chars will be replaced by lowercase. Has priority against :uppercase definition. Default false

  • opts (defaults to: nil)

    :numeric if true include numeric chars (‘0’..‘9’) on tokens. Default true

  • opts (String) (defaults to: nil)

    :special_chars special chars to be include on token generation, by default include alphanumeric chars

Returns:

  • (String)

    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)
  options = DEFAULT_OPTIONS.merge(opts || {})
  size = options[:size] || 8
  # Merge available chars
  chars_array = options[:numeric] ? Array.new(NUMERIC_CHARS) : [] 
  if options[:uppercase] || options[:lowercase]
    chars_array += (options[:lowercase] ? LOWER_ALPHA_CHARS : UPPER_ALPHA_CHARS)
  else
    chars_array += LOWER_ALPHA_CHARS
    chars_array += UPPER_ALPHA_CHARS
  end
  chars_array += options[:special_chars].split('') if options[: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

Instance Method Details

#rtoken(opts = nil) ⇒ String

Generates a token with previews options

Parameters:

  • opts (Hash) (defaults to: nil)

    options that will be merged to the predefined on initialize

Returns:

  • (String)

    token

See Also:



51
52
53
54
# File 'lib/rtoken.rb', line 51

def rtoken(opts=nil)
  opts = RToken.check_param(opts)
  RToken.rtoken @options.merge(opts || {})
end