2012-02-10 00:42:16 +01:00
|
|
|
module KeyUtilities
|
|
|
|
|
|
|
|
# generates a database unique api key
|
|
|
|
def generate_api_key(size = 16)
|
|
|
|
alphanumerics = ('0'..'9').to_a + ('A'..'Z').to_a
|
2013-04-20 15:51:21 +02:00
|
|
|
k = (0..(size - 1)).map {alphanumerics[Kernel.rand(36)]}.join
|
2012-02-10 00:42:16 +01:00
|
|
|
|
|
|
|
# if key exists in database, regenerate key
|
|
|
|
k = generate_api_key if ApiKey.find_by_api_key(k)
|
|
|
|
|
|
|
|
# output the key
|
|
|
|
k
|
|
|
|
end
|
2013-04-20 15:51:21 +02:00
|
|
|
end
|