Create UrlGenerator service

This commit is contained in:
Matt-Yorkley
2022-01-15 12:26:18 +00:00
parent 3f5e5d52ad
commit 0ef35b319c
2 changed files with 37 additions and 0 deletions

View File

@@ -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

View File

@@ -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