Skip to content

Instantly share code, notes, and snippets.

@cupakromer
Created November 19, 2013 18:39
Show Gist options
  • Save cupakromer/7550244 to your computer and use it in GitHub Desktop.
Save cupakromer/7550244 to your computer and use it in GitHub Desktop.
RefactorMelee : Arg this is terrible code, halp!
# The idea here is that if any of the three tokens are not already
# set, then they should be generated. In order to generate one, it
# must be unique.
def generate_tokens!
[ :code, :access_token, :refresh_token ].each do |attr|
next if send(attr)
token = loop do
t = SecureRandom.hex(16)
break t unless self.class.exists?(attr => t)
end
send "#{attr}=", token
end
end
@jwieringa
Copy link

Very similar to code that I wrote (I think you looked at this with me).

def generate_unique_link
  loop do
    self.link = SecureRandom.urlsafe_base64
    break unless self.class.exists?(link: link)
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment