Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clojure implementation #40

Merged
merged 2 commits into from
Oct 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ MailChecker currently supports:
* [Python](https://github.com/FGRibreau/mailchecker/tree/master/platform/python)
* [Ruby](https://github.com/FGRibreau/mailchecker/tree/master/platform/ruby)
* [Elixir](https://github.com/FGRibreau/mailchecker/tree/master/platform/elixir)
* [Clojure](https://github.com/FGRibreau/mailchecker/tree/master/platform/clojure)
* **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 @@ -114,6 +115,19 @@ unless MailChecker.valid?('myemail@yopmail.com')
end
```

### Clojure

```clojure
; no package yet; just drop in mailchecker.clj where you want to use it.
(load-file "platform/clojure/mailchecker.clj")

(if (not (mailchecker/valid? "myemail@yopmail.com"))
(throw (Throwable. "O RLY!")))

(if (not (mailchecker/valid? "myemail.com"))
(throw (Throwable. "O RLY!")))
```

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


Expand Down
59 changes: 59 additions & 0 deletions platform/clojure/mailchecker.clj

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions platform/clojure/mailchecker.tmpl.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
(ns mailchecker)

(require '[clojure.string :as str])

(def ^:const blacklist (set [{{& listSTR }}]))

; Source: https://github.com/scstarkey/noir/blob/998e846dd44f42b8e01a6977e6d22a3eff5e4542/src/noir/validation.clj#L37-L40
; Modified to return true/false
(defn is-email?
"Returns true if email is an email address"
[email]
(if (re-matches #"(?i)[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?" email)
true false))

(defn at-split
"Returns list from string splitted on @ char"
[email]
(str/split email #"@"))

(defn last-element
"Returns the last element of the arr"
[arr]
(first
(take-last 1 arr)))

(defn domain-part
"Returns the domain part from email"
[email]
(last-element (at-split email)))

(defn dot-join
"Returns string from arr joined with dot char"
[arr]
(str/join "." arr))

(defn dot-split
"Returns list from string splitted on dot char"
[string]
(str/split string #"\."))

(defn top-domain-part
"Returns the top domain for email"
[email]
(dot-join
(take-last 2
(dot-split (domain-part email)))))

(defn in-blacklist?
"Returns true if email domain is not in the blacklist"
[email]
(contains? blacklist (top-domain-part email)))

(defn valid?
"Returns true if the email is valid"
[email]
(and
(is-email? email)
(not
(in-blacklist? email))))
39 changes: 39 additions & 0 deletions test/platform.clojure.test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
; Run test with the lein-exec plugin:
; $ lein exec test/platform.clojure.test.clj
(load-file "platform/clojure/mailchecker.clj")

(ns clojure.test.example
(:use clojure.test))

; Valid
(deftest true-for-valid-1
(is (= true (mailchecker/valid? "plop@plop.com"))))
(deftest true-for-valid-2
(is (= true (mailchecker/valid? "my.ok@ok.plop.com"))))
(deftest true-for-valid-3
(is (= true (mailchecker/valid? "my+ok@ok.plop.com"))))
(deftest true-for-valid-4
(is (= true (mailchecker/valid? "my=ok@ok.plop.com"))))
(deftest true-for-valid-5
(is (= true (mailchecker/valid? "ok@gmail.com"))))
(deftest true-for-valid-6
(is (= true (mailchecker/valid? "ok@hotmail.com"))))

; Invalid
(deftest false-for-invalid-1
(is(= false (mailchecker/valid? "plopplop.com"))))
(deftest false-for-invalid-2
(is(= false (mailchecker/valid? "my+ok@ok=plop.com"))))
(deftest false-for-invalid-3
(is(= false (mailchecker/valid? "my,ok@ok.plop.com"))))

(deftest false-for-spam-1
(is(= false (mailchecker/valid? "ok@tmail.com"))))
(deftest false-for-spam-2
(is(= false (mailchecker/valid? "ok@33mail.com"))))
(deftest false-for-spam-3
(is(= false (mailchecker/valid? "ok@ok.33mail.com"))))
(deftest false-for-spam-4
(is(= false (mailchecker/valid? "ok@guerrillamailblock.com"))))

(run-all-tests)