zerobounce 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5ef1325579afd30cbb3769cfbfe204c057fa7f12
4
- data.tar.gz: 425716b0fc5f8af222625c4b9761745363d6fac8
3
+ metadata.gz: c721e810255839a88073d013c9b97065471160e1
4
+ data.tar.gz: 84b8c3683741ea097daba407bc245ebd0d92b3cf
5
5
  SHA512:
6
- metadata.gz: 9cdf22f0b5d461eb37247ba3ab8facdbd73edea58381a54d3f9f85e5bc960dcab76f88549f9452b1d69af23c43e8847ece85e9ec405d8538a8ce7602e71367b5
7
- data.tar.gz: 0674c6bdd1e4b52ccd492315c59faf5006c7c1a29534082c6300ee064c292c3aeffc4b12ad248f471c955cc9ea281c610de2e767a43a9cc8e9d976586cb68cd4
6
+ metadata.gz: be2427a95a25fed9e115da52757cefe0c389a67e02d6b720117ae24f760c5de86603a0b9b8af2d46f9c30ede766e83d2003d64b4633cd3b3cfdcd360654f33c7
7
+ data.tar.gz: 9ec05422f597f3b91fba26d07ef5d57be77a46b0936a61481a6b9290121a5e20eb7c4917cd0c0e266b3e519a67d3839b757420be8ffcd630e7441f7b53c866ae
@@ -43,11 +43,7 @@ module Zerobounce
43
43
  # @option params [Proc] :middleware Use different middleware for this request.
44
44
  # @return [Zerobounce::Response]
45
45
  def validate(params)
46
- if params.key?(:ipaddress) || params.key?(:ip_address)
47
- Request.new(params).validate_with_ip(params)
48
- else
49
- Request.new(params).validate(params)
50
- end
46
+ Request.new(params).validate(params)
51
47
  end
52
48
 
53
49
  # Get the number of remaining credits on the account.
@@ -24,6 +24,9 @@ module Zerobounce
24
24
  # @note If you modify the default make sure to add middleware to parse
25
25
  # the response as json and symbolize the keys.
26
26
  #
27
+ # @attr [String] api_version
28
+ # The version of the API to use.
29
+ #
27
30
  # @attr [Array<Symbol>] valid_statues
28
31
  # The statuses that are considered valid by {Response#valid?}.
29
32
  class Configuration
@@ -31,16 +34,19 @@ module Zerobounce
31
34
  attr_accessor :headers
32
35
  attr_accessor :apikey
33
36
  attr_accessor :middleware
37
+ attr_accessor :api_version
34
38
  attr_accessor :valid_statuses
35
39
 
36
40
  def initialize
37
41
  self.host = 'https://api.zerobounce.net'
38
42
  self.apikey = ENV['ZEROBOUNCE_API_KEY']
43
+ self.api_version = 'v2'
39
44
  self.valid_statuses = %i[valid catch_all]
40
45
  self.headers = { user_agent: "ZerobounceRubyGem/#{Zerobounce::VERSION}" }
41
46
 
42
47
  self.middleware = proc do |builder|
43
48
  builder.response(:json, content_type: /\bjson$/, parser_options: { symbolize_names: true })
49
+ builder.response(:logger) { |l| l.filter(/(api_?key=)(\w+)/, '\1[REMOVED]') } if ENV['ZEROBOUNCE_API_DEBUG']
44
50
  builder.use(Zerobounce::Middleware::RaiseError)
45
51
  builder.adapter(Faraday.default_adapter)
46
52
  end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'faraday'
4
+ require 'zerobounce/request/v1_request'
5
+ require 'zerobounce/request/v2_request'
4
6
 
5
7
  module Zerobounce
6
8
  # Sends the HTTP request.
@@ -16,46 +18,30 @@ module Zerobounce
16
18
  # @attr_reader [Proc] middleware
17
19
  # Faraday middleware used for the request.
18
20
  class Request
19
- # The normal email validation endpoint.
20
- VALIDATE_PATH = '/v1/validate'
21
- # The validation endpoint for email and IP validation.
22
- VALIDATE_WITH_IP_PATH = '/v1/validatewithip'
23
- # The path to get number number of credits remaining on the account.
24
- GET_CREDITS_PATH = '/v1/getcredits'
25
-
26
21
  attr_reader :host
27
22
  attr_reader :headers
