Class: Object

Inherits:
BasicObject
Defined in:
lib/object.rb

Overview

This provides a method for generating a random token of the given length. The generated token is base58 encoded, so the token just contains numbers, up- and down case characters.

Example:

Object.new_token(10) # => "S2Mq4mJBv6" (i.e.)

or

Object.new.new_token(10) # => "S2Mq4mJBv6" (i.e.)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new_token(length = 8, characters = ('a'..'z').to_a+('A'..'Z').to_a+(0..9).to_a) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/object.rb', line 11

def self.new_token(length=8, characters = ('a'..'z').to_a+('A'..'Z').to_a+(0..9).to_a)
  result = ''
  length.times do
    result += characters[SecureRandom.random_number(characters.length)].to_s
  end
  result
end

Instance Method Details

#new_token(length = 8, characters = ('a'..'z').to_a+('A'..'Z').to_a+(0..9).to_a) ⇒ Object



19
20
21
# File 'lib/object.rb', line 19

def new_token(length=8, characters = ('a'..'z').to_a+('A'..'Z').to_a+(0..9).to_a)
  self.class.new_token(length, characters)
end