Module: Shrinker::Token

Extended by:
Token
Included in:
Token
Defined in:
lib/shrinker/token.rb

Instance Method Summary collapse

Instance Method Details

#fetch_unique_token(backend, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/shrinker/token.rb', line 7

def fetch_unique_token(backend, options = {})
  need_token = true

  round = 0
  while need_token
    token      = generate(round, options)
    need_token = backend.used_token?(token)
    round += 1
  end

  token
end

#generate(round, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/shrinker/token.rb', line 20

def generate(round, options = {})
  length   = Shrinker.config.token_length_target || 6
  strategy = Shrinker.config.token_length_strategy || "longer"
  
  prefix = options[:prefix].to_s
  if !prefix || prefix.length == 0
    # need soemthing
    prefix = rand(99999999).to_s
  end
  
  extra = ""
  if strategy.to_s == "random"
    extra= "__#{rand(99999999999)}"
  else
    length += round
  end
  
  Digest::MD5.hexdigest("#{prefix}#{extra}")[-1*length..-1]
end