Skip to content

Commit

Permalink
refacor(elixir): automatic start_link for backword-compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
choznerol committed Nov 9, 2021
1 parent f09dceb commit dc5636b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
11 changes: 11 additions & 0 deletions platform/elixir/mail_checker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ defmodule MailChecker do
end

def add_custom_domains(new_domains) do
ensure_started()

Agent.update(__MODULE__, fn config ->
Map.update(config, :custom_domains, @initial_config[:custom_domains], fn existing_custom_domains ->
new_domains
Expand Down Expand Up @@ -41,7 +43,16 @@ defmodule MailChecker do
Regex.match?(~r/\A(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))\z/i, email)
end

defp ensure_started do
case start_link() do
{:ok, pid} -> pid
{:error, {:already_started, pid}} -> pid
end
end

defp custom_domains do
ensure_started()

Agent.get(__MODULE__, & &1[:custom_domains])
end

Expand Down
11 changes: 11 additions & 0 deletions platform/elixir/mail_checker.tmpl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ defmodule MailChecker do
end

def add_custom_domains(new_domains) do
ensure_started()

Agent.update(__MODULE__, fn config ->
Map.update(config, :custom_domains, @initial_config[:custom_domains], fn existing_custom_domains ->
new_domains
Expand Down Expand Up @@ -41,7 +43,16 @@ defmodule MailChecker do
Regex.match?(~r/\A{{& unanchoredRegexpString }}\z/i, email)
end

defp ensure_started do
case start_link() do
{:ok, pid} -> pid
{:error, {:already_started, pid}} -> pid
end
end

defp custom_domains do
ensure_started()

Agent.get(__MODULE__, & &1[:custom_domains])
end

Expand Down
8 changes: 4 additions & 4 deletions test/platform.elixir.test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ defmodule MailCheckerTest do
end

test "should return true if the email is valid" do
{:ok, _pid} = MailChecker.start_link() # FIXME: This is a breaking change :(
assert_valid("plop@plop.com")
assert_valid("my.ok@ok.plop.com")
assert_valid("my+ok@ok.plop.com")
Expand All @@ -48,15 +47,13 @@ defmodule MailCheckerTest do
end

test "should return false if the email come from a throwable domain" do
{:ok, _pid} = MailChecker.start_link() # FIXME: This is a breaking change :(
assert_invalid("ok@tmail.com")
assert_invalid("ok@33mail.com")
assert_invalid("ok@ok.33mail.com")
assert_invalid("ok@guerrillamailblock.com")
end

test "should return false if the email is from a blacklisted domain" do
{:ok, _pid} = MailChecker.start_link() # FIXME: This is a breaking change :(
Enum.each(MailChecker.blacklist, fn domain ->
assert_invalid("test@" <> domain)
assert_invalid("test@subdomain." <> domain)
Expand All @@ -65,7 +62,6 @@ defmodule MailCheckerTest do
end

test "should return false if the email is from a custom domain" do
{:ok, _pid} = MailChecker.start_link()
assert_valid("foo@youtube.com")
assert_valid("foo@google.com")
assert_valid("ok@gmail.com")
Expand All @@ -75,5 +71,9 @@ defmodule MailCheckerTest do
assert_invalid("foo@youtube.com")
assert_invalid("foo@google.com")
assert_valid("ok@gmail.com")

MailChecker.add_custom_domains(["gmail.com"])

assert_invalid("ok@gmail.com")
end
end

0 comments on commit dc5636b

Please sign in to comment.