thingspeak/lib/key_utilities.rb
Andrew Ruthven 3075563564 Fix returning an API Key that was size + 1.
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.
2013-04-21 01:51:21 +12:00

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