Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/sdk_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,26 @@ jobs:

- name: Run specs
run: bundle exec rspec

# The gem as users install it: built from the gemspec, installed into an
# empty GEM_HOME and loaded from outside the checkout, so lib/ is not on
# the load path and a file left out of spec.files fails here.
- name: Build, install and load the gem
run: |
gem_file="$RUNNER_TEMP/zerobounce-sdk.gem"
gem build zerobounce.gemspec --output "$gem_file"
ruby -rrubygems/package -e 'missing = Dir["lib/**/*.rb"] - Gem::Package.new(ARGV[0]).spec.files
abort "Not in the gem: #{missing.join(", ")}" unless missing.empty?' "$gem_file"
export GEM_HOME="$RUNNER_TEMP/gem-home" GEM_PATH="$RUNNER_TEMP/gem-home"
gem install --no-document rest-client -v '~> 2.1'
gem install --local --no-document "$gem_file"
cd "$RUNNER_TEMP"
ruby -e '
require "zerobounce"
home = ENV.fetch("GEM_HOME")
stray = $LOADED_FEATURES.grep(%r{/zerobounce[/.]}).reject { |f| f.start_with?(home) }
abort "Loaded from outside the installed gem: #{stray.join(", ")}" unless stray.empty?
Zerobounce.configure { |c| c.apikey = "not-a-real-key" }
Zerobounce::Warmup.new("not-a-real-key").inspect
puts "zerobounce-sdk #{Zerobounce::VERSION} loads from #{Gem.loaded_specs.fetch("zerobounce-sdk").full_gem_path}"
'
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `validate_file_send_stream` and `scoring_file_send_stream` work again. From 2.0.1 to 3.0.1 they raised `ArgumentError` ("File path must be under the current directory") whenever the system temp directory is outside the current directory, as it usually is: they wrote the content to a Tempfile in the system temp directory and uploaded its path, which the path check added in 2.0.1 rejects. 2.0.0 was not affected. The content is now uploaded from memory under the given `file_name` (2.0.0 sent the temp file's random name); file paths passed to `validate_file_send` and the other upload methods are still checked as before.
- A response whose body is cut short (the connection closes before `Content-Length` bytes arrive) raises `Zerobounce::ApiResponseError` with `status_code` 0 on every request, including the result-file downloads and `Zerobounce::Warmup`. Up to 3.0.1 Net::HTTP returned the bytes that had arrived, so a truncated result file or JSON body after a 200 header was returned as a success.
- `Zerobounce::Warmup#download_seeds` and `#download_statistics` raise `Zerobounce::ApiResponseError` (with the HTTP status) for an HTML page on a 200, such as a proxy's "Access denied" page, as the Python SDK does. Up to 3.0.1 the page was returned as the CSV text.
- An HTML page on a 200 (such as a proxy's "Access denied" page), or a JSON content type with a body that does not parse, raises `Zerobounce::ApiResponseError` with `status_code` 200 from every request method, as the Python and PHP SDKs do; so does any other non-JSON body from the calls that answer in JSON. Up to 3.0.1 `validate_file_send`, `scoring_file_send` (and their `_stream` forms), `send_email_finder_file`, `send_domain_search_file`, `email_finder_file_status`, `email_finder_file_delete`, `domain_search_file_status`, `domain_search_file_delete`, `send_list_evaluator_file`, `send_evaluated_file` and `list_s3_files` returned an HTML or other non-JSON body as a successful `String`; `validate_file_check`, `validate_file_delete`, `scoring_file_check` and `scoring_file_delete` raised a plain `RuntimeError` with no status for an HTML page, and `JSON::ParserError` for a JSON content type with a body that does not parse.
- `Zerobounce::Warmup#add_sending_account` parses `ignore_conflict` strictly, as the client parses its boolean options: `true`/`false`, `'true'`/`'false'` (any case) and `1`/`0` are accepted, and anything else raises `ArgumentError` before a request is sent. Up to 3.0.1 any truthy value was sent as `true`, so `ignore_conflict: 'false'` sent `true`. `ignore_conflict: nil` now leaves the field out (3.0.1 sent `false`).

## [3.0.1] - 2026-11-11

Expand Down
11 changes: 9 additions & 2 deletions lib/zerobounce/base_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,18 +343,25 @@ def self.__raise_if_bare_error__(response, parsed)
end
end

def self.__parse_json_response__(response)
# The parsed JSON body. An HTML page, an empty body or one that is not
# valid JSON raises ApiResponseError with the HTTP status.
def self.__parse_json_body__(response)
body = response.body.to_s
content_type = (response.headers[:content_type] || '').to_s
code = response.code
if ResponseError.html?(body, content_type) || body.strip.empty?
raise ApiResponseError.new(ResponseError.message_from_parts(code, content_type, body), code)
end
begin
parsed = JSON.parse(body)
JSON.parse(body)
rescue JSON::ParserError
raise ApiResponseError.new('ZeroBounce returned a response that is not valid JSON.', code)
end
end

def self.__parse_json_response__(response)
parsed = __parse_json_body__(response)
code = response.code
return parsed unless parsed.is_a?(Hash)

__raise_if_success_false__(response, parsed)
Expand Down
15 changes: 3 additions & 12 deletions lib/zerobounce/priority3.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'erb'
require 'zerobounce/strict_bool'

module Zerobounce
module Priority3
Expand Down Expand Up @@ -290,19 +291,9 @@ def __blank__(value)
value.nil? || value.to_s.strip.empty?
end

# A boolean option, parsed strictly: true/false, 'true'/'false' in any
# case, or 1/0. Anything else is an ArgumentError, so 'false' can no
# longer be sent as true and a typo is not silently taken as a flag.
# A boolean option, parsed strictly (see StrictBool).
def __strict_bool__(value, name)
return value if [true, false].include?(value)
return value == 1 if value.is_a?(Integer) && [0, 1].include?(value)

if value.is_a?(String)
text = value.strip.downcase
return true if %w[true 1].include?(text)
return false if %w[false 0].include?(text)
end
raise ArgumentError, "#{name} must be true or false"
StrictBool.parse(value, name)
end

# A strict boolean option as the string the API expects on the wire.
Expand Down
28 changes: 7 additions & 21 deletions lib/zerobounce/request.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require 'stringio'
require 'zerobounce/base_request'
require 'zerobounce/get_file_helper'

Expand All @@ -26,8 +25,7 @@ def self.get(path, params, content_type='application/json')
def self.bulk_get(path, params, content_type='application/json')
response = self._get(Zerobounce.configuration.bulk_api_root_url, path, params, content_type)
if __json_content_type__(response.headers[:content_type])
response_body = response.body
response_body_json = JSON.parse(response_body)
response_body_json = __parse_json_body__(response)
return response_body_json unless response_body_json.is_a?(Hash)

if __error_value__(response_body_json['error'])
Expand All @@ -41,12 +39,9 @@ def self.bulk_get(path, params, content_type='application/json')

return response_body_json
else
if ResponseError.html?(response.body, response.headers[:content_type])
raise ResponseError.message_from(response)
end
content = StringIO.new(response.body)
content.set_encoding_by_bom
return content.string
# filestatus and deletefile answer in JSON: anything else (an HTML
# page, an empty or non-JSON body) raises with the HTTP status.
__parse_json_response__(response)
end
end

Expand All @@ -66,18 +61,12 @@ def self.post(path, params, content_type='application/json', filepath=nil)
def self.bulk_post(path, params, content_type='application/json', filepath=nil,
root: Zerobounce.configuration.bulk_api_root_url)
response = self._post(root, path, params, content_type, filepath)
if __json_content_type__(response.headers[:content_type])
return __parse_json_response__(response)
end
return response.body
__parse_json_response__(response)
end

def self.bulk_root_get(path, params, content_type='application/json')
response = self._get(bulk_api_root_without_v2, path, params, content_type)
if __json_content_type__(response.headers[:content_type])
return __parse_json_response__(response)
end
response.body
__parse_json_response__(response)
end

def self.bulk_root_getfile(path, params)
Expand All @@ -87,10 +76,7 @@ def self.bulk_root_getfile(path, params)

def self.bulk_root_post(path, params, content_type='application/json', filepath=nil)
response = self._post(bulk_api_root_without_v2, path, params, content_type, filepath)
if __json_content_type__(response.headers[:content_type])
return __parse_json_response__(response)
end
response.body
__parse_json_response__(response)
end

def self.bulk_root_form_post(path, params)
Expand Down
23 changes: 23 additions & 0 deletions lib/zerobounce/strict_bool.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Zerobounce
# A boolean option, parsed strictly: true/false, 'true'/'false' in any case,
# or 1/0. Anything else is an ArgumentError, so 'false' can no longer be sent
# as true and a typo is not silently taken as a flag. Shared by the client
# and the Warmup client.
module StrictBool
module_function

def parse(value, name)
return value if [true, false].include?(value)
return value == 1 if value.is_a?(Integer) && [0, 1].include?(value)

if value.is_a?(String)
text = value.strip.downcase
return true if %w[true 1].include?(text)
return false if %w[false 0].include?(text)
end
raise ArgumentError, "#{name} must be true or false"
end
end
end
7 changes: 5 additions & 2 deletions lib/zerobounce/warmup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'zerobounce/api_key'
require 'zerobounce/error'
require 'zerobounce/response_error'
require 'zerobounce/strict_bool'

module Zerobounce
# Client for the ZeroBounce Warmup API
Expand Down Expand Up @@ -92,8 +93,10 @@ def add_sending_account(from_address, onboarding_model, **options)
raise ArgumentError, 'email_content and managed_sending are for zb_managed'
end
end
body['ignore_conflict'] = !options[:ignore_conflict].nil? && options[:ignore_conflict] ? true : false if
options.key?(:ignore_conflict)
unless options[:ignore_conflict].nil?
# Parsed strictly, as the client's boolean options are: 'false' is false.
body['ignore_conflict'] = StrictBool.parse(options[:ignore_conflict], 'ignore_conflict')
end
body['email_content'] = email_content_body(options[:email_content]) if options.key?(:email_content)
body['managed_sending'] = managed_sending_body(options[:managed_sending]) if options.key?(:managed_sending)

Expand Down
52 changes: 37 additions & 15 deletions spec/transport_guards_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ def expect_timeouts(name)
end
end

# An HTML page (a proxy's or Cloudflare's "Access denied") on a 200 is an
# error carrying that status on every request path, never a result.
describe 'an HTML page on a 200' do
page = '<!DOCTYPE html><html><head><title>Access denied</title></head><body>no</body></html>'

Expand All @@ -330,24 +332,44 @@ def expect_timeouts(name)
.to_return(status: 200, body: page, headers: { 'Content-Type' => 'text/html; charset=UTF-8' })
end

%i[validate_file_get scoring_file_get email_finder_file_get domain_search_file_get].each do |name|
it "is an error for #{name}, not a result file" do
expect { TransportSweep::CALLS.fetch(name).call }
.to raise_error(Zerobounce::ApiResponseError) { |error|
expect(error.status_code).to eq(200)
expect(error.message).to include('Access denied')
}
def expect_page_error(&call)
expect(&call).to raise_error(Zerobounce::ApiResponseError) { |error|
expect([error.status_code, error.message]).to match([200, include('Access denied')])
}
end

TransportSweep::CALLS.each do |name, call|
it "is an error with the status for #{name}" do
expect_page_error { call.call }
end
end

%i[download_seeds download_statistics].each do |name|
it "is an error for Warmup##{name}, not CSV" do
warmup = TransportSweep.warmup(api_key, 'https://first.example')
expect { TransportSweep::WARMUP_CALLS.fetch(name).call(warmup) }
.to raise_error(Zerobounce::ApiResponseError) { |error|
expect(error.status_code).to eq(200)
expect(error.message).to include('Access denied')
}
TransportSweep::WARMUP_CALLS.each do |name, call|
it "is an error with the status for Warmup##{name}" do
expect_page_error { call.call(TransportSweep.warmup(api_key, 'https://first.example')) }
end
end
end
describe 'a JSON content type with a body that is not valid JSON, on a 200' do
before do
TransportSweep.configure(api_key, 'https://first.example')
stub_request(:any, %r{\Ahttps://first\.example/})
.to_return(status: 200, body: '{"success":', headers: { 'Content-Type' => 'application/json' })
end

def expect_error_on_ok(&call)
expect(&call).to raise_error(Zerobounce::ApiResponseError) { |error| expect(error.status_code).to eq(200) }
end

TransportSweep::CALLS.each do |name, call|
it "is an error with the status for #{name}" do
expect_error_on_ok { call.call }
end
end

TransportSweep::WARMUP_CALLS.each do |name, call|
it "is an error with the status for Warmup##{name}" do
expect_error_on_ok { call.call(TransportSweep.warmup(api_key, 'https://first.example')) }
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions spec/warmup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,23 @@ def stub_ok(body = '{"ok":true}', status: 200, content_type: 'application/json')
)
end

it 'parses ignore_conflict strictly, as the client parses its boolean options' do
add = ->(value) { warmup.add_sending_account('a@b.com', 'self_managed', engagement_rule_id: 2, ignore_conflict: value) }
['junk', 'yes', 2, '', Object.new].each do |bad|
expect { add.call(bad) }.to raise_error(ArgumentError, /ignore_conflict/)
end
expect(request_count).to eq(0)

[[false, false], ['false', false], ['FALSE', false], [0, false], ['0', false],
[true, true], ['true', true], [1, true], ['1', true]].each do |value, sent|
add.call(value)
expect(last_json['ignore_conflict']).to be(sent), "#{value.inspect} was sent as #{last_json['ignore_conflict']}"
end

add.call(nil)
expect(last_json).not_to have_key('ignore_conflict')
end

it 'checks the zb_managed payload' do
expect do
warmup.add_sending_account('a@b.com', 'zb_managed',
Expand Down
Loading