28
23
  attr_reader :middleware
24
+ attr_reader :api_version
29
25
 
26
+ # Set instance variables and extends the correct Zerobounce::Request
27
+ #
30
28
  # @param [Hash] params
31
29
  # @option params [String] :middleware default: {Configuration#middleware} {include:#middleware}
32
30
  # @option params [String] :headers default: {Configuration#headers} {include:#headers}
33
31
  # @option params [String] :host default: {Configuration#host} {include:#host}
32
+ # @option params [String] :api_version default: {Configuration#api_version} {include:#api_version}
34
33
  def initialize(params={})
35
34
  @middleware = params[:middleware] || Zerobounce.config.middleware
36
35
  @headers = params[:headers] || Zerobounce.config.headers
37
36
  @host = params[:host] || Zerobounce.config.host
38
- end
39
-
40
- # Validate the email address.
41
- #
42
- # @param [Hash] params
43
- # @option params [String] :email
44
- # @option params [String] :apikey
45
- # @return [Zerobounce::Response]
46
- def validate(params)
47
- Response.new(get(VALIDATE_PATH, params), self)
48
- end
37
+ @api_version = params[:api_version] || Zerobounce.config.api_version
49
38
 
50
- # Validate the email address and get geoip info for the IP.
51
- #
52
- # @param [Hash] params
53
- # @option params [String] :email
54
- # @option params [String] :ip_address
55
- # @option params [String] :apikey
56
- # @return [Zerobounce::Response]
57
- def validate_with_ip(params)
58
- Response.new(get(VALIDATE_WITH_IP_PATH, params), self)
39
+ case api_version
40
+ when 'v2'
41
+ extend(V2Request)
42
+ else
43
+ extend(V1Request)
44
+ end
59
45
  end
60
46
 
61
47
  # Get the number of remaining credits on the account.
@@ -64,7 +50,7 @@ module Zerobounce
64
50
  # @option params [String] :apikey
65
51
  # @return [Integer] A value of -1 can mean the API is invalid.
66
52
  def credits(params={})
67
- get(GET_CREDITS_PATH, params).body[:Credits]&.to_i
53
+ get('getcredits', params).body[:Credits]&.to_i
68
54
  end
69
55
 
70
56
  private
@@ -80,15 +66,7 @@ module Zerobounce
80
66
 
81
67
  # @return [Faraday::Connection]
82
68
  def conn
83
- @conn ||= Faraday.new(host, headers: headers, &middleware)
84
- end
85
-
86
- # @param [Hash] params
87
- # @return [Hash]
88
- def get_params(params)
89
- valid_params = %i[apikey ipaddress email]
90
- params[:ipaddress] = params.delete(:ip_address) if params.key?(:ip_address) # normalize ipaddress key
91
- { apikey: Zerobounce.config.apikey }.merge(params.select { |k, _| valid_params.include?(k) })
69
+ @conn ||= Faraday.new("#{host}/#{api_version}", headers: headers, &middleware)
92
70
  end
93
71
  end
94
72
  end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zerobounce
