Skip to content

Commit

Permalink
Merge pull request #256 from mailjet/fix-yajl-back-compatibility-with…
Browse files Browse the repository at this point in the history
…-json

fix yajl gem bug with backward compatibility to json
  • Loading branch information
mgrishko authored Apr 16, 2024
2 parents f8855ea + 6474a77 commit 221e4b0
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 18 deletions.
10 changes: 5 additions & 5 deletions lib/mailjet/connection.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'rest_client'
require 'yajl/json_gem'
require 'yajl'

module Mailjet
class Connection
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions lib/mailjet/exception/errors.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'yajl/json_gem'

module Mailjet
class Error < StandardError
attr_reader :object
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/mailjet/rack/endpoint.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'rack/request'
require 'yajl/json_gem'
require 'yajl'

module Mailjet
module Rack
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions lib/mailjet/resource.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/mailjet/api_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"<html>\\n<head><title>404 Not Found</title></head>\\n<body bgcolor=\\"white\\">\\n<center><h1>404 Not Found</h1></center>\\n<hr><center>nginx</center>\\n</body>\\n</html>"
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
Expand Down
5 changes: 3 additions & 2 deletions spec/mailjet_spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "bundler/setup"
require "mailjet"
require "dotenv"
require 'yajl'
require "vcr"
Bundler.setup

Expand All @@ -27,7 +28,7 @@

c.filter_sensitive_data("<PUBLIC_KEY>") do |interaction|
begin
body = JSON.parse(interaction.response.body)
body = Yajl::Parser.parse(interaction.response.body)
data = body.fetch("Data", []).first || {}

data["APIKey"]
Expand All @@ -38,7 +39,7 @@

c.filter_sensitive_data("<PRIVATE_KEY>") do |interaction|
begin
body = JSON.parse(interaction.response.body)
body = Yajl::Parser.parse(interaction.response.body)
data = body.fetch("Data", []).first || {}

data["SecretKey"]
Expand Down

0 comments on commit 221e4b0

Please sign in to comment.