From 6474a77f2f1b6b53d5c623cbf99bcf29c03b970a Mon Sep 17 00:00:00 2001 From: Mikhail Grishko Date: Wed, 10 Apr 2024 12:37:35 +0300 Subject: [PATCH] fix yajl gem bug with backward compatibility to json --- lib/mailjet/connection.rb | 10 +++++----- lib/mailjet/exception/errors.rb | 4 +--- lib/mailjet/rack/endpoint.rb | 4 ++-- lib/mailjet/resource.rb | 10 +++++----- spec/mailjet/api_error_spec.rb | 2 +- spec/mailjet_spec_helper.rb | 5 +++-- 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/mailjet/connection.rb b/lib/mailjet/connection.rb index a48488f..9eb0b3d 100644 --- a/lib/mailjet/connection.rb +++ b/lib/mailjet/connection.rb @@ -1,5 +1,5 @@ require 'rest_client' -require 'yajl/json_gem' +require 'yajl' module Mailjet class Connection @@ -69,7 +69,7 @@ def url private def handle_api_call(method, additional_headers = {}, payload = {}, &block) - formatted_payload = (additional_headers[:content_type] == :json) ? payload.to_json : payload + formatted_payload = (additional_headers[:content_type] == :json) ? Yajl::Encoder.encode(payload) : payload raise Mailjet::MethodNotAllowed unless method_allowed(method) if self.perform_api_call @@ -79,7 +79,7 @@ def handle_api_call(method, additional_headers = {}, payload = {}, &block) @adapter.send(method, formatted_payload, additional_headers, &block) end else - return {'Count' => 0, 'Data' => [mock_api_call: true], 'Total' => 0}.to_json + return Yajl::Encoder.encode({'Count' => 0, 'Data' => [mock_api_call: true], 'Total' => 0}) end rescue RestClient::Exception => e handle_exception(e, additional_headers, formatted_payload) @@ -94,7 +94,7 @@ def handle_exception(e, additional_headers, payload = {}) return e.http_body if e.http_headers[:content_type].include?("text/plain") params = additional_headers[:params] || {} - formatted_payload = (additional_headers[:content_type] == :json) ? JSON.parse(payload) : payload + formatted_payload = (additional_headers[:content_type] == :json) ? Yajl::Parser.parse(payload) : payload params = params.merge!(formatted_payload) if formatted_payload.is_a?(Hash) http_body = if e.http_headers[:content_type].include?("application/json") @@ -128,7 +128,7 @@ def sent_invalid_email?(error_http_body, url) return false unless url.include?('v3.1/send') return unless error_http_body - parsed_body = JSON.parse(error_http_body) + parsed_body = Yajl::Parser.parse(error_http_body) error_message = parsed_body.dig('Messages')&.first&.dig('Errors')&.first&.dig('ErrorMessage') || [] error_message.include?('is an invalid email address.') rescue diff --git a/lib/mailjet/exception/errors.rb b/lib/mailjet/exception/errors.rb index 6dbad49..eb2ac42 100644 --- a/lib/mailjet/exception/errors.rb +++ b/lib/mailjet/exception/errors.rb @@ -1,5 +1,3 @@ -require 'yajl/json_gem' - module Mailjet class Error < StandardError attr_reader :object @@ -54,7 +52,7 @@ def initialize(message = nil, response = nil) end api_message = begin - JSON.parse(response.body)['ErrorMessage'] + Yajl::Parser.parse(response.body)['ErrorMessage'] rescue Yajl::ParseError response.body rescue NoMethodError diff --git a/lib/mailjet/rack/endpoint.rb b/lib/mailjet/rack/endpoint.rb index 8180641..97b85ef 100644 --- a/lib/mailjet/rack/endpoint.rb +++ b/lib/mailjet/rack/endpoint.rb @@ -1,5 +1,5 @@ require 'rack/request' -require 'yajl/json_gem' +require 'yajl' module Mailjet module Rack @@ -12,7 +12,7 @@ def initialize(app, path, &block) def call(env) if env['PATH_INFO'] == @path && (content = env['rack.input'].read) - @block.call(JSON.parse(content)) + @block.call(Yajl::Parser.parse(content)) [200, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, []] else @app.call(env) diff --git a/lib/mailjet/resource.rb b/lib/mailjet/resource.rb index 2fc5d91..c63862e 100644 --- a/lib/mailjet/resource.rb +++ b/lib/mailjet/resource.rb @@ -1,5 +1,5 @@ require 'mailjet/connection' -require 'yajl/json_gem' +require 'yajl' require 'active_support' require 'active_support/core_ext/string' require 'active_support/core_ext/hash/indifferent_access' @@ -78,7 +78,7 @@ def all(params = {}, options = {}) def count(options = {}) opts = define_options(options) response_json = connection(opts).get(default_headers.merge!(params: {limit: 1, countrecords: 1})) - response_hash = JSON.parse(response_json) + response_hash = Yajl::Parser.parse(response_json) response_hash['Total'] rescue Mailjet::ApiError => error raise error @@ -140,7 +140,7 @@ def send_data(id, binary_data = nil, options = {}) opts = define_options(options) self.resource_path = create_action_resource_path(id) if self.action - response_hash = JSON.parse(connection(opts).post(binary_data, default_headers)) + response_hash = Yajl::Parser.parse(connection(opts).post(binary_data, default_headers)) response_hash['ID'] ? response_hash['ID'] : response_hash end @@ -157,7 +157,7 @@ def instanciate_from_api(attributes = {}) end def parse_api_json(response_json) - response_hash = JSON.parse(response_json) + response_hash = Yajl::Parser.parse(response_json) #Take the response from the API and put it through a method -- taken from the ActiveSupport library -- which converts #the date-time from "2014-05-19T15:31:09Z" to "Mon, 19 May 2014 15:31:09 +0000" format. @@ -284,7 +284,7 @@ def save(options = {}) if opts[:perform_api_call] && !persisted? # get attributes only for entity creation self.attributes = if self.resource_path == 'send' - JSON.parse(response) + Yajl::Parser.parse(response) else parse_api_json(response).first end diff --git a/spec/mailjet/api_error_spec.rb b/spec/mailjet/api_error_spec.rb index 6a72398..a870719 100644 --- a/spec/mailjet/api_error_spec.rb +++ b/spec/mailjet/api_error_spec.rb @@ -55,7 +55,7 @@ "\\n404 Not Found\\n\\n

404 Not Found

\\n
nginx
\\n\\n" -Please see https://dev.mailjet.com/email/reference/overview/errors/for more informations on error numbers. +Please see https://dev.mailjet.com/email/reference/overview/errors/ for more informations on error numbers. MESSAGE end diff --git a/spec/mailjet_spec_helper.rb b/spec/mailjet_spec_helper.rb index 3435784..e608ddb 100644 --- a/spec/mailjet_spec_helper.rb +++ b/spec/mailjet_spec_helper.rb @@ -1,6 +1,7 @@ require "bundler/setup" require "mailjet" require "dotenv" +require 'yajl' require "vcr" Bundler.setup @@ -27,7 +28,7 @@ c.filter_sensitive_data("") do |interaction| begin - body = JSON.parse(interaction.response.body) + body = Yajl::Parser.parse(interaction.response.body) data = body.fetch("Data", []).first || {} data["APIKey"] @@ -38,7 +39,7 @@ c.filter_sensitive_data("") do |interaction| begin - body = JSON.parse(interaction.response.body) + body = Yajl::Parser.parse(interaction.response.body) data = body.fetch("Data", []).first || {} data["SecretKey"]