Sha256: 220ec689d0b3ffb8ba2dc89b1088cf701521151a2b1381dcc5ac71eff9460289

Contents?: true

Size: 694 Bytes

Versions: 2

Compression:

Stored size: 694 Bytes

Contents

# 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 Object
  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
  
  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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ar-tokens-0.0.5 lib/object.rb
ar-tokens-0.0.4 lib/object.rb