Skip to content

Commit

Permalink
Merge pull request #32 from benbalter/nuke-email-veracity
Browse files Browse the repository at this point in the history
drop email veracity
  • Loading branch information
benbalter committed Jan 31, 2014
2 parents b0298fe + f5b45b5 commit a0469cc
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 33 deletions.
2 changes: 0 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ PATH
specs:
gman (1.0.0)
addressable
email_veracity
public_suffix
swot

Expand All @@ -18,7 +17,6 @@ GEM
tzinfo (~> 0.3.37)
addressable (2.3.5)
atomic (1.1.14)
email_veracity (0.6.0)
i18n (0.6.9)
json (1.8.1)
minitest (4.7.5)
Expand Down
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ Gman.valid? "[email protected]" #true
Gman.valid? "[email protected]" #false
```

### Really verify an email address

(also verifies that the server returns a valid MX record)

```ruby
Gman.valid? "[email protected]", true #true
Gman.valid? "[email protected]", true #false
```
### Verify domain

```ruby
Expand All @@ -53,8 +45,6 @@ Gman.get_domain "http://foo.bar.gov" # foo.bar.gov
Gman.get_domain "[email protected]" # bar.gov
Gman.get_domain "foo.bar.gov" # foo.bar.gov
Gman.get_domain "asdf@asdf" # nil (no domain within the string)
Gman.get_domain "[email protected]", true #false (no MX record)
Gman.get_domain "[email protected]", true # true (valid MX record)
```

## Contributing
Expand Down
1 change: 0 additions & 1 deletion gman.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.add_dependency( "public_suffix" )
s.add_dependency( "swot" )
s.add_dependency( "email_veracity" )
s.add_dependency( "addressable" )

s.add_development_dependency( "rake" )
Expand Down
57 changes: 43 additions & 14 deletions lib/gman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,48 @@
require 'yaml'
require 'swot'
require "addressable/uri"
require "email_veracity"

module Gman

# Source: http://bit.ly/1n2X9iv
EMAIL_REGEX = %r{
^
(
[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+
\.
)
*
[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+
@
(
(
(
(
(
[a-z0-9]{1}
[a-z0-9\-]{0,62}
[a-z0-9]{1}
)
|
[a-z]
)
\.
)+
[a-z]{2,6}
)
|
(
\d{1,3}
\.
){3}
\d{1,3}
(
\:\d{1,5}
)?
)
$
}xi

class << self

# Normalizes and checks if a given string represents a governemnt domain
Expand All @@ -16,20 +54,12 @@ class << self
# "foo.gov.uk"
# "http://foo.bar.gov"
#
# check_mx - if an email is passed, check the domain for an mx record
#
# Returns boolean true if a government domain
def valid?(text, check_mx=false)
def valid?(text)

domain = get_domain text
return false unless PublicSuffix.valid?(domain)

# validate mx record
if check_mx && email?(text)
EmailVeracity::Config[:skip_lookup] = false
return false unless EmailVeracity::Address.new(text).valid?
end

# Ensure non-edu
return false if Swot::is_academic?(domain)

Expand All @@ -44,7 +74,7 @@ def valid?(text, check_mx=false)
# returns an instance of our custom public suffix list
# list behaves like PublicSuffix::List but is limited to our whitelisted domains
def list
@list ||= PublicSuffix::List::parse( File.new(File.join(File.dirname(__FILE__), "domains.txt"), "r:utf-8"))
@list ||= PublicSuffix::List::parse(File.new(File.join(File.dirname(__FILE__), "domains.txt"), "r:utf-8"))
end

# Get the FQDN name from a URL or email address.
Expand All @@ -60,7 +90,7 @@ def get_domain(text)
if uri.host # valid https?://* URI
uri.host
elsif email?(text)
EmailVeracity::Address.new(text).domain.to_s
text.match(/@([\w\.\-]+)\Z/i)[1]
else # url sans http://
begin
uri = Addressable::URI.parse("http://#{text}")
Expand Down Expand Up @@ -90,8 +120,7 @@ def domain_parts(text)
#
# Returns true if email, otherwise false
def email?(text)
EmailVeracity::Config[:skip_lookup] = true
EmailVeracity::Address.new(text).valid?
text =~ EMAIL_REGEX
end
end
end
6 changes: 0 additions & 6 deletions test/test_gman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ class TestGman < Test::Unit::TestCase
assert_equal nil, Gman.get_domain("foo")
end

should "validate mx records when asked" do
assert_equal true, Gman.valid?("[email protected]", true)
assert_equal false, Gman.valid?("[email protected]", true)
assert_equal true, Gman.valid?("[email protected]", false)
end

should "not err out on invalid domains" do
assert_equal false, Gman.valid?("[email protected]")
assert_equal "act.gov.au", Gman.get_domain("[email protected]")
Expand Down

0 comments on commit a0469cc

Please sign in to comment.