verifalia 1.1.0 → 2.0.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.
Files changed (48) hide show
  1. checksums.yaml +5 -5
  2. data/Gemfile +5 -5
  3. data/LICENSE +3 -4
  4. data/README.md +329 -78
  5. data/Rakefile +4 -0
  6. data/lib/verifalia/client.rb +60 -0
  7. data/lib/verifalia/credits/balance.rb +30 -0
  8. data/lib/verifalia/credits/client.rb +25 -0
  9. data/lib/verifalia/email_validation/client.rb +207 -0
  10. data/lib/verifalia/email_validation/completion_callback.rb +15 -0
  11. data/lib/verifalia/email_validation/entry.rb +89 -0
  12. data/lib/verifalia/email_validation/job.rb +75 -0
  13. data/lib/verifalia/email_validation/overview.rb +76 -0
  14. data/lib/verifalia/email_validation/progress.rb +19 -0
  15. data/lib/verifalia/email_validation/request.rb +19 -0
  16. data/lib/verifalia/email_validation/request_entry.rb +14 -0
  17. data/lib/verifalia/email_validation/wait_options.rb +53 -0
  18. data/lib/verifalia/rest/client.rb +82 -0
  19. data/lib/verifalia/security/certificate_authenticator.rb +21 -0
  20. data/lib/verifalia/security/username_password_authenticator.rb +22 -0
  21. data/lib/verifalia.rb +8 -23
  22. data/sig/completion_callback.rbs +5 -0
  23. data/sig/verifalia/client.rbs +11 -0
  24. data/sig/verifalia/credits/balance.rbs +11 -0
  25. data/sig/verifalia/credits/client.rbs +7 -0
  26. data/sig/verifalia/email_validations/client.rbs +24 -0
  27. data/sig/verifalia/email_validations/entry.rbs +22 -0
  28. data/sig/verifalia/email_validations/job.rbs +13 -0
  29. data/sig/verifalia/email_validations/overview.rbs +20 -0
  30. data/sig/verifalia/email_validations/progress.rbs +8 -0
  31. data/sig/verifalia/email_validations/request.rbs +13 -0
  32. data/sig/verifalia/email_validations/request_entry.rbs +8 -0
  33. data/sig/verifalia/email_validations/wait_options.rbs +20 -0
  34. data/sig/verifalia/rest/client.rbs +12 -0
  35. data/sig/verifalia/rest.rbs +6 -0
  36. data/sig/verifalia/security/username_password_authenticator.rbs +10 -0
  37. data/verifalia.gemspec +27 -18
  38. metadata +56 -54
  39. data/.gitignore +0 -23
  40. data/lib/rest/client.rb +0 -75
  41. data/lib/rest/email_validations.rb +0 -143
  42. data/lib/verifalia/util/configuration.rb +0 -7
  43. data/lib/verifalia/version.rb +0 -3
  44. data/spec/rest/client_spec.rb +0 -88
  45. data/spec/rest/email_validations_spec.rb +0 -263
  46. data/spec/spec_helper.rb +0 -21
  47. data/spec/util/configuration_spec.rb +0 -15
  48. data/spec/verifalia_spec.rb +0 -17
