3075563564
0..size iterates through size + 1 elements. So asking for an API that was 16 was actually returning a string of 17 characters. This broke trying to shove it into a varchar(16) in PostgreSQL.
15 lines
364 B
Ruby
15 lines
364 B
Ruby
module KeyUtilities
|
|
|
|
# generates a database unique api key
|
|
def generate_api_key(size = 16)
|
|
alphanumerics = ('0'..'9').to_a + ('A'..'Z').to_a
|
|
k = (0..(size - 1)).map {alphanumerics[Kernel.rand(36)]}.join
|
|
|
|
# if key exists in database, regenerate key
|
|
k = generate_api_key if ApiKey.find_by_api_key(k)
|
|
|
|
# output the key
|
|
k
|
|
end
|
|
end
|