diff --git a/.github/workflows/sdk_ci.yml b/.github/workflows/sdk_ci.yml index 363d738..e51a802 100644 --- a/.github/workflows/sdk_ci.yml +++ b/.github/workflows/sdk_ci.yml @@ -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}" + ' diff --git a/CHANGELOG.md b/CHANGELOG.md index 5184cd6..fb26148 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/zerobounce/base_request.rb b/lib/zerobounce/base_request.rb index 5c5a758..a3713e1 100644 --- a/lib/zerobounce/base_request.rb +++ b/lib/zerobounce/base_request.rb @@ -343,7 +343,9 @@ 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 @@ -351,10 +353,15 @@ def self.__parse_json_response__(response) 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) diff --git a/lib/zerobounce/priority3.rb b/lib/zerobounce/priority3.rb index 55c17ea..3849103 100644 --- a/lib/zerobounce/priority3.rb +++ b/lib/zerobounce/priority3.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'erb' +require 'zerobounce/strict_bool' module Zerobounce module Priority3 @@ -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. diff --git a/lib/zerobounce/request.rb b/lib/zerobounce/request.rb index bda1ae5..d459724 100644 --- a/lib/zerobounce/request.rb +++ b/lib/zerobounce/request.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require 'stringio' require 'zerobounce/base_request' require 'zerobounce/get_file_helper' @@ -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']) @@ -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 @@ -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) @@ -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) diff --git a/lib/zerobounce/strict_bool.rb b/lib/zerobounce/strict_bool.rb new file mode 100644 index 0000000..1176156 --- /dev/null +++ b/lib/zerobounce/strict_bool.rb @@ -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 diff --git a/lib/zerobounce/warmup.rb b/lib/zerobounce/warmup.rb index 19f7766..61056bf 100644 --- a/lib/zerobounce/warmup.rb +++ b/lib/zerobounce/warmup.rb @@ -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 @@ -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) diff --git a/spec/transport_guards_spec.rb b/spec/transport_guards_spec.rb index 15822fc..beb9f25 100644 --- a/spec/transport_guards_spec.rb +++ b/spec/transport_guards_spec.rb @@ -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 = '