21
1
Fork 0
mirror of https://github.com/Evolix/chexpire.git synced 2024-04-29 07:20:50 +02:00

Domain tld & normalize helpers

This commit is contained in:
Colin Darie 2018-05-29 22:33:35 +02:00
parent 022d681c33
commit c6b1ac7162
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
2 changed files with 34 additions and 0 deletions

View file

@ -0,0 +1,12 @@
module DomainHelper
def normalize_domain(str)
str.strip.downcase
end
def tld(str)
parts = normalize_domain(str).split(".")
fail ArgumentError unless parts.size >= 2
".#{parts.last}"
end
end

View file

@ -0,0 +1,22 @@
require "test_helper"
require "domain_helper"
class DomainHelperTest < ActiveSupport::TestCase
include DomainHelper
test "should normalize a domain name" do
assert_equal "example.org", normalize_domain(" example.org ")
assert_equal "example.org", normalize_domain("eXaMple.oRg")
end
test "tld should return the domain tld" do
assert_equal ".org", tld("exaMple.ORG")
assert_equal ".fr", tld("www.example.fr")
assert_equal ".com", tld("www.example-dashed.com")
assert_equal ".uk", tld("www.example.co.uk")
assert_raises(ArgumentError) do
tld("not a domain")
end
end
end