Method: StringUtils#urlify
- Defined in:
- lib/string_utils.rb
#urlify(string, opts = {}) ⇒ Object
Converts a string to a nicely readable URL opts: :default_replacement – string to use for unknown characters (Default: “”) :whitespace_replacement – string to use to replace whitespace+ (Default: “-”)
43 44 45 46 47 48 49 50 |
# File 'lib/string_utils.rb', line 43 def urlify(string, opts = {}) opts = {:whitespace_replacement => '-', :default_replacement => ""}.merge(opts) string = string.gsub(WHITESPACES, opts[:whitespace_replacement]) string.strip! string.gsub!(/[^\x00-\x7f]/u) { |char| TRANSLITERATIONS[char] || opts[:default_replacement] } string.gsub!(/[^a-z0-9\-+_]/, opts[:default_replacement]) string end |