@@ -1,143 +0,0 @@
1
- require 'rest_client'
2
- require 'json'
3
- module Verifalia
4
- module REST
5
- class EmailValidations
6
- ##
7
- # The Verifalia::REST::EmailValidations class allow you to comminucate
8
- # with Email Validations 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
- # The following keys are supported:
11
- #
12
- # === <tt>unique_id: 'example-example'</tt>
13
- #
14
- # The unique if of the Verifalia Email Validation resource
15
- #
16
- def initialize(config, account_sid, account_token, args = {})
17
- @resource = build_resource(config, account_sid, account_token)
18
- @unique_id = args[:unique_id] if args[:unique_id]
19
- end
20
-
21
- ##
22
- # Query the Email Validations Api with:
23
- #
24
- # === <tt> emails: ['test@test.com']
25
- #
26
- # An array of emails to validate
27
- #
28
- def verify(inputs, options = {})
29
- raise ArgumentError, 'inputs must be not empty' if (inputs.nil? || inputs.empty?)
30
- raise ArgumentError, 'options must be hash' if (!options.is_a?(Hash))
31
- data = inputs.map do |input|
32
- if (input.is_a? String)
33
- { inputData: input }
34
- elsif (input.is_a? Hash)
35
- raise ArgumentError, 'if inputs content is a Hash you need to supply :inputData as key' if (!input.has_key?(:inputData))
36
- input
37
- else
38
- raise ArgumentError, 'inputs content must be a String or a Hash'
39
- end
40
- end
41
- content = ({ entries: data }.merge(options)).to_json
42
- begin
43
- r = @resource.post content
44
- @unique_id = JSON.parse(r)["uniqueID"]
45
- @response = nil
46
- @error = nil
47
- @query_result = nil
48
- @unique_id
49
- rescue => e
50
- compute_error(e)
51
- false
52
- end
53
- end
54
-
55
- ##
56
- # Query the Email Validations Api for specific result. In order to use
57
- # this method you need to supply unique_id uring initialization or call verify first. If request fail,
58
- # you can call <tt>error</tt> to receive detailed information
59
- #
60
- def query
61
- raise ArgumentError, 'You must call verify first or supply and uniqueId' unless @unique_id
62
- if @response == nil || @response.code != 200
63
- begin
64
- @response = @resource[@unique_id].get
65
- @query_result = JSON.parse(@response)
66
- @error = nil
67
- rescue => e
68
- compute_error(e)
69
- return false
70
- end
71
- end
72
- @query_result
73
- end
74
-
75
- ##
76
- # Destroy an Email Validations entity. In order to use
77
- # this method you need to supply unique_id during initialization or call verify first. If request fail,
78
- # you can call <tt>error</tt> to receive detailed information
79
- #
80
- def destroy
81
- raise ArgumentError, 'You must call verify first or supply and uniqueId' unless @unique_id
82
- begin
83
- r = @resource[@unique_id].delete
84
- @error = nil
85
- @response = nil
86
- @query_result = nil
87
- @unique_id = nil
88
- true
89
- rescue => e
90
- compute_error(e)
91
- return false
92
- end
93
- end
94
-
95
- ##
96
- # Check if the Email validation entity is completed processed. In order to use
97
- # this method you need to supply unique_id during initialization or call verify first.
98
- #
99
- def completed?
100
- query_progress = query["progress"]
101
- @response.code == 200 && query_progress["noOfTotalEntries"] == query_progress["noOfCompletedEntries"]
102
- end
103
-
104
- def error
105
- @error
106
- end
107
-
108
- private
109
- def compute_error(e)
110
- unless e.is_a? RestClient::Exception
111
- @error = :internal_server_error
112
- end
113
-
114
- case e.http_code
115
- when 400
116
- @error = :bad_request
117
- when 401
118
- @error = :unauthorized
119
- when 402
120
- @error = :payment_required
121
- when 404
122
- @error = :not_found
123
- when 406
124
- @error = :not_acceptable
125
- when 410
126
- @error = :gone
127
- else
128
- @error = :internal_server_error
129
- end
130
- end
131
-
132
- def build_resource(config, account_sid, account_token)
133
- api_url = "#{config[:host]}/#{config[:api_version]}/email-validations"
134
- opts = {
135
- user: account_sid,
136
- password: account_token,
137
- headers: { content_type: :json }
138
- }
139
- return RestClient::Resource.new api_url, opts
140
- end
141
- end
142
- end
143
- end
@@ -1,7 +0,0 @@
1
- module Verifalia
2
- module Util
3
- class Configuration
4
- attr_accessor :account_sid, :auth_token
5
- end
6
- end
7
- end
@@ -1,3 +0,0 @@
1
- module Verifalia
2
- VERSION = "1.1.0"
3
- end
@@ -1,88 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Verifalia::REST::Client do
4
- describe 'config at class level' do
5
- after(:each) do
6
- Verifalia.instance_variable_set('@configuration', nil)
7
- end
8
-
9
- it 'should set the account sid and auth token with a config block' do
10
- Verifalia.configure do |config|
11
- config.account_sid = 'someSid'
12
- config.auth_token = 'someToken'
13
- end
14
-
15
- client = Verifalia::REST::Client.new
16
- expect(client.account_sid).to eq('someSid')
17
- expect(client.instance_variable_get('@auth_token')).to eq('someToken')
18
- end
19
-
20
- it 'should overwrite account sid and auth token if passed to initializer' do
21
- Verifalia.configure do |config|
22
- config.account_sid = 'someSid'
23
- config.auth_token = 'someToken'
24
- end
25
-
26
- client = Verifalia::REST::Client.new 'otherSid', 'otherToken'
27
- expect(client.account_sid).to eq('otherSid')
28
- expect(client.instance_variable_get('@auth_token')).to eq('otherToken')
29
- end
30
-
31
- it 'should overwrite the account sid if only the sid is given' do
32
- Verifalia.configure do |config|
33
- config.account_sid = 'someSid'
34
- config.auth_token = 'someToken'
35
- end
36
-
37
- client = Verifalia::REST::Client.new 'otherSid'
38
- expect(client.account_sid).to eq('otherSid')
39
- expect(client.instance_variable_get('@auth_token')).to eq('someToken')
40
- end
41
-
42
- it 'should allow options after setting up auth with config' do
43
- Verifalia.configure do |config|
44
- config.account_sid = 'someSid'
45
- config.auth_token = 'someToken'
46
- end
47
-
48
- client = Verifalia::REST::Client.new :host => 'api.fake.com'
49
-
50
- config = client.instance_variable_get('@config')
51
- expect(config[:host]).to eq('api.fake.com')
52
- end
53
-
54
- it 'should throw an argument error if the sid and token isn\'t set' do
55
- expect { Verifalia::REST::Client.new }.to raise_error(ArgumentError)
56
- end
57
-
58
- it 'should throw an argument error if only the account_sid is set' do
59
- expect { Verifalia::REST::Client.new 'someSid' }.to raise_error(ArgumentError)
60
- end
61
- end
62
-
63
- describe '#email_validations' do
64
- before(:each) do
65
- Verifalia.configure do |config|
66
- config.account_sid = 'someSid'
67
- config.auth_token = 'someToken'
68
- end
69
- @client = Verifalia::REST::Client.new
70
- end
71
-
72
- context 'without parameters' do
73
- it 'call #new on Verifalia::REST::EmailValidations with correct paramenters' do
74
- config = @client.instance_variable_get('@config')
75
- expect(Verifalia::REST::EmailValidations).to receive(:new).with(config, 'someSid', 'someToken')
76
- @client.email_validations
77
- end
78
- end
79
-
80
- context 'with parameter' do
81
- it 'call #new on Verifalia::REST::EmailValidations with correct paramenters' do
82
- config = @client.instance_variable_get('@config')
83
- expect(Verifalia::REST::EmailValidations).to receive(:new).with(config, 'someSid', 'someToken', :unique_id => 'fake')
84
- @client.email_validations(:unique_id => 'fake')
85
- end
86
- end
87
- end
88
- end
@@ -1,263 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Verifalia::REST::EmailValidations do
4
- let(:config) { { host: 'https://api.fake.com', api_version: "v" } }
5
-
6
- describe '#initialize' do
7
-
8
- it 'create RestClient::Resource with correct parameters' do
9
- api_url = "#{config[:host]}/#{config[:api_version]}/email-validations"
10
- opts = {
11
- user: 'someSid',
12
- password: 'someToken',
13
- headers: { content_type: :json }
14
- }
15
-
16
- expect(RestClient::Resource).to receive(:new).with(api_url, opts)
17
- Verifalia::REST::EmailValidations.new(config, 'someSid', 'someToken')
18
- end
19
-
20
- it 'associate RestClient::Resource to @resource' do
21
- resource = double()
22
- allow(RestClient::Resource).to receive(:new).and_return(resource)
23
- email_validations = Verifalia::REST::EmailValidations.new(config, 'someSid', 'someToken')
24
- expect(email_validations.instance_variable_get('@resource')).to eq(resource)
25
- end
26
-
27
- it 'associate :unique_id to @unique_id' do
28
- unique_id = double()
29
- email_validations = Verifalia::REST::EmailValidations.new(config, 'someSid', 'someToken', unique_id: unique_id)
30
- expect(email_validations.instance_variable_get('@unique_id')).to eq(unique_id)
31
- end
32
-
33
- end
34
-
35
- context 'initialized' do
36
- let(:resource) { double().as_null_object }
37
- let(:response) { double().as_null_object }
38
- let(:response_json) { double().as_null_object }
39
- before(:each) do
40
- @email_validations = Verifalia::REST::EmailValidations.new(config, 'someSid', 'someToken')
41
- @email_validations.instance_variable_set('@resource', resource)
42
- end
43
-
44
- describe '#verify' do
45
-
46
- it 'raise ArgumentError with nil emails' do
47
- expect{ @email_validations.verify(nil) }.to raise_error(ArgumentError)
48
- end
49
-
50
- it 'raise ArgumentError with empty emails' do
51
- expect{ @email_validations.verify([]) }.to raise_error(ArgumentError)
52
- end
53
-
54
- it 'raise ArgumentError with unvalid options' do
55
- emails = ['first', 'second']
56
- expect{ @email_validations.verify(emails, 'a string') }.to raise_error(ArgumentError)
57
- end
58
-
59
-
60
- it 'raise ArgumentError with with hash array parameters with invalid value' do
61
- inputs = [
62
- {
63
- fakeKey: 'fake second'
64
- },
65
- {
66
- fakeKey: 'fake second'
67
- }
68
- ]
69
- expect{ @email_validations.verify(inputs) }.to raise_error(ArgumentError)
70
- end
71
-
72
- it 'call #post on @resources with string array parameters' do
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
80
-
81
- it 'call #post on @resources with string array parameters and options' do
82
- emails = ['first', 'second']
83
- options = { option_1: 'test' }
84
- data = emails.map { |email| { inputData: email }}
85
- content = { entries: data, option_1: 'test' }.to_json
86
- expect(resource).to receive(:post).with(content).and_return(response)
87
- expect(JSON).to receive(:parse).with(response).and_return(response_json)
88
- @email_validations.verify(emails, options)
89
- end
90
-
91
- it 'call #post on @resources with hash array parameters with valid value' do
92
- data = [
93
- {
94
- inputData: 'first'
95
- },
96
- {
97
- inputData: 'second'
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
105
-
106
- it 'associate @unique_id and clear @response and @error' do
107
- emails = ['first', 'second']
108
- unique_id = 'fake'
109
- parsed = double()
110
- expect(JSON).to receive(:parse).and_return(parsed)
111
- expect(parsed).to receive(:[]).with("uniqueID").and_return(unique_id)
112
- @email_validations.verify(emails)
113
- expect(@email_validations.instance_variable_get('@unique_id')).to eq(unique_id)
114
- expect(@email_validations.instance_variable_get('@response')).to eq(nil)
115
- expect(@email_validations.instance_variable_get('@error')).to eq(nil)
116
- end
117
-
118
- it 'return @unique_id' do
119
- emails = ['first', 'second']
120
- unique_id = 'fake'
121
- parsed = double()
122
- expect(JSON).to receive(:parse).and_return(parsed)
123
- expect(parsed).to receive(:[]).with("uniqueID").and_return(unique_id)
124
- result = @email_validations.verify(emails)
125
- expect(result).to eq(unique_id)
126
- end
127
-
128
- context 'request failed' do
129
-
130
- it 'raise exception, call #compute_error and return false' do
131
- emails = ['first', 'second']
132
- expect(resource).to receive(:post).and_raise(RestClient::Exception)
133
- result = @email_validations.verify(emails)
134
- expect(result).to eq(false)
135
- expect(@email_validations.error).to eq(:internal_server_error)
136
- end
137
-
138
- it 'raise exception, call #compute_error and return correct error' do
139
- emails = ['first', 'second']
140
- exception = RestClient::Exception.new(nil, 402)
141
- expect(resource).to receive(:post).and_raise(exception)
142
- result = @email_validations.verify(emails)
143
- expect(result).to eq(false)
144
- expect(@email_validations.error).to eq(:payment_required)
145
- end
146
- end
147
- end
148
-
149
- describe '#query' do
150
- it 'raise ArgumentError without @unique_id' do
151
- expect{ @email_validations.query }.to raise_error(ArgumentError)
152
- end
153
-
154
- context 'with @unique_id' do
155
- before(:each) do
156
- @email_validations.instance_variable_set('@unique_id', 'fake')
157
- end
158
-
159
- it 'call #get on @resource[@uniqueId] with correct parameters' do
160
- request = double()
161
- expect(resource).to receive(:[]).with('fake').and_return(request)
162
- expect(request).to receive(:get).and_return(double().as_null_object)
163
- expect(JSON).to receive(:parse)
164
- @email_validations.query
165
- end
166
-
167
- it 'return parsed json' do
168
- parsed = double()
169
- expect(resource).to receive(:[]).with('fake')
170
- expect(JSON).to receive(:parse).and_return(parsed)
171
- result = @email_validations.query
172
- expect(result).to eq(parsed)
173
- end
174
-
175
- context 'request failed' do
176
-
177
- it 'raise exception, call #compute_error and return false' do
178
- request = double()
179
- expect(resource).to receive(:[]).with('fake').and_return(request)
180
- expect(request).to receive(:get).and_raise(RestClient::Exception)
181
- expect(@email_validations).to receive(:compute_error).and_return(double())
182
- result = @email_validations.query
183
- expect(result).to eq(false)
184
- end
185
- end
186
-
187
- end
188
- end
189
-
190
- describe '#completed?' do
191
- let(:response) { double().as_null_object }
192
-
193
- before(:each) do
194
- @email_validations.instance_variable_set('@response', response)
195
- end
196
-
197
-
198
- context 'with 202 http code response' do
199
- before(:each) do
200
- allow(response).to receive(:code).and_return(202)
201
- allow(@email_validations).to receive(:query).and_return({ "progress" => nil })
202
- end
203
-
204
- it 'should return false' do
205
- expect(@email_validations.completed?).to be false
206
- end
207
- end
208
-
209
- 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
-
217
- before(:each) do
218
- allow(response).to receive(:code).and_return(200)
219
- end
220
-
221
- it 'should return true if completed' do
222
- allow(@email_validations).to receive(:query).and_return(completed_query)
223
- expect(@email_validations.completed?).to be true
224
- 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
- end
231
- end
232
-
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
- end
262
-
263
- end
data/spec/spec_helper.rb DELETED
@@ -1,21 +0,0 @@
1
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
-
3
- require 'bundler'
4
- Bundler.setup
5
-
6
- require 'verifalia'
7
-
8
- RSpec.configure do |config|
9
- config.expect_with :rspec do |c|
10
- c.syntax = :expect
11
- end
12
- end
13
-
14
- if ENV["CI"]
15
- require 'coveralls'
16
- Coveralls.wear!
17
- else
18
- require "simplecov"
19
- SimpleCov.start
20
- end
21
-
@@ -1,15 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Verifalia::Util::Configuration do
4
- it 'should have an account sid attribute' do
5
- config = Verifalia::Util::Configuration.new
6
- config.account_sid = 'someSid'
7
- expect(config.account_sid).to eq('someSid')
8
- end
9
-
10
- it 'should have an auth token attribute' do
11
- config = Verifalia::Util::Configuration.new
12
- config.auth_token = 'someToken'
13
- expect(config.auth_token).to eq('someToken')
14
- end
15
- end
@@ -1,17 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Verifalia do
4
- after(:each) do
5
- Verifalia.instance_variable_set('@configuration', nil)
6
- end
7
-
8
- it 'should set the account sid and auth token with a config block' do
9
- Verifalia.configure do |config|
10
- config.account_sid = 'someSid'
11
- config.auth_token = 'someToken'
12
- end
13
-
14
- expect(Verifalia.account_sid).to eq('someSid')
15
- expect(Verifalia.auth_token).to eq('someToken')
16
- end
17
- end