4
+ class Request
5
+ # Request methods specific to V1 of the API.
6
+ module V1Request
7
+ # Valid v1 get query params
8
+ VALID_GET_PARAMS = %i[apikey ipaddress email].freeze
9
+
10
+ # Validate the email address.
11
+ #
12
+ # @param [Hash] params
13
+ # @option params [String] :email
14
+ # @option params [String] :ip_address
15
+ # @option params [String] :apikey
16
+ # @return [Zerobounce::Response]
17
+ def validate(params)
18
+ if params.key?(:ipaddress) || params.key?(:ip_address)
19
+ validate_with_ip(params)
20
+ else
21
+ Response.new(get('validate', params), self)
22
+ end
23
+ end
24
+
25
+ # Validate the email address and get geoip info for the IP.
26
+ #
27
+ # @param [Hash] params
28
+ # @option params [String] :email
29
+ # @option params [String] :ip_address
30
+ # @option params [String] :apikey
31
+ # @return [Zerobounce::Response]
32
+ def validate_with_ip(params)
33
+ Response.new(get('validatewithip', params), self)
34
+ end
35
+
36
+ private
37
+
38
+ # @param [Hash] params
39
+ # @return [Hash]
40
+ def get_params(params)
41
+ params[:ipaddress] = params.delete(:ip_address) if params.key?(:ip_address) # normalize ipaddress key
42
+ params[:apikey] = params.delete(:api_key) if params.key?(:api_key) # normalize apikey param
43
+ { apikey: Zerobounce.config.apikey }.merge(params.select { |k, _| VALID_GET_PARAMS.include?(k) })
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zerobounce
4
+ class Request
5
+ # Request methods specific to V2 of the API.
6
+ module V2Request
7
+ # Valid v2 query params
8
+ VALID_GET_PARAMS = %i[api_key ip_address email].freeze
9
+
10
+ # Validate the email address.
11
+ #
12
+ # @param [Hash] params
13
+ # @option params [String] :email
14
+ # @option params [String] :ip_address
15
+ # @option params [String] :api_key
16
+ # @return [Zerobounce::Response]
17
+ def validate(params)
18
+ Response.new(get('validate', params), self)
19
+ end
20
+
21
+ private
22
+
23
+ # @param [Hash] params
24
+ # @return [Hash]
25
+ def get_params(params)
26
+ params[:ip_address] ||= '' # ip_address must be in query string
27
+ params[:api_key] = params.delete(:apikey) if params.key?(:apikey) # normalize api_key param
28
+ { api_key: Zerobounce.config.apikey }.merge(params.select { |k, _| VALID_GET_PARAMS.include?(k) })
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'time'
4
+ require 'zerobounce/response/v1_response'
5
+ require 'zerobounce/response/v2_response'
4
6
 
5
7
  module Zerobounce
6
8
  # A Zerobounce response
@@ -22,51 +24,13 @@ module Zerobounce
22
24
  @response = response
23
25
  @request = request
24
26
  @body = response.body
25
- end
26
-
27
- # Deliverability status
28
- #
29
- # Possible values:
30
- # :valid
31
- # :invalid
32
- # :catch_all
33
- # :unknown
34
- # :spamtrap
35
- # :abuse
36
- # :do_not_mail
37
- #
38
- # @return [Symbol] The status as a +Symbol+.
39
- def status
40
- @status ||= underscore(@body[:status])&.to_sym
41
- end
42
27
 
43
- # A more detailed status
44
- #
45
- # Possible values:
46
- # :antispam_system
47
- # :greylisted
48
- # :mail_server_temporary_error
49
- # :forcible_disconnect
50
- # :mail_server_did_not_respond
51
- # :timeout_exceeded
52
- # :failed_smtp_connection
53
- # :mailbox_quota_exceeded
54
- # :exception_occurred
55
- # :possible_traps
56
- # :role_based
57
- # :global_suppression
58
- # :mailbox_not_found
59
- # :no_dns_entries
60
- # :failed_syntax_check
61
- # :possible_typo
62
- # :unroutable_ip_address
63
- # :leading_period_removed
64
- # :does_not_accept_mail
65
- # :alias_address
66
- #
67
- # @return [Symbol] The sub_status as a +Symbol+.
68
- def sub_status
69
- @sub_status ||= underscore(@body[:sub_status])&.to_sym
28
+ case request.api_version
29
+ when 'v2'
30
+ extend(V2Response)
31
+ else
32
+ extend(V1Response)
33
+ end
70
34
  end
71
35
 
72
36
  # The email address you are validating.
@@ -111,13 +75,6 @@ module Zerobounce
111
75
  @gender ||= @body[:gender]
112
76
  end
113
77
 
114
- # The location of the owner of the email when available.
115
- #
116
- # @return [String, nil]
117
- def location
118
- @location ||= @body[:location]
119
- end
120
-
121
78
  # The country of the IP passed in.
122
79
  #
123
80
  # @return [String, nil]
@@ -162,48 +119,13 @@ module Zerobounce
162
119
  !valid?
163
120
  end
164
121
 
