ced 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +46 -0
- data/Rakefile +10 -0
- data/ced.gemspec +22 -0
- data/lib/ced/email.rb +41 -0
- data/lib/ced/email_fetcher.rb +51 -0
- data/lib/ced/email_response.rb +21 -0
- data/lib/ced/version.rb +3 -0
- data/lib/ced.rb +7 -0
- data/test/email_fetcher_test.rb +82 -0
- data/test/email_response_test.rb +57 -0
- data/test/email_test.rb +244 -0
- data/test/fixtures/invalid_email.json +6 -0
- data/test/fixtures/not_seen_before_email.json +5 -0
- data/test/fixtures/valid_email.json +5 -0
- data/test/test_helper.rb +44 -0
- metadata +111 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Simon Schoeters
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# CED
|
2
|
+
|
3
|
+
The **C**entral **E**mail **D**atabase is an email verification service. You pass it an email address and it tells you if the email address is real or not. This gem wraps the API in a more Ruby friendly syntax. CED also corrects common typos in email domains.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'ced'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install ced
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Minimum workable example:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require "ced"
|
25
|
+
=> true
|
26
|
+
email = CED::Email.new("john@example.con", "ced.example.com", "client", "key")
|
27
|
+
=> #<CED::Email:0x007ffd4198c358 @address="john@example.con", @client="client", @key="key">
|
28
|
+
email.verified?
|
29
|
+
=> true
|
30
|
+
email.corrected_email
|
31
|
+
=> "john@example.com"
|
32
|
+
email.valid?
|
33
|
+
=> false
|
34
|
+
email.seen_before?
|
35
|
+
=> true
|
36
|
+
email.error
|
37
|
+
=> "Invalid domain name"
|
38
|
+
```
|
39
|
+
|
40
|
+
You can skip the host, client and key parameters and specify the `ENV['CED_HOST']`, `ENV['CED_CLIENT']` and `ENV['CED_KEY']` environment variable for convenience.
|
41
|
+
|
42
|
+
**WARNING** Don't blindly trust `email.valid?` as it will also return `false` when the request failed. Always check for `email.verified?` before inspecting the other attributes!
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
Something missing? Found a bug? Horrified by the code? Open a [github issue](https://github.com/cimm/ced/issues), write a failing test or add some code using pull requests. Your help is greatly appreciated!
|
data/Rakefile
ADDED
data/ced.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ced/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "ced"
|
8
|
+
gem.version = CED::VERSION
|
9
|
+
gem.authors = ["Simon Schoeters"]
|
10
|
+
gem.email = ["hamfilter@gmail.com"]
|
11
|
+
gem.description = %q{Ruby interface for CED's email verification service.}
|
12
|
+
gem.summary = %q{The Central Email Database is an email verification service. You pass it an email address and it tells you if the email address is real or not. This gem wraps the API in a more Ruby friendly syntax. CED also corrects common typos in email domains.}
|
13
|
+
gem.homepage = "https://github.com/cimm/ced"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency 'webmock', '~> 1.9.0'
|
21
|
+
gem.add_development_dependency 'rake', '~> 10.0.3'
|
22
|
+
end
|
data/lib/ced/email.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module CED
|
2
|
+
class Email
|
3
|
+
def initialize(address, host = ENV['CED_HOST'], client = ENV['CED_CLIENT'], key = ENV['CED_KEY'])
|
4
|
+
raise ArgumentError, "Missing address" if address.nil? || address.strip.empty?
|
5
|
+
raise ArgumentError, "Missing CED host" if host.nil? || host.strip.empty?
|
6
|
+
raise ArgumentError, "Missing CED client" if client.nil? || client.strip.empty?
|
7
|
+
raise ArgumentError, "Missing CED key" if key.nil? || key.strip.empty?
|
8
|
+
@address = address
|
9
|
+
@host = host
|
10
|
+
@client = client
|
11
|
+
@key = key
|
12
|
+
end
|
13
|
+
|
14
|
+
def verified?
|
15
|
+
raw_email.keys.any?
|
16
|
+
end
|
17
|
+
|
18
|
+
def corrected_email
|
19
|
+
raw_email["email"] || ""
|
20
|
+
end
|
21
|
+
|
22
|
+
def valid?
|
23
|
+
!!raw_email["valid"]
|
24
|
+
end
|
25
|
+
|
26
|
+
def seen_before?
|
27
|
+
!!raw_email["db"]
|
28
|
+
end
|
29
|
+
|
30
|
+
def error
|
31
|
+
raw_email["error"] || ""
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def raw_email
|
37
|
+
email_fetcher = EmailFetcher.new(@host, @client, @key)
|
38
|
+
@raw_email ||= email_fetcher.fetch_raw_email(@address)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'net/https'
|
2
|
+
require 'openssl'
|
3
|
+
require 'json'
|
4
|
+
require 'uri'
|
5
|
+
|
6
|
+
module CED
|
7
|
+
class EmailFetcher
|
8
|
+
EMAIL_PATH = "/api/v1/Email/validate"
|
9
|
+
|
10
|
+
def initialize(host, client, key)
|
11
|
+
raise ArgumentError, "Missing CED host" if host.nil? || host.strip.empty?
|
12
|
+
raise ArgumentError, "Missing CED client" if client.nil? || client.strip.empty?
|
13
|
+
raise ArgumentError, "Missing CED key" if key.nil? || key.strip.empty?
|
14
|
+
@host = host
|
15
|
+
@client = client
|
16
|
+
@key = key
|
17
|
+
end
|
18
|
+
|
19
|
+
def fetch_raw_email(address)
|
20
|
+
email_response = fetch_email(address)
|
21
|
+
email_response.raw_email
|
22
|
+
rescue Timeout::Error => e
|
23
|
+
{}
|
24
|
+
end
|
25
|
+
|
26
|
+
def fetch_email(address)
|
27
|
+
uri = verification_uri(address)
|
28
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
29
|
+
http.use_ssl = true
|
30
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE # Host certificate does not cover the subdomain
|
31
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
32
|
+
response = http.request(request)
|
33
|
+
puts response.body
|
34
|
+
EmailResponse.new(response)
|
35
|
+
end
|
36
|
+
|
37
|
+
def verification_uri(address)
|
38
|
+
arguments = [{email: address}].to_json
|
39
|
+
signature = sign_arguments(arguments)
|
40
|
+
query = URI.encode_www_form(client: @client, arguments: arguments, signature: signature)
|
41
|
+
URI::HTTPS.build({host: @host, path: EMAIL_PATH, query: query})
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def sign_arguments(arguments)
|
47
|
+
digest = OpenSSL::Digest::Digest.new("sha1")
|
48
|
+
OpenSSL::HMAC.hexdigest(digest, @key, arguments)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module CED
|
4
|
+
class EmailResponse
|
5
|
+
SUCCESS_STATUS_CODE = 200
|
6
|
+
EMPTY_JSON = "{}"
|
7
|
+
|
8
|
+
def initialize(response)
|
9
|
+
@response = response
|
10
|
+
end
|
11
|
+
|
12
|
+
def successful?
|
13
|
+
@response.code.to_i == SUCCESS_STATUS_CODE
|
14
|
+
end
|
15
|
+
|
16
|
+
def raw_email
|
17
|
+
body = successful? ? @response.body : EMPTY_JSON
|
18
|
+
JSON.parse(body)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/ced/version.rb
ADDED
data/lib/ced.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/test_helper")
|
2
|
+
|
3
|
+
module CED
|
4
|
+
describe EmailFetcher do
|
5
|
+
ADDRESS = "john.doe@example.com"
|
6
|
+
HOST = "ced.example.com"
|
7
|
+
CLIENT = "client"
|
8
|
+
KEY = "A1B2c3DeFGhi"
|
9
|
+
|
10
|
+
before do
|
11
|
+
@email_verification_request = stub_email_verification_request(valid_email_body)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "accepts a host" do
|
15
|
+
email_fetcher = EmailFetcher.new(HOST, CLIENT, KEY)
|
16
|
+
email_fetcher.instance_variable_get(:@host).must_equal(HOST)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "accepts a client" do
|
20
|
+
email_fetcher = EmailFetcher.new(HOST, CLIENT, KEY)
|
21
|
+
email_fetcher.instance_variable_get(:@client).must_equal(CLIENT)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "accepts a key" do
|
25
|
+
email_fetcher = EmailFetcher.new(HOST, CLIENT, KEY)
|
26
|
+
email_fetcher.instance_variable_get(:@key).must_equal(KEY)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "requires a host" do
|
30
|
+
lambda { EmailFetcher.new(nil, CLIENT, KEY) }.must_raise(ArgumentError)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "requires a non empty host" do
|
34
|
+
lambda { EmailFetcher.new("", CLIENT, KEY) }.must_raise(ArgumentError)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "requires a client" do
|
38
|
+
lambda { EmailFetcher.new(HOST, nil, KEY) }.must_raise(ArgumentError)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "requires a non empty client" do
|
42
|
+
lambda { EmailFetcher.new(HOST, "", KEY) }.must_raise(ArgumentError)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "requires a key" do
|
46
|
+
lambda { EmailFetcher.new(HOST, CLIENT, nil) }.must_raise(ArgumentError)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "requires a non empty key" do
|
50
|
+
lambda { EmailFetcher.new(HOST, CLIENT, "") }.must_raise(ArgumentError)
|
51
|
+
end
|
52
|
+
|
53
|
+
describe :fetch_raw_email do
|
54
|
+
it "returns the raw email" do
|
55
|
+
email_fetcher = EmailFetcher.new(HOST, CLIENT, KEY)
|
56
|
+
parsed_valid_email_body = JSON.parse(valid_email_body)
|
57
|
+
email_fetcher.fetch_raw_email(ADDRESS).must_equal(parsed_valid_email_body)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe :fetch_email do
|
62
|
+
it "verifies the email with CED" do
|
63
|
+
email_fetcher = EmailFetcher.new(HOST, CLIENT, KEY)
|
64
|
+
email_fetcher.fetch_email(ADDRESS)
|
65
|
+
assert_requested(@email_verification_request)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "returns an email response" do
|
69
|
+
email_fetcher = EmailFetcher.new(HOST, CLIENT, KEY)
|
70
|
+
email_fetcher.fetch_email(ADDRESS).must_be_instance_of(EmailResponse)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe :verification_uri do
|
75
|
+
it "returns the verification URI" do
|
76
|
+
email_fetcher = EmailFetcher.new(HOST, CLIENT, KEY)
|
77
|
+
uri = email_fetcher.verification_uri(ADDRESS)
|
78
|
+
uri.to_s.must_equal("https://ced.example.com/api/v1/Email/validate?client=client&arguments=%5B%7B%22email%22%3A%22john.doe%40example.com%22%7D%5D&signature=cfbf8f9713f81a03027b4e977f5f74af050167cb")
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/test_helper")
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module CED
|
5
|
+
describe EmailResponse do
|
6
|
+
before do
|
7
|
+
@response = Net::HTTPResponse.new(1.0, 200, "OK")
|
8
|
+
end
|
9
|
+
|
10
|
+
describe :successful? do
|
11
|
+
describe "when it succeeded" do
|
12
|
+
it "returns true" do
|
13
|
+
email_response = EmailResponse.new(@response)
|
14
|
+
email_response.successful?.must_equal(true)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "when something went wrong" do
|
19
|
+
before do
|
20
|
+
@response = Net::HTTPResponse.new(1.0, 500, "Internal Server Error")
|
21
|
+
end
|
22
|
+
|
23
|
+
it "returns false" do
|
24
|
+
email_response = EmailResponse.new(@response)
|
25
|
+
email_response.successful?.must_equal(false)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe :raw_email do
|
31
|
+
describe "when it succeeded" do
|
32
|
+
before do
|
33
|
+
@response = MiniTest::Mock.new # can't set response.body for some reason
|
34
|
+
@response.expect(:code, "200")
|
35
|
+
@response.expect(:body, valid_email_body)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "returns the raw email" do
|
39
|
+
email_response = EmailResponse.new(@response)
|
40
|
+
parsed_valid_email_body = JSON.parse(valid_email_body)
|
41
|
+
email_response.raw_email.must_equal(parsed_valid_email_body)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "when something went wrong" do
|
46
|
+
before do
|
47
|
+
@response = Net::HTTPResponse.new(1.0, 500, "Internal Server Error")
|
48
|
+
end
|
49
|
+
|
50
|
+
it "returns an empty raw email" do
|
51
|
+
email_response = EmailResponse.new(@response)
|
52
|
+
email_response.raw_email.must_equal({})
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/test/email_test.rb
ADDED
@@ -0,0 +1,244 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/test_helper")
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module CED
|
5
|
+
describe Email do
|
6
|
+
ADDRESS = "john.doe@example.com"
|
7
|
+
HOST = "ced.example.com"
|
8
|
+
CLIENT = "client"
|
9
|
+
KEY = "A1B2c3DeFGhi"
|
10
|
+
|
11
|
+
before do
|
12
|
+
stub_email_verification_request(valid_email_body)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "accepts an address" do
|
16
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
17
|
+
email.instance_variable_get(:@address).must_equal(ADDRESS)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "accepts a host" do
|
21
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
22
|
+
email.instance_variable_get(:@host).must_equal(HOST)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "accepts a client" do
|
26
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
27
|
+
email.instance_variable_get(:@client).must_equal(CLIENT)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "accepts a key" do
|
31
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
32
|
+
email.instance_variable_get(:@key).must_equal(KEY)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "requires an address" do
|
36
|
+
lambda { Email.new(nil, HOST, CLIENT, KEY) }.must_raise(ArgumentError)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "requires a non empty address" do
|
40
|
+
lambda { EmailFetcher.new("", HOST, CLIENT, KEY) }.must_raise(ArgumentError)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "requires a host" do
|
44
|
+
ENV['CED_HOST'] = nil
|
45
|
+
lambda { Email.new(ADDRESS, nil, CLIENT, KEY) }.must_raise(ArgumentError)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "requires a non empty host" do
|
49
|
+
ENV['CED_HOST'] = nil
|
50
|
+
lambda { EmailFetcher.new(ADDRESS, "", CLIENT, KEY) }.must_raise(ArgumentError)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "requires a client" do
|
54
|
+
ENV['CED_CLIENT'] = nil
|
55
|
+
lambda { Email.new(ADDRESS, HOST, nil, KEY) }.must_raise(ArgumentError)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "requires a non empty client" do
|
59
|
+
ENV['CED_CLIENT'] = nil
|
60
|
+
lambda { EmailFetcher.new(ADDRESS, HOST, "", KEY) }.must_raise(ArgumentError)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "requires a key" do
|
64
|
+
ENV['CED_KEY'] = nil
|
65
|
+
lambda { Email.new(ADDRESS, HOST, CLIENT, nil) }.must_raise(ArgumentError)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "requires a non empty key" do
|
69
|
+
ENV['CED_KEY'] = nil
|
70
|
+
lambda { EmailFetcher.new(ADDRESS, HOST, CLIENT, "") }.must_raise(ArgumentError)
|
71
|
+
end
|
72
|
+
|
73
|
+
it "uses the environment variables if the host, client and key are missing" do
|
74
|
+
ENV['CED_HOST'] = HOST
|
75
|
+
ENV['CED_CLIENT'] = CLIENT
|
76
|
+
ENV['CED_KEY'] = KEY
|
77
|
+
lambda { Email.new(ADDRESS) }.must_be_silent
|
78
|
+
ENV['CED_HOST'] = nil
|
79
|
+
ENV['CED_CLIENT'] = nil
|
80
|
+
ENV['CED_KEY'] = nil
|
81
|
+
end
|
82
|
+
|
83
|
+
describe :verified? do
|
84
|
+
describe "when the response was successful" do
|
85
|
+
it "returns true" do
|
86
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
87
|
+
email.verified?.must_equal(true)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "when the response was not successful" do
|
92
|
+
before do
|
93
|
+
stub_error_email_verification_request(500)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "returns false" do
|
97
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
98
|
+
email.verified?.must_equal(false)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe "when the response timed out" do
|
103
|
+
before do
|
104
|
+
stub_error_email_verification_request(:timeout)
|
105
|
+
end
|
106
|
+
|
107
|
+
it "returns false" do
|
108
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
109
|
+
email.verified?.must_equal(false)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe "when the response is unauthorized" do
|
114
|
+
before do
|
115
|
+
stub_error_email_verification_request(401)
|
116
|
+
end
|
117
|
+
|
118
|
+
it "returns false" do
|
119
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
120
|
+
email.verified?.must_equal(false)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe :corrected_email do
|
126
|
+
describe "when the response was successful" do
|
127
|
+
it "returns the verified and corrected address from CED" do
|
128
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
129
|
+
email.corrected_email.must_equal("john.doe@example.com")
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
describe "when the response was not successful" do
|
134
|
+
before do
|
135
|
+
stub_error_email_verification_request(500)
|
136
|
+
end
|
137
|
+
|
138
|
+
it "returns an empty string" do
|
139
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
140
|
+
email.corrected_email.must_equal("")
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe :valid? do
|
146
|
+
describe "when the response was successful" do
|
147
|
+
describe "when the address is valid" do
|
148
|
+
it "returns true" do
|
149
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
150
|
+
email.valid?.must_equal(true)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
describe "when the address is not valid" do
|
155
|
+
before do
|
156
|
+
stub_email_verification_request(invalid_email_body)
|
157
|
+
end
|
158
|
+
|
159
|
+
it "returns false" do
|
160
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
161
|
+
email.valid?.must_equal(false)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
describe "when the response was not successful" do
|
167
|
+
before do
|
168
|
+
stub_error_email_verification_request(500)
|
169
|
+
end
|
170
|
+
|
171
|
+
it "returns false" do
|
172
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
173
|
+
email.valid?.must_equal(false)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
describe :seen_before? do
|
179
|
+
describe "when the response was successful" do
|
180
|
+
describe "when the address was already seen by CED" do
|
181
|
+
it "returns true" do
|
182
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
183
|
+
email.seen_before?.must_equal(true)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
describe "when the address was not yet seen by CED" do
|
188
|
+
before do
|
189
|
+
stub_email_verification_request(not_seen_before_email_body)
|
190
|
+
end
|
191
|
+
|
192
|
+
it "returns false" do
|
193
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
194
|
+
email.seen_before?.must_equal(false)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
describe "when the response was not successful" do
|
200
|
+
before do
|
201
|
+
stub_error_email_verification_request(500)
|
202
|
+
end
|
203
|
+
|
204
|
+
it "returns false" do
|
205
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
206
|
+
email.valid?.must_equal(false)
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
describe :error do
|
212
|
+
describe "when the response was successful" do
|
213
|
+
describe "when the address is valid" do
|
214
|
+
it "has no error" do
|
215
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
216
|
+
email.error.must_equal("")
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
describe "when the address is not valid" do
|
221
|
+
before do
|
222
|
+
stub_email_verification_request(invalid_email_body)
|
223
|
+
end
|
224
|
+
|
225
|
+
it "has a more detailed error" do
|
226
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
227
|
+
email.error.must_equal("Invalid domain name")
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
describe "when the response was not successful" do
|
233
|
+
before do
|
234
|
+
stub_error_email_verification_request(500)
|
235
|
+
end
|
236
|
+
|
237
|
+
it "returns an empty string" do
|
238
|
+
email = Email.new(ADDRESS, HOST, CLIENT, KEY)
|
239
|
+
email.error.must_equal("")
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'minitest/spec'
|
2
|
+
require 'minitest/mock'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'webmock/minitest'
|
5
|
+
require 'ced'
|
6
|
+
|
7
|
+
FIXTURES_PATH = File.expand_path("#{File.dirname(__FILE__)}/fixtures")
|
8
|
+
|
9
|
+
def valid_email_body
|
10
|
+
@valid_email_body ||= File.read(File.join(FIXTURES_PATH, 'valid_email.json'))
|
11
|
+
end
|
12
|
+
|
13
|
+
def invalid_email_body
|
14
|
+
@invalid_email_body ||= File.read(File.join(FIXTURES_PATH, 'invalid_email.json'))
|
15
|
+
end
|
16
|
+
|
17
|
+
def not_seen_before_email_body
|
18
|
+
@not_seen_before_email_body ||= File.read(File.join(FIXTURES_PATH, 'not_seen_before_email.json'))
|
19
|
+
end
|
20
|
+
|
21
|
+
def stub_email_verification_request(body)
|
22
|
+
request = { :headers => {'Accept' => '*/*', 'User-Agent' => 'Ruby'} }
|
23
|
+
response = { :status => 200, :body => body, :headers => {} }
|
24
|
+
stub_request(:get, email_verification_url).with(request).to_return(response)
|
25
|
+
end
|
26
|
+
|
27
|
+
def stub_error_email_verification_request(code)
|
28
|
+
status = []
|
29
|
+
verification_url = email_verification_url
|
30
|
+
case code
|
31
|
+
when 401
|
32
|
+
stub_request(:get, verification_url).to_return(:status => [401, "Unauthorized"])
|
33
|
+
when 500
|
34
|
+
stub_request(:get, verification_url).to_return(:status => [500, "Internal Server Error"])
|
35
|
+
when :timeout
|
36
|
+
stub_request(:get, verification_url).to_timeout
|
37
|
+
else
|
38
|
+
raise "Unknown response code: #{code}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def email_verification_url
|
43
|
+
"https://ced.example.com/api/v1/Email/validate?client=client&arguments=%5B%7B%22email%22%3A%22john.doe%40example.com%22%7D%5D&signature=cfbf8f9713f81a03027b4e977f5f74af050167cb"
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ced
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Simon Schoeters
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: webmock
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.9.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.9.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 10.0.3
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 10.0.3
|
46
|
+
description: Ruby interface for CED's email verification service.
|
47
|
+
email:
|
48
|
+
- hamfilter@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- ced.gemspec
|
59
|
+
- lib/ced.rb
|
60
|
+
- lib/ced/email.rb
|
61
|
+
- lib/ced/email_fetcher.rb
|
62
|
+
- lib/ced/email_response.rb
|
63
|
+
- lib/ced/version.rb
|
64
|
+
- test/email_fetcher_test.rb
|
65
|
+
- test/email_response_test.rb
|
66
|
+
- test/email_test.rb
|
67
|
+
- test/fixtures/invalid_email.json
|
68
|
+
- test/fixtures/not_seen_before_email.json
|
69
|
+
- test/fixtures/valid_email.json
|
70
|
+
- test/test_helper.rb
|
71
|
+
homepage: https://github.com/cimm/ced
|
72
|
+
licenses: []
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
hash: 1794799080809433788
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
hash: 1794799080809433788
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 1.8.24
|
98
|
+
signing_key:
|
99
|
+
specification_version: 3
|
100
|
+
summary: The Central Email Database is an email verification service. You pass it
|
101
|
+
an email address and it tells you if the email address is real or not. This gem
|
102
|
+
wraps the API in a more Ruby friendly syntax. CED also corrects common typos in
|
103
|
+
email domains.
|
104
|
+
test_files:
|
105
|
+
- test/email_fetcher_test.rb
|
106
|
+
- test/email_response_test.rb
|
107
|
+
- test/email_test.rb
|
108
|
+
- test/fixtures/invalid_email.json
|
109
|
+
- test/fixtures/not_seen_before_email.json
|
110
|
+
- test/fixtures/valid_email.json
|
111
|
+
- test/test_helper.rb
|