]> projects.mako.cc - selectricity/blob - lib/uniq_token.rb
Mostly worked on the candidate adding feature to include pictures and
[selectricity] / lib / uniq_token.rb
1 require 'digest/md5'
2
3 class UniqueTokenGenerator
4   def initialize(length=10)
5     @length = length 
6   end
7
8   # this should probably be rewritten as a class method
9   def token
10     token = ""
11     while token.length < @length
12       seed = ""
13       16.times do
14         seed << ( i = Kernel.rand(62)
15                   i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 )) ).chr
16       end
17       token << Digest::MD5.hexdigest( seed )
18     end
19     token.slice( 0..@length )
20   end
21 end
22
23 # debug code
24 # puts UniqueTokenGenerator.new(300).token
25

Benjamin Mako Hill || Want to submit a patch?