Skip to content

Commit

Permalink
Package as Ruby gem
Browse files Browse the repository at this point in the history
  • Loading branch information
buren committed Sep 28, 2015
1 parent fd60471 commit b6dea7d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
node_modules
npm-*.log
compare.js
*.gem
Gemfile.lock
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gemspec
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ if(!MailChecker('myemail.com')){
}
```

### Ruby

```ruby
require 'mail_checker'

unless MailChecker('myemail@yopmail.com')
fail('O RLY!')
end

unless MailChecker.valid?('myemail@yopmail.com')
fail('O RLY!')
end
```

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


Expand All @@ -72,6 +86,11 @@ NodeJS/JavaScript
npm install mailchecker
```

Ruby
```bash
gem install mail_checker
```

__We accept pull-requests for other package manager__.

## Data sources
Expand Down
15 changes: 15 additions & 0 deletions mail_checker.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# coding: utf-8
Gem::Specification.new do |spec|
spec.name = 'mail_checker'
spec.version = '0.1.0'
spec.authors = ['Francois-Guillaume Ribreau', 'Jacob Burenstam']
spec.email = ['github@fgribreau.com']

spec.summary = 'Temporary (disposable/throwaway) email detection library. Covers 1979 fake email providers.'
spec.description = 'Cross-language temporary (disposable/throwaway) email detection library. Covers 1979 fake email providers. http://twitter.com/FGRibreau'
spec.homepage = 'https://github.com/FGRibreau/mailchecker'
spec.license = 'MIT'

spec.files = ['platform/ruby/mail_checker.rb']
spec.require_paths = ['platform/ruby']
end
9 changes: 4 additions & 5 deletions platform/ruby/mail_checker.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'set'
require 'uri'

class MailCheckerImpl
module MailChecker
# Based on PHP FILTER_VALIDATE_EMAIL
# See https://fightingforalostcause.net/content/misc/2006/compare-email-regex.php
EMAIL_REGEX = /^(?!(?:(?:\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
Expand All @@ -10,7 +9,7 @@ class MailCheckerImpl
map(&:downcase).
to_set

def self.test(email)
def self.valid?(email)
return false unless valid_email?(email)

!BLACKLIST.include?(extract_domain(email))
Expand All @@ -24,10 +23,10 @@ def self.valid_email?(email)

def self.extract_domain(email)
domain = email.gsub(/.+@([^.]+)/, '\1').downcase
domain.split('.')[-2,2].join('.') # Don't include subdomains
domain.split('.')[-2, 2].join('.') # Don't include subdomains
end
end

def MailChecker(email)
MailCheckerImpl.test(email)
MailChecker.valid?(email)
end
9 changes: 4 additions & 5 deletions platform/ruby/mail_checker.tmpl.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'set'
require 'uri'

class MailCheckerImpl
module MailChecker
# Based on PHP FILTER_VALIDATE_EMAIL
# See https://fightingforalostcause.net/content/misc/2006/compare-email-regex.php
EMAIL_REGEX = /^(?!(?:(?:\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
Expand All @@ -10,7 +9,7 @@ class MailCheckerImpl
map(&:downcase).
to_set

def self.test(email)
def self.valid?(email)
return false unless valid_email?(email)

!BLACKLIST.include?(extract_domain(email))
Expand All @@ -24,10 +23,10 @@ def self.valid_email?(email)

def self.extract_domain(email)
domain = email.gsub(/.+@([^.]+)/, '\1').downcase
domain.split('.')[-2,2].join('.') # Don't include subdomains
domain.split('.')[-2, 2].join('.') # Don't include subdomains
end
end

def MailChecker(email)
MailCheckerImpl.test(email)
MailChecker.valid?(email)
end
4 changes: 4 additions & 0 deletions test/platform.ruby.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ def test_return_false_if_throwable_domain
invalid!('ok@ok.33mail.com')
invalid!('ok@guerrillamailblock.com')
end

def test_can_be_called_as_regular_method
assert_equal MailChecker.valid?(nil), false
end
end

0 comments on commit b6dea7d

Please sign in to comment.