Hi :) I have a question I'm not really sure how to fix or solve the specific problem.
I am using MongoDB .
I would like to make my route small to share it to the public.
For example
https://example.com/api/v1/users/:user_id/pictures/:picture_id
To
Because I don't want that other users see the how path. I hope you guys can help me :)
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