Is there a recommended algorithm that shortens a string into a set
number of elements(could be a mix of number and strings)
As per sawa's comment, there is no such algorithm.
If you have a finite set of allowed strings, you could however enumerate them and express that number in a suitable base. There is a well-known and well-supported "url safe" version of Base 64, ideal for representing arbitrary compressed data inside URL paths.
For instance, just taking your integer order id, it is already enumerable. If you can safely assume that the maximum allowed value was a 32-bit integer, we can encode that as follows:
require 'base64'
number_to_encode = 1_234_567_890
compact_string = [number_to_encode].pack('N*') # Network byte order
encoded = Base64.urlsafe_encode64( compact_string )
# => "SZYC0g=="
That's taken an id with up to 10 digits, and created a url string with 8 characters from it. To decode it back to the number you need:
require 'base64'
string_to_decode = "SZYC0g==" # e.g. params[:order_id] from /o/:order_id
packed_string = Base64.urlsafe_decode64( string_to_decode )
number = packed_string.unpack('N*').first
# => 1234567890
In principle, you can express any kind of data via this approach, provided you can unpack and disambiguate it in the relevant controller. However, there are limits on compression. You cannot take a parameter that is an arbitrary 32-bit integer, and fit it into 5 base64 characters (because each base64 character is 6 bits of your data at best).
If you need short URLs like you may have seen at bit.ly or tinyurl.com, then this is done by creating a large lookup table of possible URLs, and encoding the id for each row from that table in a similar way to above. Or alternatively, you could store this data as a unique index on each model, and either put sequence numbers into that column, or perhaps generate random strings that you test for uniqueness. All these approaches essentially boil down to having a limited set of references to resolve, turning that into a number (either actual count for the item, or something unique chosen to be under a theoretical maximum), and using an encoding scheme like Base64 to represent it as a smaller number of characters than if you were using base 10.