diff --git a/app/services/url_generator.rb b/app/services/url_generator.rb new file mode 100644 index 0000000000..d3d352124f --- /dev/null +++ b/app/services/url_generator.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: false + +# Converts strings for use as parts of a URL. The input can contain non-roman/non-UTF8 characters +# and the output will still be valid (including some transliteration). Examples: +# +# "Top Cat!" -> "top-cat" +# "Père Noël" -> "pere-noel" +# "你好" -> "ni-hao" + +require "stringex/unidecoder" + +class UrlGenerator + def self.to_url(string) + Stringex::Unidecoder.decode(string.to_s).parameterize + end +end diff --git a/spec/services/url_generator_spec.rb b/spec/services/url_generator_spec.rb new file mode 100644 index 0000000000..29eca39b95 --- /dev/null +++ b/spec/services/url_generator_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe UrlGenerator do + subject { UrlGenerator } + + describe "#to_url" do + it "converts to url-safe strings and removes unusable characters" do + expect(subject.to_url("Top Cat!?")).to eq "top-cat" + end + + it "handles unusual characters like accents and umlauts" do + expect(subject.to_url("Père Noël")).to eq "pere-noel" + end + + it "handles transliteration of Chinese characters" do + expect(subject.to_url("你好")).to eq "ni-hao" + end + end +end