Skip to content

Commit 49f292d

Browse files
committed
Merge pull request #130 from intercom/jo/nil-guard-on-decoded_body
nil guard on decoded body
2 parents 362d291 + 232b3c8 commit 49f292d

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

lib/intercom/request.rb

+6-7
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,13 @@ def decode_body(response)
8181

8282
def parse_body(decoded_body, response)
8383
parsed_body = nil
84-
unless decoded_body.strip.empty?
85-
begin
86-
parsed_body = JSON.parse(decoded_body)
87-
rescue JSON::ParserError => e
88-
raise_errors_on_failure(response)
89-
end
90-
raise_application_errors_on_failure(parsed_body, response.code.to_i) if parsed_body['type'] == 'error.list'
84+
return parsed_body if decoded_body.nil? || decoded_body.strip.empty?
85+
begin
86+
parsed_body = JSON.parse(decoded_body)
87+
rescue JSON::ParserError => e
88+
raise_errors_on_failure(response)
9189
end
90+
raise_application_errors_on_failure(parsed_body, response.code.to_i) if parsed_body['type'] == 'error.list'
9291
parsed_body
9392
end
9493

spec/unit/intercom/request_spec.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@
77
req = Intercom::Request.new('path/', 'GET')
88
proc {req.parse_body('<html>somethjing</html>', response)}.must_raise(Intercom::ServerError)
99
end
10-
end
10+
11+
it 'parse_body returns nil if decoded_body is nil' do
12+
response = OpenStruct.new(:code => 500)
13+
req = Intercom::Request.new('path/', 'GET')
14+
req.parse_body(nil, response).must_equal(nil)
15+
end
16+
end

0 commit comments

Comments
 (0)