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

Use class methods in PHP/Python; update README #56

Merged
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
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ MailChecker currently supports:
```javascript
var MailChecker = require('mailchecker');

if(!MailChecker('myemail@yopmail.com')){
if(!MailChecker.is_valid('myemail@yopmail.com')){
console.error('O RLY !');
process.exit(1);
}

if(!MailChecker('myemail.com')){
if(!MailChecker.is_valid('myemail.com')){
console.error('O RLY !');
process.exit(1);
}
Expand All @@ -52,11 +52,11 @@ if(!MailChecker('myemail.com')){
```html
<script type="text/javascript" src="MailChecker/platform/javascript/MailChecker.js"></script>
<script type="text/javascript">
if(!MailChecker('myemail@yopmail.com')){
if(!MailChecker.is_valid('myemail@yopmail.com')){
console.error('O RLY !');
}

if(!MailChecker('myemail.com')){
if(!MailChecker.is_valid('myemail.com')){
console.error('O RLY !');
}
</script>
Expand All @@ -67,11 +67,11 @@ if(!MailChecker('myemail.com')){
```php
include __DIR__."/MailChecker/platform/php/MailChecker.php";

if(!MailChecker('myemail@yopmail.com')){
if(!MailChecker::isValid('myemail@yopmail.com')){
die('O RLY !');
}

if(!MailChecker('myemail.com')){
if(!MailChecker::isValid('myemail.com')){
die('O RLY !');
}
```
Expand All @@ -95,9 +95,8 @@ end
```python
# no package yet; just drop in MailChecker.py where you want to use it.
import MailChecker
m = MailChecker.MailChecker()

if not m.is_valid('bla@example.com'):
if not MailChecker.is_valid('bla@example.com'):
print "O RLY !"
```

Expand Down
28 changes: 14 additions & 14 deletions platform/php/MailChecker.php

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions platform/php/MailChecker.tmpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,48 @@
* Usage
*
* include('mailchecker/platform/php/Mailchecker.php');
* $mail_checker = new MailChecker->isValid(String email);
* MailChecker::isValid(String email);
* @return {Boolean} true is the specified email is valid, false otherwise
*/

class MailChecker {
private $blacklist;

public function __construct() {
$this->blacklist = array_unique(array({{& listSTR }}));
static $blacklist;
static function init() {
self::$blacklist = array_unique(array({{& listSTR }}));
}

public function isValid($email) {
static function isValid($email) {
$email = strtolower($email);

return $this->validEmail($email) && !$this->isBlacklisted($email);
return self::validEmail($email) && !self::isBlacklisted($email);
}

public function blacklist() {
return $this->blacklist;
static function blacklist() {
return self::$blacklist;
}

private function validEmail($email) {
private static function validEmail($email) {
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
return false;
} else {
return true;
}
}

private function isBlacklisted($email) {
private static function isBlacklisted($email) {
$parts = explode("@", $email);
$domain = end($parts);

foreach ($this->allDomainSuffixes($domain) as $domainSuffix) {
if (in_array($domainSuffix, $this->blacklist)) {
foreach (self::allDomainSuffixes($domain) as $domainSuffix) {
if (in_array($domainSuffix, self::$blacklist)) {
return true;
}
}

return false;
}

private function allDomainSuffixes($domain) {
private static function allDomainSuffixes($domain) {
$components = explode('.', $domain);

$return = [];
Expand All @@ -57,3 +56,4 @@ private function allDomainSuffixes($domain) {
return $return;
}
}
MailChecker::init();
27 changes: 15 additions & 12 deletions platform/python/MailChecker.py

Large diffs are not rendered by default.

27 changes: 15 additions & 12 deletions platform/python/MailChecker.tmpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@

class MailChecker(object):

def __init__(self):
self.blacklist = set([{{& listSTR}}])
self.valid_matcher = re.compile(r"{{& regexp}}")
blacklist = set([{{& listSTR}}])
valid_matcher = re.compile(r"{{& regexp}}")

def is_valid(self, email):
@classmethod
def is_valid(cls, email):
email = email.lower().strip()

return (self.is_valid_email_format(email) and
not self.is_blacklisted(email))
return (cls.is_valid_email_format(email) and
not cls.is_blacklisted(email))

def all_domain_suffixes(self, email):
@classmethod
def all_domain_suffixes(cls, email):
domain = email.split("@")[-1]
components = domain.split(".")

return (".".join(components[n:]) for n in xrange(0, len(components)))

def is_blacklisted(self, email):
all_domain_suffixes = self.all_domain_suffixes(email)
return any(domain in self.blacklist for domain in all_domain_suffixes)
@classmethod
def is_blacklisted(cls, email):
all_domain_suffixes = cls.all_domain_suffixes(email)
return any(domain in cls.blacklist for domain in all_domain_suffixes)

def is_valid_email_format(self, email):
return bool(email) and self.valid_matcher.search(email) is not None
@classmethod
def is_valid_email_format(cls, email):
return bool(email) and cls.valid_matcher.search(email) is not None
10 changes: 2 additions & 8 deletions test/platform.php.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@

class MailCheckerTest extends PHPUnit_Framework_TestCase
{
protected $mailChecker = null;

public function setUp() {
$this->mailChecker = new MailChecker();
}

public function assertIsValidResult($expected, $email) {
$this->assertEquals($expected, $this->mailChecker->isValid($email));
$this->assertEquals($expected, MailChecker::isValid($email));
}

public function isValid($email) {
Expand Down Expand Up @@ -49,7 +43,7 @@ public function testReturnFalseIfThrowableDomain() {
}

public function testReturnFalseForBlacklistedDomainsAndTheirSubdomains() {
foreach($this->mailChecker->blacklist() as $blacklisted_domain) {
foreach(MailChecker::blacklist() as $blacklisted_domain) {
$this->isInvalid("test@" . $blacklisted_domain);
$this->isInvalid("test@subdomain." . $blacklisted_domain);
# Should not be invalid as a subdomain of a valid domain.
Expand Down
7 changes: 2 additions & 5 deletions test/platform.python.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
import unittest

class TestMailCheckerIsValid(unittest.TestCase):
def setUp(self):
self.mail_checker = MailChecker()

def assert_is_valid_result(self, expected_valid, email):
self.assertEqual(self.mail_checker.is_valid(email), expected_valid)
self.assertEqual(MailChecker.is_valid(email), expected_valid)

def valid(self, email):
self.assert_is_valid_result(True, email)
Expand Down Expand Up @@ -46,7 +43,7 @@ def test_return_false_if_throwable_domain(self):
self.invalid("ok@guerrillamailblock.com")

def test_return_false_for_blacklisted_domains_and_their_subdomains(self):
for blacklisted_domain in self.mail_checker.blacklist:
for blacklisted_domain in MailChecker.blacklist:
self.invalid("test@" + blacklisted_domain)
self.invalid("test@subdomain." + blacklisted_domain)
# Should not be invalid as a subdomain of a valid domain.
Expand Down