Ruby on Rails - generating bit.ly style uuids

后端 未结 4 1856
天涯浪人
天涯浪人 2021-02-10 12:54

I\'m trying to generate UUIDs with the same style as bit.ly urls like:

http://bit.ly/aUekJP

or cloudapp ones:

http://cl.ly/1hVU         


        
4条回答
  •  一生所求
    2021-02-10 13:17

    If you are working with numbers, you can use the built in ruby methods

    6175601989.to_s(30)
     => "8e45ttj" 
    

    to go back

    "8e45ttj".to_i(30)
    =>6175601989
    

    So you don't have to store anything, you can always decode an incoming short_code.

    This works ok for proof of concept, but you aren't able to avoid ambiguous characters like: 1lji0o. If you are just looking to use the code to obfuscate database record IDs, this will work fine. In general, short codes are supposed to be easy to remember and transfer from one medium to another, like reading it on someone's presentation slide, or hearing it over the phone. If you need to avoid characters that are hard to read or hard to 'hear', you might need to switch to a process where you generate an acceptable code, and store it.

提交回复
热议问题