Skip to content

Instantly share code, notes, and snippets.

@simplyb
Forked from cupakromer/gist:7550244
Last active December 28, 2015 19:29
Show Gist options
  • Save simplyb/7550368 to your computer and use it in GitHub Desktop.
Save simplyb/7550368 to your computer and use it in GitHub Desktop.
# 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)
send "#{attr}=", tokenize(attr)
end
end
def tokenize(attr)
token = loop do
t = SecureRandom.hex(16)
break t unless self.class.exists?(attr => t)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment