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:
parent
022d681c33
commit
c6b1ac7162
12
app/helpers/domain_helper.rb
Normal file
12
app/helpers/domain_helper.rb
Normal 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
|
22
test/helpers/domain_helper_test.rb
Normal file
22
test/helpers/domain_helper_test.rb
Normal 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
|
Loading…
Reference in a new issue