zerobounce-sdk 3.0.0 → 3.1.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/CHANGELOG.md +184 -17
- data/README.md +26 -13
- data/lib/zerobounce/api_key.rb +238 -7
- data/lib/zerobounce/api_urls.rb +53 -0
- data/lib/zerobounce/base_request.rb +199 -82
- data/lib/zerobounce/client.rb +12 -4
- data/lib/zerobounce/configuration.rb +19 -3
- data/lib/zerobounce/error.rb +19 -7
- data/lib/zerobounce/get_file_helper.rb +40 -27
- data/lib/zerobounce/http_transport.rb +229 -0
- data/lib/zerobounce/priority3.rb +187 -46
- data/lib/zerobounce/request.rb +25 -42
- data/lib/zerobounce/response_error.rb +27 -4
- data/lib/zerobounce/response_shape.rb +190 -0
- data/lib/zerobounce/strict_bool.rb +23 -0
- data/lib/zerobounce/strict_json.rb +165 -0
- data/lib/zerobounce/version.rb +1 -1
- data/lib/zerobounce/warmup.rb +155 -69
- data/lib/zerobounce.rb +138 -97
- metadata +5 -15
data/lib/zerobounce/request.rb
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'stringio'
|
|
4
3
|
require 'zerobounce/base_request'
|
|
5
4
|
require 'zerobounce/get_file_helper'
|
|
6
5
|
|
|
@@ -9,15 +8,6 @@ module Zerobounce
|
|
|
9
8
|
# Sends the HTTP request.
|
|
10
9
|
class Request < BaseRequest
|
|
11
10
|
|
|
12
|
-
# The email finder and domain search. Their result is never a bare
|
|
13
|
-
# message, so a body made only of message keys is how they report a
|
|
14
|
-
# rejected request. LIVE: an invalid or drained key gets an HTTP 200 with
|
|
15
|
-
# {"Message": "Invalid API key or your account ran out of credits"}.
|
|
16
|
-
# Everywhere else a bare message is a result: validate's greylist answer
|
|
17
|
-
# {"Message": "Greylist validation still in progress..."} and the
|
|
18
|
-
# filters' {"Message": "Filter successfully added"}.
|
|
19
|
-
MESSAGE_ONLY_ERROR_PATHS = %w[guessformat].freeze
|
|
20
|
-
|
|
21
11
|
def self.get(path, params, content_type='application/json')
|
|
22
12
|
response = self._get(Zerobounce.configuration.api_root_url, path, params, content_type)
|
|
23
13
|
interpret_json_response(response, path)
|
|
@@ -26,27 +16,25 @@ module Zerobounce
|
|
|
26
16
|
def self.bulk_get(path, params, content_type='application/json')
|
|
27
17
|
response = self._get(Zerobounce.configuration.bulk_api_root_url, path, params, content_type)
|
|
28
18
|
if __json_content_type__(response.headers[:content_type])
|
|
29
|
-
|
|
30
|
-
response_body_json
|
|
31
|
-
return response_body_json unless response_body_json.is_a?(Hash)
|
|
19
|
+
response_body_json = __parse_json_body__(response)
|
|
20
|
+
return __require_shape__(response, response_body_json, path) unless response_body_json.is_a?(Hash)
|
|
32
21
|
|
|
33
22
|
if __error_value__(response_body_json['error'])
|
|
34
23
|
raise ApiResponseError.new(ResponseError.strip_prefix(response_body_json['error']), response.code)
|
|
35
24
|
end
|
|
36
25
|
if response_body_json['errors'].is_a?(Array) && !response_body_json['errors'].empty?
|
|
37
|
-
|
|
26
|
+
first = __first_error__(response, response_body_json['errors'])
|
|
27
|
+
raise ApiResponseError.new(ResponseError.strip_prefix(first), response.code)
|
|
38
28
|
end
|
|
39
29
|
__raise_if_success_false__(response, response_body_json)
|
|
40
30
|
__raise_if_bare_error__(response, response_body_json)
|
|
31
|
+
__raise_if_untyped_answer__(response, response_body_json, path)
|
|
41
32
|
|
|
42
|
-
return response_body_json
|
|
33
|
+
return __require_shape__(response, response_body_json, path)
|
|
43
34
|
else
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
content = StringIO.new(response.body)
|
|
48
|
-
content.set_encoding_by_bom
|
|
49
|
-
return content.string
|
|
35
|
+
# filestatus and deletefile answer in JSON: anything else (an HTML
|
|
36
|
+
# page, an empty or non-JSON body) raises with the HTTP status.
|
|
37
|
+
__parse_json_response__(response, path)
|
|
50
38
|
end
|
|
51
39
|
end
|
|
52
40
|
|
|
@@ -62,21 +50,16 @@ module Zerobounce
|
|
|
62
50
|
interpret_json_response(response, path)
|
|
63
51
|
end
|
|
64
52
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
end
|
|
71
|
-
return response.body
|
|
53
|
+
# +root+ defaults to bulk_api_root_url; the list evaluator passes its own.
|
|
54
|
+
def self.bulk_post(path, params, content_type='application/json', filepath=nil,
|
|
55
|
+
root: Zerobounce.configuration.bulk_api_root_url)
|
|
56
|
+
response = self._post(root, path, params, content_type, filepath)
|
|
57
|
+
__parse_json_response__(response, path)
|
|
72
58
|
end
|
|
73
59
|
|
|
74
60
|
def self.bulk_root_get(path, params, content_type='application/json')
|
|
75
61
|
response = self._get(bulk_api_root_without_v2, path, params, content_type)
|
|
76
|
-
|
|
77
|
-
return __parse_json_response__(response)
|
|
78
|
-
end
|
|
79
|
-
response.body
|
|
62
|
+
__parse_json_response__(response, path)
|
|
80
63
|
end
|
|
81
64
|
|
|
82
65
|
def self.bulk_root_getfile(path, params)
|
|
@@ -86,10 +69,7 @@ module Zerobounce
|
|
|
86
69
|
|
|
87
70
|
def self.bulk_root_post(path, params, content_type='application/json', filepath=nil)
|
|
88
71
|
response = self._post(bulk_api_root_without_v2, path, params, content_type, filepath)
|
|
89
|
-
|
|
90
|
-
return __parse_json_response__(response)
|
|
91
|
-
end
|
|
92
|
-
response.body
|
|
72
|
+
__parse_json_response__(response, path)
|
|
93
73
|
end
|
|
94
74
|
|
|
95
75
|
def self.bulk_root_form_post(path, params)
|
|
@@ -105,11 +85,14 @@ module Zerobounce
|
|
|
105
85
|
raise ApiResponseError.new(ResponseError.message_from_parts(response.code, content_type, body), response.code)
|
|
106
86
|
end
|
|
107
87
|
begin
|
|
108
|
-
parsed =
|
|
88
|
+
parsed = StrictJson.parse(body)
|
|
109
89
|
rescue JSON::ParserError
|
|
110
|
-
|
|
90
|
+
# cause: nil, or the parser's error, which quotes the body, would ride
|
|
91
|
+
# along as #cause.
|
|
92
|
+
raise ApiResponseError.new('ZeroBounce returned a response that is not valid JSON.', response.code),
|
|
93
|
+
cause: nil
|
|
111
94
|
end
|
|
112
|
-
return parsed unless parsed.is_a?(Hash)
|
|
95
|
+
return __require_shape__(response, parsed, path) unless parsed.is_a?(Hash)
|
|
113
96
|
|
|
114
97
|
code = response.code
|
|
115
98
|
if __error_value__(parsed['error'])
|
|
@@ -117,17 +100,17 @@ module Zerobounce
|
|
|
117
100
|
end
|
|
118
101
|
__raise_if_success_false__(response, parsed)
|
|
119
102
|
__raise_if_bare_error__(response, parsed)
|
|
120
|
-
|
|
103
|
+
__raise_if_untyped_answer__(response, parsed, path)
|
|
121
104
|
if parsed.key?('errors') && parsed['errors'].is_a?(Array) && !parsed['errors'].empty?
|
|
122
105
|
account = parsed['errors'].find { |item| item.is_a?(Hash) && item['email_address'].to_s.casecmp('all').zero? }
|
|
123
106
|
if account
|
|
124
107
|
raise ApiResponseError.new(ResponseError.strip_prefix(account['error']), code)
|
|
125
108
|
end
|
|
126
109
|
unless path == 'validatebatch'
|
|
127
|
-
raise ApiResponseError.new(ResponseError.strip_prefix(parsed['errors']
|
|
110
|
+
raise ApiResponseError.new(ResponseError.strip_prefix(__first_error__(response, parsed['errors'])), code)
|
|
128
111
|
end
|
|
129
112
|
end
|
|
130
|
-
parsed
|
|
113
|
+
__require_shape__(response, parsed, path)
|
|
131
114
|
end
|
|
132
115
|
|
|
133
116
|
end
|
|
@@ -19,7 +19,28 @@ module Zerobounce
|
|
|
19
19
|
sample.include?('<body')
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
+
# What an error body says when its message key holds nothing that is text.
|
|
23
|
+
NO_MESSAGE_TEXT = 'ZeroBounce returned an error with no message text.'
|
|
24
|
+
|
|
25
|
+
# The text of a message value: a non-blank String, or the non-blank
|
|
26
|
+
# Strings of an Array joined with a line break; nil for anything else.
|
|
27
|
+
def text_of(value)
|
|
28
|
+
value = value.select { |item| item.is_a?(String) && !item.strip.empty? }.join("\n") if value.is_a?(Array)
|
|
29
|
+
value.is_a?(String) && !value.strip.empty? ? value : nil
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Whether a message value is present but is not text (an object, a number).
|
|
33
|
+
def non_text?(value)
|
|
34
|
+
return false if value.nil? || value == [] || (value.is_a?(String) && value.strip.empty?)
|
|
35
|
+
|
|
36
|
+
text_of(value).nil?
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# +message+ without any "Error:" prefix. A value that is not a String is
|
|
40
|
+
# never shown as a Ruby inspect: a list of strings is joined with a line
|
|
41
|
+
# break, anything else is the fixed no-text message.
|
|
22
42
|
def strip_prefix(message)
|
|
43
|
+
message = text_of(message) || NO_MESSAGE_TEXT unless message.nil? || message.is_a?(String)
|
|
23
44
|
text = message.to_s.strip
|
|
24
45
|
loop do
|
|
25
46
|
if text.start_with?('Error:')
|
|
@@ -78,9 +99,9 @@ module Zerobounce
|
|
|
78
99
|
end
|
|
79
100
|
%w[error message error_message Message detail].each do |key|
|
|
80
101
|
value = parsed[key]
|
|
81
|
-
|
|
102
|
+
text = text_of(value)
|
|
103
|
+
return strip_prefix(text) if text
|
|
82
104
|
next unless value.is_a?(Array) && !value.empty?
|
|
83
|
-
return strip_prefix(value[0]) if value[0].is_a?(String) && !value[0].strip.empty?
|
|
84
105
|
|
|
85
106
|
# A validation error list: [{"loc" => [...], "msg" => "..."}].
|
|
86
107
|
if value[0].is_a?(Hash) && value[0]['msg']
|
|
@@ -89,11 +110,13 @@ module Zerobounce
|
|
|
89
110
|
return field.empty? ? value[0]['msg'].to_s : "#{field}: #{value[0]['msg']}"
|
|
90
111
|
end
|
|
91
112
|
end
|
|
113
|
+
# A message key that holds an object or a number: no text to show.
|
|
114
|
+
return NO_MESSAGE_TEXT if %w[error message error_message Message detail].any? { |key| non_text?(parsed[key]) }
|
|
92
115
|
end
|
|
93
116
|
# A top-level list of strings, e.g. ["In-progress tests cannot be deleted."].
|
|
94
117
|
if parsed.is_a?(Array)
|
|
95
|
-
|
|
96
|
-
return strip_prefix(
|
|
118
|
+
joined = text_of(parsed)
|
|
119
|
+
return strip_prefix(joined) if joined
|
|
97
120
|
end
|
|
98
121
|
return 'ZeroBounce returned a response that is not valid JSON.' if trimmed.length > 1000
|
|
99
122
|
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'zerobounce/error'
|
|
4
|
+
|
|
5
|
+
module Zerobounce
|
|
6
|
+
# The documented JSON type of each field of the typed calls' answers (standard.md
|
|
7
|
+
# section 3: field types are part of the shape). A field the documentation types
|
|
8
|
+
# that arrives with another JSON type is an API error with status 200, never a
|
|
9
|
+
# coerced value; null is accepted for every field that is not required. The
|
|
10
|
+
# answer itself is returned unchanged, still a Hash.
|
|
11
|
+
# rubocop:disable Metrics/ModuleLength
|
|
12
|
+
module ResponseShape
|
|
13
|
+
# fields: name => kind. required: names that must be present and not null.
|
|
14
|
+
# items: array name => the Shape of each item. greylist: a bare
|
|
15
|
+
# {"Message": "..."} answer is a result (validate only).
|
|
16
|
+
Shape = Struct.new(:fields, :required, :items, :greylist)
|
|
17
|
+
|
|
18
|
+
# The JSON types a kind allows. A flag is a boolean, or the "true"/"false"
|
|
19
|
+
# string the API also sends. A text_or_array is a string, or an array of strings.
|
|
20
|
+
KINDS = {
|
|
21
|
+
string: [String],
|
|
22
|
+
flag: [TrueClass, FalseClass, String],
|
|
23
|
+
int: [Integer],
|
|
24
|
+
number: [Integer, Float],
|
|
25
|
+
array: [Array],
|
|
26
|
+
text_or_array: [String, Array]
|
|
27
|
+
}.freeze
|
|
28
|
+
|
|
29
|
+
def self.shape(fields, required: [], items: {}, greylist: false)
|
|
30
|
+
Shape.new(fields, required, items, greylist)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
VALIDATE = {
|
|
34
|
+
'address' => :string, 'status' => :string, 'sub_status' => :string, 'account' => :string,
|
|
35
|
+
'domain' => :string, 'did_you_mean' => :string, 'domain_age_days' => :string, 'active_in_days' => :string,
|
|
36
|
+
'active_first_seen' => :string, 'free_email' => :flag, 'mx_found' => :flag, 'mx_record' => :string,
|
|
37
|
+
'smtp_provider' => :string, 'catchall_domain' => :flag, 'firstname' => :string, 'lastname' => :string,
|
|
38
|
+
'gender' => :string, 'city' => :string, 'region' => :string, 'zipcode' => :string, 'country' => :string,
|
|
39
|
+
'processed_at' => :string, 'domain_website_exists' => :string, 'domain_registrant_company_name' => :string,
|
|
40
|
+
'message' => :string, 'Message' => :string
|
|
41
|
+
}.freeze
|
|
42
|
+
|
|
43
|
+
BATCH_ERROR = { 'error' => :string, 'email_address' => :string }.freeze
|
|
44
|
+
|
|
45
|
+
API_USAGE_COUNTERS = %w[
|
|
46
|
+
total status_valid status_invalid status_catch_all status_do_not_mail status_spamtrap status_unknown
|
|
47
|
+
sub_status_toxic sub_status_disposable sub_status_role_based sub_status_possible_trap
|
|
48
|
+
sub_status_global_suppression sub_status_timeout_exceeded sub_status_mail_server_temporary_error
|
|
49
|
+
sub_status_mail_server_did_not_respond sub_status_greylisted sub_status_antispam_system
|
|
50
|
+
sub_status_does_not_accept_mail sub_status_exception_occurred sub_status_failed_syntax_check
|
|
51
|
+
sub_status_mailbox_not_found sub_status_unroutable_ip_address sub_status_possible_typo
|
|
52
|
+
sub_status_no_dns_entries sub_status_role_based_catch_all sub_status_mailbox_quota_exceeded
|
|
53
|
+
sub_status_forcible_disconnect sub_status_failed_smtp_connection sub_status_accept_all
|
|
54
|
+
sub_status_mx_forward sub_status_alternate sub_status_allowed sub_status_blocked sub_status_gold
|
|
55
|
+
sub_status_role_based_accept_all sub_status_leading_period_removed sub_status_alias_address
|
|
56
|
+
].freeze
|
|
57
|
+
API_USAGE = API_USAGE_COUNTERS.to_h { |name| [name, :int] }.merge('start_date' => :string, 'end_date' => :string)
|
|
58
|
+
.freeze
|
|
59
|
+
|
|
60
|
+
ACTIVITY = { 'found' => :flag, 'active_in_days' => :string, 'active_first_seen' => :string }.freeze
|
|
61
|
+
SCORE = { 'email' => :string, 'score' => :int }.freeze
|
|
62
|
+
|
|
63
|
+
FILE_STATUS = {
|
|
64
|
+
'success' => :flag, 'message' => :text_or_array, 'file_id' => :string, 'file_name' => :string,
|
|
65
|
+
'upload_date' => :string, 'file_status' => :string, 'file_phase_2_status' => :string,
|
|
66
|
+
'complete_percentage' => :string, 'error_reason' => :string, 'return_url' => :string
|
|
67
|
+
}.freeze
|
|
68
|
+
|
|
69
|
+
# A file job's send and delete answers.
|
|
70
|
+
FILE_JOB = {
|
|
71
|
+
'success' => :flag, 'message' => :text_or_array, 'file_name' => :string, 'file_id' => :string
|
|
72
|
+
}.freeze
|
|
73
|
+
|
|
74
|
+
LIST_EVALUATOR = {
|
|
75
|
+
'file_id' => :string, 'status' => :string, 'progress' => :number, 'invalid_percentage' => :number,
|
|
76
|
+
'catch_all_percentage' => :number, 'activity_data_percentage' => :number, 'abuse' => :flag,
|
|
77
|
+
'do_not_mail' => :flag, 'spam_trap' => :flag, 'total_risky_percentage' => :number,
|
|
78
|
+
'error_message' => :string
|
|
79
|
+
}.freeze
|
|
80
|
+
|
|
81
|
+
# guessformat, find_domain and find_email share one endpoint, so one shape covers all three.
|
|
82
|
+
GUESS = {
|
|
83
|
+
'email' => :string, 'domain' => :string, 'company_name' => :string, 'format' => :string,
|
|
84
|
+
'status' => :string, 'sub_status' => :string, 'confidence' => :string, 'email_confidence' => :string,
|
|
85
|
+
'failure_reason' => :string, 'did_you_mean' => :string, 'other_domain_formats' => :array
|
|
86
|
+
}.freeze
|
|
87
|
+
DOMAIN_FORMAT = { 'format' => :string, 'confidence' => :string }.freeze
|
|
88
|
+
|
|
89
|
+
VALIDATE_SHAPE = shape(VALIDATE, required: %w[address status], greylist: true)
|
|
90
|
+
FILE_STATUS_SHAPE = shape(FILE_STATUS)
|
|
91
|
+
FILE_JOB_SHAPE = shape(FILE_JOB)
|
|
92
|
+
BATCH_SHAPE = shape(
|
|
93
|
+
{ 'email_batch' => :array, 'errors' => :array },
|
|
94
|
+
items: { 'email_batch' => shape(VALIDATE, required: %w[address status]), 'errors' => shape(BATCH_ERROR) }
|
|
95
|
+
)
|
|
96
|
+
GUESS_SHAPE = shape(GUESS, items: { 'other_domain_formats' => shape(DOMAIN_FORMAT) })
|
|
97
|
+
|
|
98
|
+
# The shape of each typed answer, by the endpoint path the request uses, or by
|
|
99
|
+
# the name of a call whose path is not fixed (the list evaluator's).
|
|
100
|
+
SHAPES = {
|
|
101
|
+
'validate' => VALIDATE_SHAPE,
|
|
102
|
+
'validatebatch' => BATCH_SHAPE,
|
|
103
|
+
'getapiusage' => shape(API_USAGE),
|
|
104
|
+
'activity' => shape(ACTIVITY),
|
|
105
|
+
'scoring' => shape(SCORE),
|
|
106
|
+
'filestatus' => FILE_STATUS_SHAPE,
|
|
107
|
+
'scoring/filestatus' => FILE_STATUS_SHAPE,
|
|
108
|
+
'email-finder/filestatus' => FILE_STATUS_SHAPE,
|
|
109
|
+
'domain-search/filestatus' => FILE_STATUS_SHAPE,
|
|
110
|
+
'sendfile' => FILE_JOB_SHAPE,
|
|
111
|
+
'scoring/sendfile' => FILE_JOB_SHAPE,
|
|
112
|
+
'email-finder/sendfile' => FILE_JOB_SHAPE,
|
|
113
|
+
'domain-search/sendfile' => FILE_JOB_SHAPE,
|
|
114
|
+
'send-evaluated-file/' => FILE_JOB_SHAPE,
|
|
115
|
+
'deletefile' => FILE_JOB_SHAPE,
|
|
116
|
+
'scoring/deletefile' => FILE_JOB_SHAPE,
|
|
117
|
+
'email-finder/deletefile' => FILE_JOB_SHAPE,
|
|
118
|
+
'domain-search/deletefile' => FILE_JOB_SHAPE,
|
|
119
|
+
'guessformat' => GUESS_SHAPE,
|
|
120
|
+
:list_evaluator => shape(LIST_EVALUATOR)
|
|
121
|
+
}.freeze
|
|
122
|
+
|
|
123
|
+
module_function
|
|
124
|
+
|
|
125
|
+
# +result+ (a Hash the API sent) checked against the shape of +key+; it is returned
|
|
126
|
+
# unchanged. A key with no shape is not checked.
|
|
127
|
+
def check!(result, key, status = 200)
|
|
128
|
+
shape = SHAPES[key]
|
|
129
|
+
return result if shape.nil?
|
|
130
|
+
|
|
131
|
+
check_object!(result, shape, status, '')
|
|
132
|
+
result
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def check_object!(result, shape, status, where)
|
|
136
|
+
check_hash!(result, status, where)
|
|
137
|
+
return if where.empty? && shape.greylist && greylist_answer?(result)
|
|
138
|
+
|
|
139
|
+
check_required!(result, shape, status, where)
|
|
140
|
+
shape.fields.each { |name, kind| check_field!(result, [name, kind], shape, status, where) }
|
|
141
|
+
nil
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def check_hash!(result, status, where)
|
|
145
|
+
return if result.is_a?(Hash)
|
|
146
|
+
|
|
147
|
+
subject = where.empty? ? 'the response' : where.chomp('.')
|
|
148
|
+
raise unexpected("#{subject} is not an object", status)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def check_field!(result, (name, kind), shape, status, where)
|
|
152
|
+
value = result[name]
|
|
153
|
+
check_value!(value, kind, "#{where}#{name}", status)
|
|
154
|
+
check_items!(value, shape.items[name], status, "#{where}#{name}") if shape.items.key?(name) && value
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def check_required!(result, shape, status, where)
|
|
158
|
+
missing = shape.required.find { |name| result[name].nil? }
|
|
159
|
+
raise unexpected("#{where}#{missing} is missing", status) if missing
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def check_value!(value, kind, path, status)
|
|
163
|
+
return if value.nil? || allowed?(value, kind)
|
|
164
|
+
|
|
165
|
+
raise unexpected("#{path} has the wrong type", status)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def check_items!(items, item_shape, status, path)
|
|
169
|
+
items.each_with_index { |item, index| check_object!(item, item_shape, status, "#{path}[#{index}].") }
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# validate's greylist answer: a bare message, with no address or status.
|
|
173
|
+
def greylist_answer?(result)
|
|
174
|
+
greylist = result['Message'].is_a?(String)
|
|
175
|
+
greylist && !result.key?('address') && !result.key?('status')
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def allowed?(value, kind)
|
|
179
|
+
return false unless KINDS.fetch(kind).any? { |type| value.is_a?(type) }
|
|
180
|
+
return value.all?(String) if kind == :text_or_array && value.is_a?(Array)
|
|
181
|
+
|
|
182
|
+
true
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def unexpected(problem, status)
|
|
186
|
+
ApiResponseError.new("ZeroBounce returned JSON of an unexpected shape: #{problem}.", status)
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
# rubocop:enable Metrics/ModuleLength
|
|
190
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Zerobounce
|
|
4
|
+
# A boolean option, parsed strictly: true/false, 'true'/'false' in any case,
|
|
5
|
+
# or 1/0. Anything else is an ArgumentError, so 'false' can no longer be sent
|
|
6
|
+
# as true and a typo is not silently taken as a flag. Shared by the client
|
|
7
|
+
# and the Warmup client.
|
|
8
|
+
module StrictBool
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def parse(value, name)
|
|
12
|
+
return value if [true, false].include?(value)
|
|
13
|
+
return value == 1 if value.is_a?(Integer) && [0, 1].include?(value)
|
|
14
|
+
|
|
15
|
+
if value.is_a?(String)
|
|
16
|
+
text = value.strip.downcase
|
|
17
|
+
return true if %w[true 1].include?(text)
|
|
18
|
+
return false if %w[false 0].include?(text)
|
|
19
|
+
end
|
|
20
|
+
raise ArgumentError, "#{name} must be true or false"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
module Zerobounce
|
|
6
|
+
# Parses a response body as strict RFC 8259 JSON.
|
|
7
|
+
#
|
|
8
|
+
# Ruby's JSON.parse is lenient: it accepts /* */ and // comments (json 2.7
|
|
9
|
+
# and later), and other versions differ in what else they let through. A
|
|
10
|
+
# body that is not JSON is not an API response, so the body is checked
|
|
11
|
+
# first, untrimmed, by a small RFC 8259 validator: only space, tab, CR and
|
|
12
|
+
# LF may surround the value (no BOM, no NBSP, no NUL). Only then is it
|
|
13
|
+
# parsed, with allow_nan: false pinned.
|
|
14
|
+
#
|
|
15
|
+
# A recursive-descent validator reads best as one method per grammar rule,
|
|
16
|
+
# so the method size metrics are off for this module.
|
|
17
|
+
# rubocop:disable Metrics/ModuleLength, Metrics/AbcSize, Metrics/CyclomaticComplexity
|
|
18
|
+
# rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
19
|
+
module StrictJson
|
|
20
|
+
module_function
|
|
21
|
+
|
|
22
|
+
WHITESPACE = [0x20, 0x09, 0x0a, 0x0d].freeze
|
|
23
|
+
ESCAPES = '"\\/bfnrt'.bytes.freeze
|
|
24
|
+
HEX = '0123456789abcdefABCDEF'.bytes.freeze
|
|
25
|
+
MAX_DEPTH = 512
|
|
26
|
+
|
|
27
|
+
# The parsed value, or JSON::ParserError (without the body in its message).
|
|
28
|
+
def parse(text)
|
|
29
|
+
body = text.to_s
|
|
30
|
+
raise JSON::ParserError, 'response is not valid JSON' unless valid?(body)
|
|
31
|
+
|
|
32
|
+
JSON.parse(body, allow_nan: false)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Whether the whole text is one RFC 8259 JSON value.
|
|
36
|
+
def valid?(text)
|
|
37
|
+
bytes = text.to_s.b
|
|
38
|
+
pos = value(bytes, skip_ws(bytes, 0), 0)
|
|
39
|
+
!pos.nil? && skip_ws(bytes, pos) == bytes.bytesize
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def skip_ws(bytes, pos)
|
|
43
|
+
pos += 1 while pos < bytes.bytesize && WHITESPACE.include?(bytes.getbyte(pos))
|
|
44
|
+
pos
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# The position after the value at pos, or nil.
|
|
48
|
+
def value(bytes, pos, depth)
|
|
49
|
+
return nil if depth > MAX_DEPTH
|
|
50
|
+
|
|
51
|
+
case bytes.getbyte(pos)
|
|
52
|
+
when 0x7b then object(bytes, pos + 1, depth + 1)
|
|
53
|
+
when 0x5b then array(bytes, pos + 1, depth + 1)
|
|
54
|
+
when 0x22 then string(bytes, pos + 1)
|
|
55
|
+
when 0x74 then literal(bytes, pos, 'true')
|
|
56
|
+
when 0x66 then literal(bytes, pos, 'false')
|
|
57
|
+
when 0x6e then literal(bytes, pos, 'null')
|
|
58
|
+
else number(bytes, pos)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def literal(bytes, pos, word)
|
|
63
|
+
bytes.byteslice(pos, word.bytesize) == word ? pos + word.bytesize : nil
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def object(bytes, pos, depth)
|
|
67
|
+
pos = skip_ws(bytes, pos)
|
|
68
|
+
return pos + 1 if bytes.getbyte(pos) == 0x7d
|
|
69
|
+
|
|
70
|
+
loop do
|
|
71
|
+
return nil unless bytes.getbyte(pos) == 0x22
|
|
72
|
+
|
|
73
|
+
pos = string(bytes, pos + 1)
|
|
74
|
+
return nil if pos.nil?
|
|
75
|
+
|
|
76
|
+
pos = skip_ws(bytes, pos)
|
|
77
|
+
return nil unless bytes.getbyte(pos) == 0x3a
|
|
78
|
+
|
|
79
|
+
pos = value(bytes, skip_ws(bytes, pos + 1), depth)
|
|
80
|
+
return nil if pos.nil?
|
|
81
|
+
|
|
82
|
+
pos = skip_ws(bytes, pos)
|
|
83
|
+
case bytes.getbyte(pos)
|
|
84
|
+
when 0x7d then return pos + 1
|
|
85
|
+
when 0x2c then pos = skip_ws(bytes, pos + 1)
|
|
86
|
+
else return nil
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def array(bytes, pos, depth)
|
|
92
|
+
pos = skip_ws(bytes, pos)
|
|
93
|
+
return pos + 1 if bytes.getbyte(pos) == 0x5d
|
|
94
|
+
|
|
95
|
+
loop do
|
|
96
|
+
pos = value(bytes, pos, depth)
|
|
97
|
+
return nil if pos.nil?
|
|
98
|
+
|
|
99
|
+
pos = skip_ws(bytes, pos)
|
|
100
|
+
case bytes.getbyte(pos)
|
|
101
|
+
when 0x5d then return pos + 1
|
|
102
|
+
when 0x2c then pos = skip_ws(bytes, pos + 1)
|
|
103
|
+
else return nil
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# pos is just after the opening quote.
|
|
109
|
+
def string(bytes, pos)
|
|
110
|
+
loop do
|
|
111
|
+
byte = bytes.getbyte(pos)
|
|
112
|
+
return nil if byte.nil? || byte < 0x20
|
|
113
|
+
return pos + 1 if byte == 0x22
|
|
114
|
+
|
|
115
|
+
if byte == 0x5c
|
|
116
|
+
escape = bytes.getbyte(pos + 1)
|
|
117
|
+
if escape == 0x75
|
|
118
|
+
return nil unless (2..5).all? { |i| HEX.include?(bytes.getbyte(pos + i)) }
|
|
119
|
+
|
|
120
|
+
pos += 6
|
|
121
|
+
elsif ESCAPES.include?(escape)
|
|
122
|
+
pos += 2
|
|
123
|
+
else
|
|
124
|
+
return nil
|
|
125
|
+
end
|
|
126
|
+
else
|
|
127
|
+
pos += 1
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# -? (0 | [1-9][0-9]*) (. [0-9]+)? ([eE] [+-]? [0-9]+)?
|
|
133
|
+
def number(bytes, pos)
|
|
134
|
+
pos += 1 if bytes.getbyte(pos) == 0x2d
|
|
135
|
+
first = bytes.getbyte(pos)
|
|
136
|
+
return nil unless digit?(first)
|
|
137
|
+
|
|
138
|
+
pos = first == 0x30 ? pos + 1 : digits(bytes, pos)
|
|
139
|
+
if bytes.getbyte(pos) == 0x2e
|
|
140
|
+
return nil unless digit?(bytes.getbyte(pos + 1))
|
|
141
|
+
|
|
142
|
+
pos = digits(bytes, pos + 1)
|
|
143
|
+
end
|
|
144
|
+
if [0x65, 0x45].include?(bytes.getbyte(pos))
|
|
145
|
+
pos += 1
|
|
146
|
+
pos += 1 if [0x2b, 0x2d].include?(bytes.getbyte(pos))
|
|
147
|
+
return nil unless digit?(bytes.getbyte(pos))
|
|
148
|
+
|
|
149
|
+
pos = digits(bytes, pos)
|
|
150
|
+
end
|
|
151
|
+
pos
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def digits(bytes, pos)
|
|
155
|
+
pos += 1 while digit?(bytes.getbyte(pos))
|
|
156
|
+
pos
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def digit?(byte)
|
|
160
|
+
!byte.nil? && byte >= 0x30 && byte <= 0x39
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
# rubocop:enable Metrics/ModuleLength, Metrics/AbcSize, Metrics/CyclomaticComplexity
|
|
164
|
+
# rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
165
|
+
end
|
data/lib/zerobounce/version.rb
CHANGED