verifalia 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/README.md +6 -1
- data/lib/rest/account_balance.rb +93 -0
- data/lib/rest/client.rb +9 -1
- data/lib/rest/email_validations.rb +71 -19
- data/lib/verifalia/util/configuration.rb +2 -2
- data/lib/verifalia/version.rb +1 -1
- data/spec/rest/account_balance_spec.rb +93 -0
- data/spec/rest/client_spec.rb +23 -6
- data/spec/rest/email_validations_spec.rb +181 -122
- data/verifalia.gemspec +1 -1
- metadata +9 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13ecdf0dda8a8b5eed02d2fbc681f57f46905e4e
|
4
|
+
data.tar.gz: 45a067871869dacd52fce6649d6ecf6f244a71e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc7beb23842a872e087cfc3e44af72cef66e67fbc4f36a303610fe39185cc13afbc0bad27370fb65fdb5abaf3e274b18d4b2c7364615f2c33c0f6b5fdd32d633
|
7
|
+
data.tar.gz: 7676bc471f56549a75b4e7303341500c69b86f091567b0edfa7f7a6dcca4ab0443e5aba2ac5273c3a6c2bd60ab72d5433ddc2ece045aa5e8ea50cf3b2ba1a6bf
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -92,12 +92,15 @@ options = { #view verifalia API documentation
|
|
92
92
|
if (unique_id = @client.email_validations.verify(emails, options))
|
93
93
|
#response is an hash with all the values returned
|
94
94
|
response = @client.email_validations.query
|
95
|
-
@client.email_validations.destroy
|
95
|
+
@client.email_validations.destroy #destroy the job on the Verifalia server
|
96
96
|
else
|
97
97
|
#error is HTTP status code in symbol (:bad_request)
|
98
98
|
error = @client.email_validations.error
|
99
99
|
end
|
100
100
|
|
101
|
+
#you can wait for job completion using options on query
|
102
|
+
response = @client.email_validations.query(wait_for_completion: true, completion_max_retry: 2, completion_interval: 1)
|
103
|
+
|
101
104
|
|
102
105
|
##with previous unique id
|
103
106
|
unique_id = "example-example"
|
@@ -113,4 +116,6 @@ if @client.email_validations(unique_id: unique_id).completed?
|
|
113
116
|
response = @client.email_validations(unique_id: unique_id).query
|
114
117
|
end
|
115
118
|
|
119
|
+
#checking account_balance. Remember to enable this feature in Verifalia dashboard or you get a :forbidden error
|
120
|
+
@client.account_balance.balance
|
116
121
|
```
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'rest_client'
|
2
|
+
require 'json'
|
3
|
+
module Verifalia
|
4
|
+
module REST
|
5
|
+
class AccountBalance
|
6
|
+
##
|
7
|
+
# The Verifalia::REST::AccountBalance class allow you to comminucate
|
8
|
+
# with Account balance Api. You don't need to instantiate this class, but
|
9
|
+
# use the client for autoconfiguration. # The +args+ parameter is a hash of configuration
|
10
|
+
def initialize(config, account_sid, account_token, args = {})
|
11
|
+
@resources = build_resources(config, account_sid, account_token)
|
12
|
+
end
|
13
|
+
|
14
|
+
##
|
15
|
+
# Query the Account balance
|
16
|
+
#
|
17
|
+
def balance()
|
18
|
+
begin
|
19
|
+
response = multiplex_request do |resource|
|
20
|
+
resource[@unique_id].get
|
21
|
+
end
|
22
|
+
@error = nil
|
23
|
+
JSON.parse(response)
|
24
|
+
rescue => e
|
25
|
+
compute_error(e)
|
26
|
+
false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def error
|
31
|
+
@error
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def multiplex_request
|
37
|
+
@resources.shuffle.each do |resource|
|
38
|
+
begin
|
39
|
+
response = yield(resource)
|
40
|
+
return response
|
41
|
+
rescue => e
|
42
|
+
if ((e.is_a? RestClient::Exception) && (e.http_code != 500))
|
43
|
+
raise e
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
raise RestClient::Exception.new(nil, 500)
|
48
|
+
end
|
49
|
+
|
50
|
+
def compute_error(e)
|
51
|
+
unless e.is_a? RestClient::Exception
|
52
|
+
@error = :internal_server_error
|
53
|
+
end
|
54
|
+
|
55
|
+
case e.http_code
|
56
|
+
when 400
|
57
|
+
@error = :bad_request
|
58
|
+
when 401
|
59
|
+
@error = :unauthorized
|
60
|
+
when 402
|
61
|
+
@error = :payment_required
|
62
|
+
when 403
|
63
|
+
@error = :forbidden
|
64
|
+
when 404
|
65
|
+
@error = :not_found
|
66
|
+
when 406
|
67
|
+
@error = :not_acceptable
|
68
|
+
when 410
|
69
|
+
@error = :gone
|
70
|
+
when 429
|
71
|
+
@error = :too_many_request
|
72
|
+
else
|
73
|
+
@error = :internal_server_error
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def build_resources(config, account_sid, account_token)
|
78
|
+
opts = {
|
79
|
+
user: account_sid,
|
80
|
+
password: account_token,
|
81
|
+
headers: {
|
82
|
+
content_type: :json,
|
83
|
+
user_agent: "verifalia-rest-client/ruby/#{Verifalia::VERSION}"
|
84
|
+
}
|
85
|
+
}
|
86
|
+
config[:hosts].map do |host|
|
87
|
+
api_url = "#{host}/#{config[:api_version]}/account-balance"
|
88
|
+
RestClient::Resource.new api_url, opts
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
data/lib/rest/client.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'rest/email_validations'
|
2
|
+
require 'rest/account_balance'
|
2
3
|
|
3
4
|
module Verifalia
|
4
5
|
module REST
|
@@ -25,7 +26,7 @@ module Verifalia
|
|
25
26
|
API_VERSION = 'v1.4'
|
26
27
|
|
27
28
|
DEFAULTS = {
|
28
|
-
|
29
|
+
hosts: ['https://api-1.verifalia.com', 'https://api-2.verifalia.com'],
|
29
30
|
api_version: 'v1.4'
|
30
31
|
}
|
31
32
|
|
@@ -70,6 +71,13 @@ module Verifalia
|
|
70
71
|
end
|
71
72
|
end
|
72
73
|
|
74
|
+
##
|
75
|
+
# Instantiate a new HTTP client to talk to Verifalia Account Balance Api.
|
76
|
+
#
|
77
|
+
def account_balance()
|
78
|
+
@account_balance ||= AccountBalance.new @config, @account_sid, @auth_token
|
79
|
+
end
|
80
|
+
|
73
81
|
end
|
74
82
|
end
|
75
83
|
end
|
@@ -3,6 +3,9 @@ require 'json'
|
|
3
3
|
module Verifalia
|
4
4
|
module REST
|
5
5
|
class EmailValidations
|
6
|
+
|
7
|
+
COMPLETION_MAX_RETRY = 5
|
8
|
+
COMPLETION_INTERVAL = 1
|
6
9
|
##
|
7
10
|
# The Verifalia::REST::EmailValidations class allow you to comminucate
|
8
11
|
# with Email Validations Api. You don't need to instantiate this class, but
|
@@ -14,14 +17,16 @@ module Verifalia
|
|
14
17
|
# The unique if of the Verifalia Email Validation resource
|
15
18
|
#
|
16
19
|
def initialize(config, account_sid, account_token, args = {})
|
17
|
-
@
|
20
|
+
@resources = build_resources(config, account_sid, account_token)
|
18
21
|
@unique_id = args[:unique_id] if args[:unique_id]
|
19
22
|
end
|
20
23
|
|
21
24
|
##
|
22
25
|
# Query the Email Validations Api with:
|
23
26
|
#
|
24
|
-
# === <tt>
|
27
|
+
# === <tt> inputs: ['test@test.com']
|
28
|
+
# === <tt> inputs: data = [{ inputData: 'first@first.it' }, { inputData: 'first@first.it' } ]
|
29
|
+
# === <tt> options: { quality: 'high', priority: 100, deduplication: 'safe' } view verifalia API documentation
|
25
30
|
#
|
26
31
|
# An array of emails to validate
|
27
32
|
#
|
@@ -40,11 +45,12 @@ module Verifalia
|
|
40
45
|
end
|
41
46
|
content = ({ entries: data }.merge(options)).to_json
|
42
47
|
begin
|
43
|
-
|
44
|
-
|
45
|
-
|
48
|
+
@response = multiplex_request do |resource|
|
49
|
+
resource.post content
|
50
|
+
end
|
51
|
+
@query_result = JSON.parse(@response)
|
52
|
+
@unique_id = @query_result["uniqueID"]
|
46
53
|
@error = nil
|
47
|
-
@query_result = nil
|
48
54
|
@unique_id
|
49
55
|
rescue => e
|
50
56
|
compute_error(e)
|
@@ -57,19 +63,39 @@ module Verifalia
|
|
57
63
|
# this method you need to supply unique_id uring initialization or call verify first. If request fail,
|
58
64
|
# you can call <tt>error</tt> to receive detailed information
|
59
65
|
#
|
60
|
-
|
66
|
+
# === <tt> options: { wait_for_completion: true, completion_max_retry: 5, completion_interval: 1(seconds) }
|
67
|
+
def query(options = {})
|
61
68
|
raise ArgumentError, 'You must call verify first or supply and uniqueId' unless @unique_id
|
62
|
-
|
69
|
+
opts = {
|
70
|
+
wait_for_completion: false,
|
71
|
+
completion_max_retry: COMPLETION_MAX_RETRY,
|
72
|
+
completion_interval: COMPLETION_INTERVAL,
|
73
|
+
}
|
74
|
+
.merge! options
|
75
|
+
if @query_result == nil || !completed?
|
63
76
|
begin
|
64
|
-
|
65
|
-
|
66
|
-
|
77
|
+
loop_count = 0
|
78
|
+
loop do
|
79
|
+
@response = multiplex_request do |resource|
|
80
|
+
resource[@unique_id].get
|
81
|
+
end
|
82
|
+
@query_result = JSON.parse(@response)
|
83
|
+
@error = nil
|
84
|
+
loop_count += 1
|
85
|
+
sleep opts[:completion_interval] if opts[:wait_for_completion]
|
86
|
+
break if !opts[:wait_for_completion] || (completed? || loop_count >= opts[:completion_max_retry])
|
87
|
+
end
|
67
88
|
rescue => e
|
68
89
|
compute_error(e)
|
69
90
|
return false
|
70
91
|
end
|
71
92
|
end
|
72
|
-
|
93
|
+
if (opts[:wait_for_completion] && !completed?)
|
94
|
+
@error = :not_completed
|
95
|
+
false
|
96
|
+
else
|
97
|
+
@query_result
|
98
|
+
end
|
73
99
|
end
|
74
100
|
|
75
101
|
##
|
@@ -80,13 +106,16 @@ module Verifalia
|
|
80
106
|
def destroy
|
81
107
|
raise ArgumentError, 'You must call verify first or supply and uniqueId' unless @unique_id
|
82
108
|
begin
|
83
|
-
r =
|
109
|
+
r = multiplex_request do |resource|
|
110
|
+
resource[@unique_id].delete
|
111
|
+
end
|
84
112
|
@error = nil
|
85
113
|
@response = nil
|
86
114
|
@query_result = nil
|
87
115
|
@unique_id = nil
|
88
116
|
true
|
89
117
|
rescue => e
|
118
|
+
return true if (e.is_a? RestClient::Exception && e.http_code == 410)
|
90
119
|
compute_error(e)
|
91
120
|
return false
|
92
121
|
end
|
@@ -97,8 +126,7 @@ module Verifalia
|
|
97
126
|
# this method you need to supply unique_id during initialization or call verify first.
|
98
127
|
#
|
99
128
|
def completed?
|
100
|
-
|
101
|
-
@response.code == 200 && query_progress["noOfTotalEntries"] == query_progress["noOfCompletedEntries"]
|
129
|
+
@response.code == 200
|
102
130
|
end
|
103
131
|
|
104
132
|
def error
|
@@ -106,6 +134,21 @@ module Verifalia
|
|
106
134
|
end
|
107
135
|
|
108
136
|
private
|
137
|
+
|
138
|
+
def multiplex_request
|
139
|
+
@resources.shuffle.each do |resource|
|
140
|
+
begin
|
141
|
+
response = yield(resource)
|
142
|
+
return response
|
143
|
+
rescue => e
|
144
|
+
if ((e.is_a? RestClient::Exception) && (e.http_code != 500))
|
145
|
+
raise e
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
raise RestClient::Exception.new(nil, 500)
|
150
|
+
end
|
151
|
+
|
109
152
|
def compute_error(e)
|
110
153
|
unless e.is_a? RestClient::Exception
|
111
154
|
@error = :internal_server_error
|
@@ -118,25 +161,34 @@ module Verifalia
|
|
118
161
|
@error = :unauthorized
|
119
162
|
when 402
|
120
163
|
@error = :payment_required
|
164
|
+
when 403
|
165
|
+
@error = :forbidden
|
121
166
|
when 404
|
122
167
|
@error = :not_found
|
123
168
|
when 406
|
124
169
|
@error = :not_acceptable
|
125
170
|
when 410
|
126
171
|
@error = :gone
|
172
|
+
when 429
|
173
|
+
@error = :too_many_request
|
127
174
|
else
|
128
175
|
@error = :internal_server_error
|
129
176
|
end
|
130
177
|
end
|
131
178
|
|
132
|
-
def
|
133
|
-
api_url = "#{config[:host]}/#{config[:api_version]}/email-validations"
|
179
|
+
def build_resources(config, account_sid, account_token)
|
134
180
|
opts = {
|
135
181
|
user: account_sid,
|
136
182
|
password: account_token,
|
137
|
-
headers: {
|
183
|
+
headers: {
|
184
|
+
content_type: :json,
|
185
|
+
user_agent: "verifalia-rest-client/ruby/#{Verifalia::VERSION}"
|
186
|
+
}
|
138
187
|
}
|
139
|
-
|
188
|
+
config[:hosts].map do |host|
|
189
|
+
api_url = "#{host}/#{config[:api_version]}/email-validations"
|
190
|
+
RestClient::Resource.new api_url, opts
|
191
|
+
end
|
140
192
|
end
|
141
193
|
end
|
142
194
|
end
|
data/lib/verifalia/version.rb
CHANGED
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Verifalia::REST::AccountBalance do
|
4
|
+
let(:config) { { hosts: ["https://api.fake.com", "https://api-2.fake.com"], api_version: "v" } }
|
5
|
+
|
6
|
+
describe '#initialize' do
|
7
|
+
|
8
|
+
it 'create RestClient::Resource with correct parameters' do
|
9
|
+
opts = {
|
10
|
+
user: 'someSid',
|
11
|
+
password: 'someToken',
|
12
|
+
headers: { content_type: :json, user_agent: "verifalia-rest-client/ruby/#{Verifalia::VERSION}" }
|
13
|
+
}
|
14
|
+
config[:hosts].each do |host|
|
15
|
+
api_url = "#{host}/#{config[:api_version]}/account-balance"
|
16
|
+
expect(RestClient::Resource).to receive(:new).with(api_url, opts)
|
17
|
+
end
|
18
|
+
Verifalia::REST::AccountBalance.new(config, 'someSid', 'someToken')
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'associate RestClient::Resource to @resources' do
|
22
|
+
resource = double()
|
23
|
+
allow(RestClient::Resource).to receive(:new).and_return(resource)
|
24
|
+
account_balance = Verifalia::REST::AccountBalance.new(config, 'someSid', 'someToken')
|
25
|
+
expect(account_balance.instance_variable_get('@resources')).to include(resource)
|
26
|
+
expect(account_balance.instance_variable_get('@resources').size).to eq(config[:hosts].size)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'initialized' do
|
31
|
+
let(:resources) { [ double().as_null_object, double().as_null_object ] }
|
32
|
+
let(:response) { double().as_null_object }
|
33
|
+
let(:response_json) { double().as_null_object }
|
34
|
+
|
35
|
+
before(:each) do
|
36
|
+
@account_balance = Verifalia::REST::AccountBalance.new(config, 'someSid', 'someToken')
|
37
|
+
@account_balance.instance_variable_set('@resources', resources)
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#balance' do
|
41
|
+
|
42
|
+
context 'without errors' do
|
43
|
+
it 'call #get on @resources' do
|
44
|
+
resources.each { |resource| allow(resource).to receive(:get).and_return(response) }
|
45
|
+
expect(JSON).to receive(:parse).with(response).and_return(response_json)
|
46
|
+
@account_balance.balance
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'return parsed json' do
|
50
|
+
parsed = double()
|
51
|
+
resources.each { |resource| allow(resource).to receive(:get).and_return(response) }
|
52
|
+
expect(JSON).to receive(:parse).and_return(parsed)
|
53
|
+
result = @account_balance.balance
|
54
|
+
expect(result).to eq(parsed)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'request failed' do
|
59
|
+
|
60
|
+
before(:each) do
|
61
|
+
resources.each { |resource| allow(resource).to receive(:get).and_raise(RestClient::Exception.new(nil, 402))}
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'raise exception, call #compute_error and return false' do
|
65
|
+
result = @account_balance.balance
|
66
|
+
expect(result).to eq(false)
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'raise exception, call #compute_error and return correct error' do
|
70
|
+
result = @account_balance.balance
|
71
|
+
expect(result).to eq(false)
|
72
|
+
expect(@account_balance.error).to eq(:payment_required)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'with one request failed with 500' do
|
77
|
+
before(:each) do
|
78
|
+
expect(resources).to receive(:shuffle).and_return([resources[0], resources[1]])
|
79
|
+
expect(resources[0]).to receive(:get).and_raise(RestClient::Exception.new(nil, 500))
|
80
|
+
expect(resources[1]).to receive(:get).and_return(response)
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'return parsed json' do
|
84
|
+
parsed = double()
|
85
|
+
expect(JSON).to receive(:parse).and_return(parsed)
|
86
|
+
result = @account_balance.balance
|
87
|
+
expect(result).to eq(parsed)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
data/spec/rest/client_spec.rb
CHANGED
@@ -45,10 +45,10 @@ describe Verifalia::REST::Client do
|
|
45
45
|
config.auth_token = 'someToken'
|
46
46
|
end
|
47
47
|
|
48
|
-
client = Verifalia::REST::Client.new :
|
48
|
+
client = Verifalia::REST::Client.new :hosts => ['api.fake.com']
|
49
49
|
|
50
50
|
config = client.instance_variable_get('@config')
|
51
|
-
expect(config[:
|
51
|
+
expect(config[:hosts]).to eq(['api.fake.com'])
|
52
52
|
end
|
53
53
|
|
54
54
|
it 'should throw an argument error if the sid and token isn\'t set' do
|
@@ -59,7 +59,7 @@ describe Verifalia::REST::Client do
|
|
59
59
|
expect { Verifalia::REST::Client.new 'someSid' }.to raise_error(ArgumentError)
|
60
60
|
end
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
describe '#email_validations' do
|
64
64
|
before(:each) do
|
65
65
|
Verifalia.configure do |config|
|
@@ -68,7 +68,7 @@ describe Verifalia::REST::Client do
|
|
68
68
|
end
|
69
69
|
@client = Verifalia::REST::Client.new
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
72
|
context 'without parameters' do
|
73
73
|
it 'call #new on Verifalia::REST::EmailValidations with correct paramenters' do
|
74
74
|
config = @client.instance_variable_get('@config')
|
@@ -76,7 +76,7 @@ describe Verifalia::REST::Client do
|
|
76
76
|
@client.email_validations
|
77
77
|
end
|
78
78
|
end
|
79
|
-
|
79
|
+
|
80
80
|
context 'with parameter' do
|
81
81
|
it 'call #new on Verifalia::REST::EmailValidations with correct paramenters' do
|
82
82
|
config = @client.instance_variable_get('@config')
|
@@ -85,4 +85,21 @@ describe Verifalia::REST::Client do
|
|
85
85
|
end
|
86
86
|
end
|
87
87
|
end
|
88
|
-
|
88
|
+
|
89
|
+
|
90
|
+
describe '#account_balance' do
|
91
|
+
before(:each) do
|
92
|
+
Verifalia.configure do |config|
|
93
|
+
config.account_sid = 'someSid'
|
94
|
+
config.auth_token = 'someToken'
|
95
|
+
end
|
96
|
+
@client = Verifalia::REST::Client.new
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'call #new on Verifalia::REST::AccountBalance with correct paramenters' do
|
100
|
+
config = @client.instance_variable_get('@config')
|
101
|
+
expect(Verifalia::REST::AccountBalance).to receive(:new).with(config, 'someSid', 'someToken')
|
102
|
+
@client.account_balance
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -1,27 +1,29 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Verifalia::REST::EmailValidations do
|
4
|
-
let(:config) { {
|
4
|
+
let(:config) { { hosts: ["https://api.fake.com", "https://api-2.fake.com"], api_version: "v" } }
|
5
5
|
|
6
6
|
describe '#initialize' do
|
7
7
|
|
8
8
|
it 'create RestClient::Resource with correct parameters' do
|
9
|
-
api_url = "#{config[:host]}/#{config[:api_version]}/email-validations"
|
10
9
|
opts = {
|
11
10
|
user: 'someSid',
|
12
11
|
password: 'someToken',
|
13
|
-
headers: { content_type: :json }
|
12
|
+
headers: { content_type: :json, user_agent: "verifalia-rest-client/ruby/#{Verifalia::VERSION}" }
|
14
13
|
}
|
15
|
-
|
16
|
-
|
14
|
+
config[:hosts].each do |host|
|
15
|
+
api_url = "#{host}/#{config[:api_version]}/email-validations"
|
16
|
+
expect(RestClient::Resource).to receive(:new).with(api_url, opts)
|
17
|
+
end
|
17
18
|
Verifalia::REST::EmailValidations.new(config, 'someSid', 'someToken')
|
18
19
|
end
|
19
20
|
|
20
|
-
it 'associate RestClient::Resource to @
|
21
|
+
it 'associate RestClient::Resource to @resources' do
|
21
22
|
resource = double()
|
22
23
|
allow(RestClient::Resource).to receive(:new).and_return(resource)
|
23
24
|
email_validations = Verifalia::REST::EmailValidations.new(config, 'someSid', 'someToken')
|
24
|
-
expect(email_validations.instance_variable_get('@
|
25
|
+
expect(email_validations.instance_variable_get('@resources')).to include(resource)
|
26
|
+
expect(email_validations.instance_variable_get('@resources').size).to eq(config[:hosts].size)
|
25
27
|
end
|
26
28
|
|
27
29
|
it 'associate :unique_id to @unique_id' do
|
@@ -33,12 +35,12 @@ describe Verifalia::REST::EmailValidations do
|
|
33
35
|
end
|
34
36
|
|
35
37
|
context 'initialized' do
|
36
|
-
let(:
|
38
|
+
let(:resources) { [ double().as_null_object, double().as_null_object ] }
|
37
39
|
let(:response) { double().as_null_object }
|
38
40
|
let(:response_json) { double().as_null_object }
|
39
41
|
before(:each) do
|
40
42
|
@email_validations = Verifalia::REST::EmailValidations.new(config, 'someSid', 'someToken')
|
41
|
-
@email_validations.instance_variable_set('@
|
43
|
+
@email_validations.instance_variable_set('@resources', resources)
|
42
44
|
end
|
43
45
|
|
44
46
|
describe '#verify' do
|
@@ -69,81 +71,99 @@ describe Verifalia::REST::EmailValidations do
|
|
69
71
|
expect{ @email_validations.verify(inputs) }.to raise_error(ArgumentError)
|
70
72
|
end
|
71
73
|
|
72
|
-
|
73
|
-
emails = ['first', 'second']
|
74
|
-
data = emails.map { |email| { inputData: email }}
|
75
|
-
content = { entries: data }.to_json
|
76
|
-
expect(resource).to receive(:post).with(content).and_return(response)
|
77
|
-
expect(JSON).to receive(:parse).with(response).and_return(response_json)
|
78
|
-
@email_validations.verify(emails)
|
79
|
-
end
|
74
|
+
context 'without errors' do
|
80
75
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
end
|
76
|
+
it 'call #post on resource with string array parameters' do
|
77
|
+
emails = ['first', 'second']
|
78
|
+
data = emails.map { |email| { inputData: email }}
|
79
|
+
content = { entries: data }.to_json
|
80
|
+
resources.each { |resource| allow(resource).to receive(:post).with(content).and_return(response) }
|
81
|
+
expect(JSON).to receive(:parse).with(response).and_return(response_json)
|
82
|
+
@email_validations.verify(emails)
|
83
|
+
end
|
90
84
|
|
91
|
-
|
92
|
-
|
93
|
-
{
|
94
|
-
|
95
|
-
}
|
96
|
-
{
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
content = { entries: data }.to_json
|
101
|
-
expect(resource).to receive(:post).with(content).and_return(response)
|
102
|
-
expect(JSON).to receive(:parse).with(response).and_return(response_json)
|
103
|
-
@email_validations.verify(data)
|
104
|
-
end
|
85
|
+
it 'call #post on resource with string array parameters and options' do
|
86
|
+
emails = ['first', 'second']
|
87
|
+
options = { option_1: 'test' }
|
88
|
+
data = emails.map { |email| { inputData: email }}
|
89
|
+
content = { entries: data, option_1: 'test' }.to_json
|
90
|
+
resources.each { |resource| allow(resource).to receive(:post).with(content).and_return(response) }
|
91
|
+
expect(JSON).to receive(:parse).with(response).and_return(response_json)
|
92
|
+
@email_validations.verify(emails, options)
|
93
|
+
end
|
105
94
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
95
|
+
it 'call #post on @resources with hash array parameters with valid value' do
|
96
|
+
data = [
|
97
|
+
{
|
98
|
+
inputData: 'first'
|
99
|
+
},
|
100
|
+
{
|
101
|
+
inputData: 'second'
|
102
|
+
}
|
103
|
+
]
|
104
|
+
content = { entries: data }.to_json
|
105
|
+
resources.each { |resource| allow(resource).to receive(:post).with(content).and_return(response) }
|
106
|
+
expect(JSON).to receive(:parse).with(response).and_return(response_json)
|
107
|
+
@email_validations.verify(data)
|
108
|
+
end
|
117
109
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
110
|
+
it 'associate @unique_id' do
|
111
|
+
emails = ['first', 'second']
|
112
|
+
unique_id = 'fake'
|
113
|
+
parsed = double()
|
114
|
+
expect(JSON).to receive(:parse).and_return(parsed)
|
115
|
+
expect(parsed).to receive(:[]).with("uniqueID").and_return(unique_id)
|
116
|
+
@email_validations.verify(emails)
|
117
|
+
expect(@email_validations.instance_variable_get('@unique_id')).to eq(unique_id)
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'return @unique_id' do
|
121
|
+
emails = ['first', 'second']
|
122
|
+
unique_id = 'fake'
|
123
|
+
parsed = double()
|
124
|
+
expect(JSON).to receive(:parse).and_return(parsed)
|
125
|
+
expect(parsed).to receive(:[]).with("uniqueID").and_return(unique_id)
|
126
|
+
result = @email_validations.verify(emails)
|
127
|
+
expect(result).to eq(unique_id)
|
128
|
+
end
|
126
129
|
end
|
127
130
|
|
128
|
-
context '
|
131
|
+
context 'requests failed' do
|
132
|
+
|
133
|
+
before(:each) do
|
134
|
+
resources.each { |resource| allow(resource).to receive(:post).and_raise(RestClient::Exception.new(nil, 402))}
|
135
|
+
end
|
129
136
|
|
130
137
|
it 'raise exception, call #compute_error and return false' do
|
131
138
|
emails = ['first', 'second']
|
132
|
-
expect(resource).to receive(:post).and_raise(RestClient::Exception)
|
133
139
|
result = @email_validations.verify(emails)
|
134
140
|
expect(result).to eq(false)
|
135
|
-
expect(@email_validations.error).to eq(:internal_server_error)
|
136
141
|
end
|
137
142
|
|
138
143
|
it 'raise exception, call #compute_error and return correct error' do
|
139
144
|
emails = ['first', 'second']
|
140
|
-
exception = RestClient::Exception.new(nil, 402)
|
141
|
-
expect(resource).to receive(:post).and_raise(exception)
|
142
145
|
result = @email_validations.verify(emails)
|
143
146
|
expect(result).to eq(false)
|
144
147
|
expect(@email_validations.error).to eq(:payment_required)
|
145
148
|
end
|
146
149
|
end
|
150
|
+
|
151
|
+
context 'with one request failed with 500' do
|
152
|
+
before(:each) do
|
153
|
+
expect(resources).to receive(:shuffle).and_return([resources[0], resources[1]])
|
154
|
+
expect(resources[0]).to receive(:post).and_raise(RestClient::Exception.new(nil, 500))
|
155
|
+
expect(resources[1]).to receive(:post).and_return(response)
|
156
|
+
end
|
157
|
+
|
158
|
+
it 'should return @unique_id' do
|
159
|
+
emails = ['first', 'second']
|
160
|
+
unique_id = 'fake'
|
161
|
+
expect(JSON).to receive(:parse).with(response).and_return(response_json)
|
162
|
+
expect(response_json).to receive(:[]).with("uniqueID").and_return(unique_id)
|
163
|
+
result = @email_validations.verify(emails)
|
164
|
+
expect(result).to eq(unique_id)
|
165
|
+
end
|
166
|
+
end
|
147
167
|
end
|
148
168
|
|
149
169
|
describe '#query' do
|
@@ -156,34 +176,114 @@ describe Verifalia::REST::EmailValidations do
|
|
156
176
|
@email_validations.instance_variable_set('@unique_id', 'fake')
|
157
177
|
end
|
158
178
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
179
|
+
context 'without errors' do
|
180
|
+
it 'call #get on @resource[@uniqueId] with correct parameters' do
|
181
|
+
request = double()
|
182
|
+
resources.each do |resource|
|
183
|
+
allow(resource).to receive(:[]).with('fake').and_return(request)
|
184
|
+
allow(request).to receive(:get).and_return(double().as_null_object)
|
185
|
+
end
|
186
|
+
expect(JSON).to receive(:parse)
|
187
|
+
@email_validations.query
|
188
|
+
end
|
166
189
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
190
|
+
it 'should return parsed json' do
|
191
|
+
request = double()
|
192
|
+
parsed = double()
|
193
|
+
resources.each do |resource|
|
194
|
+
allow(resource).to receive(:[]).with('fake').and_return(request)
|
195
|
+
allow(request).to receive(:get).and_return(double().as_null_object)
|
196
|
+
end
|
197
|
+
expect(JSON).to receive(:parse).and_return(parsed)
|
198
|
+
result = @email_validations.query
|
199
|
+
expect(result).to eq(parsed)
|
200
|
+
end
|
201
|
+
|
202
|
+
context 'with completion' do
|
203
|
+
it 'call #get on @resource[@uniqueId] multiple time' do
|
204
|
+
request = double()
|
205
|
+
resources.each do |resource|
|
206
|
+
allow(resource).to receive(:[]).with('fake').and_return(request)
|
207
|
+
allow(request).to receive(:get).and_return(double().as_null_object)
|
208
|
+
end
|
209
|
+
allow(JSON).to receive(:parse)
|
210
|
+
@email_validations.query(wait_for_completion: true, completion_max_retry: 2)
|
211
|
+
end
|
212
|
+
end
|
173
213
|
end
|
174
214
|
|
175
215
|
context 'request failed' do
|
176
216
|
|
177
|
-
|
217
|
+
before(:each) do
|
178
218
|
request = double()
|
179
|
-
|
180
|
-
|
219
|
+
resources.each do |resource|
|
220
|
+
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
it 'raise exception, call #compute_error and return false' do
|
181
225
|
expect(@email_validations).to receive(:compute_error).and_return(double())
|
182
226
|
result = @email_validations.query
|
183
227
|
expect(result).to eq(false)
|
184
228
|
end
|
185
229
|
end
|
186
230
|
|
231
|
+
context 'with one request failed with 500' do
|
232
|
+
before(:each) do
|
233
|
+
request_1 = double()
|
234
|
+
request_2 = double()
|
235
|
+
response = double()
|
236
|
+
expect(resources).to receive(:shuffle).and_return([resources[0], resources[1]])
|
237
|
+
expect(resources[0]).to receive(:[]).with('fake').and_return(request_1)
|
238
|
+
expect(request_1).to receive(:get).and_raise(RestClient::Exception.new(nil, 500))
|
239
|
+
expect(resources[1]).to receive(:[]).with('fake').and_return(request_2)
|
240
|
+
allow(request_2).to receive(:get).and_return(double().as_null_object)
|
241
|
+
end
|
242
|
+
|
243
|
+
it 'should return parsed json' do
|
244
|
+
parsed = double()
|
245
|
+
expect(JSON).to receive(:parse).and_return(parsed)
|
246
|
+
result = @email_validations.query
|
247
|
+
expect(result).to eq(parsed)
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
describe '#destroy' do
|
254
|
+
it 'raise ArgumentError without @unique_id' do
|
255
|
+
expect{ @email_validations.destroy }.to raise_error(ArgumentError)
|
256
|
+
end
|
257
|
+
|
258
|
+
context 'with @unique_id' do
|
259
|
+
before(:each) do
|
260
|
+
@email_validations.instance_variable_set('@unique_id', 'fake')
|
261
|
+
end
|
262
|
+
|
263
|
+
context 'without errors' do
|
264
|
+
|
265
|
+
it 'call #delete on @resource[@uniqueId]' do
|
266
|
+
request = double()
|
267
|
+
resources.each do |resource|
|
268
|
+
allow(resource).to receive(:[]).with('fake').and_return(request)
|
269
|
+
allow(request).to receive(:delete).and_return(double().as_null_object)
|
270
|
+
end
|
271
|
+
@email_validations.destroy
|
272
|
+
end
|
273
|
+
|
274
|
+
it 'clear @response, @unique_id and @error' do
|
275
|
+
request = double()
|
276
|
+
resources.each do |resource|
|
277
|
+
allow(resource).to receive(:[]).with('fake').and_return(request)
|
278
|
+
allow(request).to receive(:delete).and_return(double().as_null_object)
|
279
|
+
end
|
280
|
+
@email_validations.destroy
|
281
|
+
expect(@email_validations.instance_variable_get('@response')).to eq(nil)
|
282
|
+
expect(@email_validations.instance_variable_get('@unique_id')).to eq(nil)
|
283
|
+
expect(@email_validations.instance_variable_get('@error')).to eq(nil)
|
284
|
+
end
|
285
|
+
|
286
|
+
end
|
187
287
|
end
|
188
288
|
end
|
189
289
|
|
@@ -198,7 +298,6 @@ describe Verifalia::REST::EmailValidations do
|
|
198
298
|
context 'with 202 http code response' do
|
199
299
|
before(:each) do
|
200
300
|
allow(response).to receive(:code).and_return(202)
|
201
|
-
allow(@email_validations).to receive(:query).and_return({ "progress" => nil })
|
202
301
|
end
|
203
302
|
|
204
303
|
it 'should return false' do
|
@@ -207,57 +306,17 @@ describe Verifalia::REST::EmailValidations do
|
|
207
306
|
end
|
208
307
|
|
209
308
|
context 'with 200 http code response' do
|
210
|
-
let(:completed_query) do
|
211
|
-
{ "progress"=> { "noOfTotalEntries" => 1, "noOfCompletedEntries" => 1 } }
|
212
|
-
end
|
213
|
-
let(:incompleted_query) do
|
214
|
-
{ "progress"=> { "noOfTotalEntries" => 0, "noOfCompletedEntries" => 1 } }
|
215
|
-
end
|
216
309
|
|
217
310
|
before(:each) do
|
218
311
|
allow(response).to receive(:code).and_return(200)
|
219
312
|
end
|
220
313
|
|
221
|
-
it 'should return true
|
222
|
-
allow(@email_validations).to receive(:query).and_return(completed_query)
|
314
|
+
it 'should return true' do
|
223
315
|
expect(@email_validations.completed?).to be true
|
224
316
|
end
|
225
|
-
|
226
|
-
it 'should return false if not completed' do
|
227
|
-
allow(@email_validations).to receive(:query).and_return(incompleted_query)
|
228
|
-
expect(@email_validations.completed?).to be false
|
229
|
-
end
|
230
317
|
end
|
231
318
|
end
|
232
319
|
|
233
|
-
describe '#destroy' do
|
234
|
-
it 'raise ArgumentError without @unique_id' do
|
235
|
-
expect{ @email_validations.destroy }.to raise_error(ArgumentError)
|
236
|
-
end
|
237
|
-
|
238
|
-
context 'with @unique_id' do
|
239
|
-
before(:each) do
|
240
|
-
@email_validations.instance_variable_set('@unique_id', 'fake')
|
241
|
-
end
|
242
|
-
|
243
|
-
it 'call #delete on @resource[@uniqueId]' do
|
244
|
-
request = double()
|
245
|
-
expect(resource).to receive(:[]).with('fake').and_return(request)
|
246
|
-
expect(request).to receive(:delete).and_return(double().as_null_object)
|
247
|
-
@email_validations.destroy
|
248
|
-
end
|
249
|
-
|
250
|
-
it 'clear @response, @unique_id and @error' do
|
251
|
-
request = double()
|
252
|
-
expect(resource).to receive(:[]).with('fake').and_return(request)
|
253
|
-
expect(request).to receive(:delete).and_return(double().as_null_object)
|
254
|
-
@email_validations.destroy
|
255
|
-
expect(@email_validations.instance_variable_get('@response')).to eq(nil)
|
256
|
-
expect(@email_validations.instance_variable_get('@unique_id')).to eq(nil)
|
257
|
-
expect(@email_validations.instance_variable_get('@error')).to eq(nil)
|
258
|
-
end
|
259
|
-
end
|
260
|
-
end
|
261
320
|
end
|
262
321
|
|
263
322
|
end
|
data/verifalia.gemspec
CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.rdoc_options = ['--line-numbers', '--inline-source', '--title', '--main', 'README.md']
|
23
23
|
|
24
24
|
spec.add_dependency('builder', '>= 2.1.2')
|
25
|
-
spec.add_dependency('rest-client', '
|
25
|
+
spec.add_dependency('rest-client', '>= 2.0.0')
|
26
26
|
|
27
27
|
spec.add_development_dependency "bundler", "~> 1.6"
|
28
28
|
spec.add_development_dependency "rake"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: verifalia
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Verifalia
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-09-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: builder
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: rest-client
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 2.0.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 2.0.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,11 +80,13 @@ files:
|
|
80
80
|
- LICENSE
|
81
81
|
- README.md
|
82
82
|
- Rakefile
|
83
|
+
- lib/rest/account_balance.rb
|
83
84
|
- lib/rest/client.rb
|
84
85
|
- lib/rest/email_validations.rb
|
85
86
|
- lib/verifalia.rb
|
86
87
|
- lib/verifalia/util/configuration.rb
|
87
88
|
- lib/verifalia/version.rb
|
89
|
+
- spec/rest/account_balance_spec.rb
|
88
90
|
- spec/rest/client_spec.rb
|
89
91
|
- spec/rest/email_validations_spec.rb
|
90
92
|
- spec/spec_helper.rb
|
@@ -121,6 +123,7 @@ signing_key:
|
|
121
123
|
specification_version: 4
|
122
124
|
summary: Verifalia API wrapper (email validation, list cleaning and scrubbing)
|
123
125
|
test_files:
|
126
|
+
- spec/rest/account_balance_spec.rb
|
124
127
|
- spec/rest/client_spec.rb
|
125
128
|
- spec/rest/email_validations_spec.rb
|
126
129
|
- spec/spec_helper.rb
|