Generates a well-formed and unique URI from an array of strings.
$ gem install baptist
Or add it to your Gemfile.
Baptist.generate # => 'hMAkUyhyqdPkSDWHaUtptQ' Baptist.generate('Arthur Russell') # => 'Arthur-Russell' Baptist.generate('Arthur Russell', :space => '_') # => 'Arthur_Russell' Baptist.generate(['Arthur Russell', 'Calling Out of Context']) # => 'Arthur-Russell/Calling-Out-of-Context' Baptist.generate(['Rihanna', 'Loud'], :modifier => 'Explicit') # => 'Rihanna/Loud-(Explicit)'
Baptist will percent encode any character (except the delimiter characters explicitly added by Baptist) that is not an unreserved URI character according to RFC3986.
To guarantee the generated URI is unique:
Baptist.generate('Arthur Russell', :multiplier => '*') do |uri| Resource.find_by_uri(uri) end
Will take the
:multiplier character and multiply it for each time
the block returns
false and add that to the end of the URI. So if
there were three name collisions when running the code above the resulting
URI would be
The default multiplier is simply the Integer 1, which will result in an incrementing number being added to the end of the URI.
:space- Space character (default: '-')
:separator- Separator character (default: '/')
:modifier- Will add a modifier string in parentheses at the end of the generated URI
:multiplier- The object to multiply with to find a unique URI (default: 1)
:encoding- Force this encoding (default: 'UTF-8')
Baptist was created by Niklas Holmgren (firstname.lastname@example.org) and released under the MIT license.