Skip to content

Commit

Permalink
Merge pull request #2 from livestorm/liv-12186-pipedrive-fix-nomethod…
Browse files Browse the repository at this point in the history
…error-in-pipedrive-gem-for-non-json

feat(integrations): manage string body response that is not a json, liv-12186
  • Loading branch information
fabienpiette authored May 21, 2024
2 parents 90fc624 + 147dba3 commit d72f20b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/pipedrive/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,23 @@ 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)

case res.status
when 401
failed_res[:not_authorized] = true
Expand Down
21 changes: 21 additions & 0 deletions spec/lib/pipedrive/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down

0 comments on commit d72f20b

Please sign in to comment.