165
- # If the email domain is disposable, which are usually temporary email addresses.
166
- #
167
- # These are temporary emails created for the sole purpose to sign up to websites without giving their real
168
- # email address. These emails are short lived from 15 minutes to around 6 months.
169
- #
170
- # @note If you have valid emails with this flag set to +true+, you shouldn't email them.
171
- #
172
- # @return [Boolean]
173
- def disposable?
174
- @disposable ||= @body[:disposable] || false
175
- end
176
-
177
- # These domains are known for abuse, spam, or are bot created.
178
- #
179
- # @note If you have a valid email with this flag set to +true+, you shouldn't email them.
180
- #
181
- # @return [Boolean]
182
- def toxic?
183
- @toxic ||= @body[:toxic] || false
184
- end
185
-
186
- # The UTC time the email was validated.
187
- #
188
- # @return [Time, nil]
189
- def process_date
190
- @process_date ||= @body[:processedat] && Time.parse(@body[:processedat])
191
- end
192
-
193
- # The creation date of the email when available.
194
- #
195
- # @return [Time, nil]
196
- def creation_date
197
- @creation_date ||= @body[:creationdate] && Time.parse(@body[:creationdate])
198
- end
199
-
200
122
  # Returns a string containing a human-readable representation.
201
123
  #
202
124
  # @note Overriding inspect to hide the {#request}/{#response} instance variables
203
125
  #
204
126
  # @return [String]
205
127
  def inspect
206
- "#<#{self.class.name}:#{object_id}>"
128
+ "#<#{self.class.name}:0x#{object_id.to_s(16)} @address=#{address}>"
207
129
  end
208
130
 
209
131
  # Convert to a hash.
@@ -215,14 +137,5 @@ module Zerobounce
215
137
  memo[meth] = send(meth)
216
138
  end
217
139
  end
218
-
219
- private
220
-
221
- # @param [String, nil] word
222
- # @return [String, nil]
223
- def underscore(word)
224
- return if word.nil? || word.empty?
225
- word.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase.tr('-', '_')
226
- end
227
140
  end
228
141
  end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zerobounce
