You can use shortener gem, but there is no fun in using 3rd party gems. So, If you want in-house version then you can use following sample code.
ALLOWED_CHARACTER_SPACE = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split(//)
def convert_uid_to_short(uid)
surl = ''
base = ALLOWED_CHARACTER_SPACE.length
while uid > 0
surl << ALLOWED_CHARACTER_SPACE[uid.modulo(base)]
uid /= base
end
surl.reverse
end
In above method, you have pass uid
, a unique identifier in integer format for your url/api. It will return a short url for the unique identifier. You can then use the short version in your code appropriately.
Sample:
convert_uid_to_short(10)
output: k
convert_uid_to_short(1043234)
output: exyw