From 97dc0db0b0f93ddef9a2a4551385801017ab8e65 Mon Sep 17 00:00:00 2001 From: Fabien Piette Date: Wed, 15 May 2024 12:05:27 +0200 Subject: [PATCH 1/2] feat(integrations): manage string body that is not a json, liv-12186 --- lib/pipedrive/base.rb | 18 +++++++++++++++--- spec/lib/pipedrive/base_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/pipedrive/base.rb b/lib/pipedrive/base.rb index 2660380..8f3086b 100644 --- a/lib/pipedrive/base.rb +++ b/lib/pipedrive/base.rb @@ -56,12 +56,24 @@ def process_response(res) def response_body(json) JSON.parse(json) rescue StandardError - {} + if json.present? + { body: json } + else + {} + end end def failed_response(res) - failed_res = res.body.merge(success: false, not_authorized: false, - failed: false).merge(res.headers) + failed_res = if res.body.is_a?(::Hashie::Mash) + res.body + else + ::Hashie::Mash.new(response_body(res.body)) + end + + failed_res.merge!(success: false, not_authorized: false, failed: false) + failed_res.merge!(res.headers) + + # failed_res = res.body.merge(success: false, not_authorized: false, failed: false).merge(res.headers) case res.status when 401 failed_res[:not_authorized] = true diff --git a/spec/lib/pipedrive/base_spec.rb b/spec/lib/pipedrive/base_spec.rb index c5c0e13..e012fdd 100644 --- a/spec/lib/pipedrive/base_spec.rb +++ b/spec/lib/pipedrive/base_spec.rb @@ -41,6 +41,27 @@ ) end + context "when the body is a string" do + let(:res) do + instance_double( + Faraday::Response, + body: "Too many requests", + status: 429, + headers: ::Hashie::Mash.new({foo: :bar}) + ) + end + + it "returns an error hash" do + expect(subject).to eq(::Hashie::Mash.new({ + body: "Too many requests", + failed: false, + not_authorized: false, + success: false, + foo: :bar, + })) + end + end + context "status is 401" do let(:status) { 401 } From 147dba37ff13816bfe506c7fb0acfb9a2aacbba0 Mon Sep 17 00:00:00 2001 From: Fabien Piette Date: Wed, 15 May 2024 12:07:22 +0200 Subject: [PATCH 2/2] style: remove comment, liv-12186 --- lib/pipedrive/base.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/pipedrive/base.rb b/lib/pipedrive/base.rb index 8f3086b..347303d 100644 --- a/lib/pipedrive/base.rb +++ b/lib/pipedrive/base.rb @@ -73,7 +73,6 @@ def failed_response(res) failed_res.merge!(success: false, not_authorized: false, failed: false) failed_res.merge!(res.headers) - # failed_res = res.body.merge(success: false, not_authorized: false, failed: false).merge(res.headers) case res.status when 401 failed_res[:not_authorized] = true