4
+ class Response
5
+ # V1 specific methods for the API.
6
+ module V1Response
7
+ # Deliverability status
8
+ #
9
+ # Possible values:
10
+ # :valid
11
+ # :invalid
12
+ # :catch_all
13
+ # :unknown
14
+ # :spamtrap
15
+ # :abuse
16
+ # :do_not_mail
17
+ #
18
+ # @return [Symbol] The status as a +Symbol+.
19
+ def status
20
+ @status ||= underscore(@body[:status])&.to_sym
21
+ end
22
+
23
+ # A more detailed status
24
+ #
25
+ # Possible values:
26
+ # :antispam_system
27
+ # :greylisted
28
+ # :mail_server_temporary_error
29
+ # :forcible_disconnect
30
+ # :mail_server_did_not_respond
31
+ # :timeout_exceeded
32
+ # :failed_smtp_connection
33
+ # :mailbox_quota_exceeded
34
+ # :exception_occurred
35
+ # :possible_traps
36
+ # :role_based
37
+ # :global_suppression
38
+ # :mailbox_not_found
39
+ # :no_dns_entries
40
+ # :failed_syntax_check
41
+ # :possible_typo
42
+ # :unroutable_ip_address
43
+ # :leading_period_removed
44
+ # :does_not_accept_mail
45
+ # :alias_address
46
+ #
47
+ # @return [Symbol] The sub_status as a +Symbol+.
48
+ def sub_status
49
+ @sub_status ||= underscore(@body[:sub_status])&.to_sym
50
+ end
51
+
52
+ # If the email domain is disposable, which are usually temporary email addresses.
53
+ #
54
+ # These are temporary emails created for the sole purpose to sign up to websites without giving their real
55
+ # email address. These emails are short lived from 15 minutes to around 6 months.
56
+ #
57
+ # @note If you have valid emails with this flag set to +true+, you shouldn't email them.
58
+ #
59
+ # @return [Boolean]
60
+ def disposable?
61
+ @disposable ||= @body[:disposable] || false
62
+ end
63
+
64
+ # These domains are known for abuse, spam, or are bot created.
65
+ #
66
+ # @note If you have a valid email with this flag set to +true+, you shouldn't email them.
67
+ #
68
+ # @return [Boolean]
69
+ def toxic?
70
+ @toxic ||= @body[:toxic] || false
71
+ end
72
+
73
+ # The location of the owner of the email when available.
74
+ #
75
+ # @return [String, nil]
76
+ def location
77
+ @location ||= @body[:location]
78
+ end
79
+
80
+ # The UTC time the email was validated.
81
+ #
82
+ # @return [Time, nil]
83
+ def processed_at
84
+ @processed_at ||= @body[:processedat] && Time.parse(@body[:processedat])
85
+ end
86
+
87
+ # The creation date of the email when available.
88
+ #
89
+ # @return [Time, nil]
90
+ def creation_date
91
+ @creation_date ||= @body[:creationdate] && Time.parse(@body[:creationdate])
92
+ end
93
+
94
+ private
95
+
96
+ # @param [String, nil] word
97
+ # @return [String, nil]
98
+ def underscore(word)
99
+ return if word.nil? || word.empty?
100
+ word.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase.tr('-', '_')
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zerobounce
4
+ class Response
5
+ # V2 specific methods for the API.
6
+ module V2Response
7
+ # Deliverability status
8
+ #
9
+ # @see https://docs.zerobounce.net/docs/status-codes-v2
10
+ #
11
+ # Possible values:
12
+ # :valid
13
+ # :invalid
14
+ # :catch_all
15
+ # :unknown
16
+ # :spamtrap
17
+ # :abuse
18
+ # :do_not_mail
19
+ #
20
+ # @return [Symbol] The status as a +Symbol+.
21
+ def status
22
+ @status ||= @body[:status]&.tr('-', '_')&.to_sym
23
+ end
24
+
25
+ # @return [Symbol, nil]
26
+ def sub_status
27
+ @sub_status ||= @body[:sub_status] == '' ? nil : @body[:sub_status]&.to_sym
28
+ end
29
+
30
+ # @return [Integer]
31
+ def domain_age_days
32
+ @domain_age_days ||= @body[:domain_age_days].to_i
33
+ end
34
+
35
+ # The SMTP Provider of the email.
36
+ #
37
+ # @return [String, nil]
38
+ def smtp_provider
39
+ @smtp_provider ||= @body[:smtp_provider]
40
+ end
41
+
42
+ # @return [String, nil]
43
+ def did_you_mean
44
+ @did_you_mean ||= @body[:did_you_mean]
45
+ end
46
+
47
+ # The preferred MX record of the domain.
48
+ #
49
+ # @return [String, nil]
50
+ def mx_record
51
+ @mx_record ||= @body[:mx_record]
52
+ end
53
+
54
+ # The UTC time the email was validated.
55
+ #
56
+ # @return [Time, nil]
57
+ def processed_at
58
+ @processed_at ||= @body[:processed_at] && Time.parse(@body[:processed_at])
59
+ end
60
+
61
+ # If the email comes from a free provider.
62
+ #
63
+ # @return [Boolean]
64
+ def free_email?
65
+ @free_email ||= @body[:free_email] || false
66
+ end
67
+
68
+ # If the email domain is disposable, which are usually temporary email addresses.
69
+ #
70
+ # These are temporary emails created for the sole purpose to sign up to websites without giving their real
71
+ # email address. These emails are short lived from 15 minutes to around 6 months.
72
+ #
73
+ # @note If you have valid emails with this flag set to +true+, you shouldn't email them.
74
+ #
75
+ # @return [Boolean]
76
+ def disposable?
77
+ @disposable ||= sub_status == :disposable
78
+ end
79
+
80
+ # These domains are known for abuse, spam, or are bot created.
81
+ #
82
+ # @note If you have a valid email with this flag set to +true+, you shouldn't email them.
83
+ #
84
+ # @return [Boolean]
85
+ def toxic?
86
+ @toxic ||= sub_status == :toxic
87
+ end
88
+
89
+ # Does the domain have an MX record.
90
+ #
91
+ # @return [Boolean]
92
+ def mx_found?
93
+ @mx_found ||= @body[:mx_found] == 'true'
94
+ end
95
+ end
96
+ end
97
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Zerobounce
4
4
  # The version of the gem.
5
- VERSION = '0.0.6'
5
+ VERSION = '0.1.0'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zerobounce
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Frase
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-26 00:00:00.000000000 Z
11
+ date: 2018-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -197,7 +197,11 @@ files:
197
197
  - lib/zerobounce/error.rb
198
198
  - lib/zerobounce/middleware/raise_error.rb
199
199
  - lib/zerobounce/request.rb
200
+ - lib/zerobounce/request/v1_request.rb
201
+ - lib/zerobounce/request/v2_request.rb
200
202
  - lib/zerobounce/response.rb
203
+ - lib/zerobounce/response/v1_response.rb
204
+ - lib/zerobounce/response/v2_response.rb
201
205
  - lib/zerobounce/version.rb
202
206
  - zerobounce.gemspec
203
207
  homepage: https://github.com/afrase/zerobounce