Skip to content

Commit

Permalink
Elixir implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
buren committed Sep 30, 2015
1 parent 712a7dd commit f2107ab
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ MailChecker currently supports:
* [NodeJS](https://github.com/FGRibreau/mailchecker/tree/master/platform/node) (CommonJS)
* [JavaScript](https://github.com/FGRibreau/mailchecker/tree/master/platform/javascript) (Client-Side)
* [PHP](https://github.com/FGRibreau/mailchecker/tree/master/platform/php)
* [Elixir](https://github.com/FGRibreau/mailchecker/tree/master/platform/elixir)
* **Easily add support for your own language with MailChecker template system and [send us a pull-request!](https://github.com/FGRibreau/mailchecker/fork_select)**

-------------------------
Expand Down Expand Up @@ -62,6 +63,20 @@ if(!MailChecker('myemail.com')){
}
```

### Elixir

```elixir
Code.require_file("mail_checker.ex", "mailchecker/platform/elixir/")

unless MailChecker.valid?("myemail@yopmail.com") do
raise "O RLY !"
end

unless MailChecker.valid?("myemail.com") do
raise "O RLY !"
end
```

--------------------


Expand Down
28 changes: 28 additions & 0 deletions platform/elixir/mail_checker.ex

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions platform/elixir/mail_checker.tmpl.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule MailChecker do
def valid?(email) do
valid_address?(email) && !in_blacklist?(email)
end

def in_blacklist?(email) do
set = Enum.into(blacklist, HashSet.new)

member = HashSet.member?(set, extract_top_domain(email))
member
end

def blacklist do
[{{& listSTR }}]
end

def extract_top_domain(email) do
domain = String.split(email, "@") |> List.last
domain_parts = String.split(domain, ".")
top_domain_part = Enum.slice(domain_parts, -2..2)
Enum.join(top_domain_part, ".")
end

def valid_address?(email) do
Regex.match?(~r/^(?!(?:(?:\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}))\]))$/i, email)
end

end
36 changes: 36 additions & 0 deletions test/platform.elixir.test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Run tests from the repository root directory:
# $ elixir test/platform.elixir.test.exs
ExUnit.start

try do
Code.require_file("mail_checker.ex", "platform/elixir/")
rescue
_ -> raise ArgumentError, message: "You must be in the repository root directory in order to run the tests."
end

defmodule MailCheckerTest do
use ExUnit.Case

test "should return true if the email is valid" do
assert MailChecker.valid?("plop@plop.com")
assert MailChecker.valid?("my.ok@ok.plop.com")
assert MailChecker.valid?("my+ok@ok.plop.com")
assert MailChecker.valid?("my=ok@ok.plop.com")
assert MailChecker.valid?("ok@gmail.com")
assert MailChecker.valid?("ok@hotmail.com")
end

test "should return false if the email is invalid" do
assert MailChecker.valid?("plopplop.com") == false
assert MailChecker.valid?("my+ok@ok°plop.com") == false
assert MailChecker.valid?("my+ok@ok=plop.com") == false
assert MailChecker.valid?("my,ok@ok.plop.com") == false
assert MailChecker.valid?("ok@tmail.com") == false
end

test "should return false if the email come from a throwable domain" do
assert MailChecker.valid?("ok@33mail.com") == false
assert MailChecker.valid?("ok@ok.33mail.com") == false
end

end

0 comments on commit f2107ab

Please sign in to comment.