Skip to content

Commit 299b130

Browse files
committed
refactor: Extract body validation
1 parent 407edfc commit 299b130

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

lib/octokit/error.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,8 @@ def response_body
162162

163163
def data
164164
@data ||=
165-
if (body = @response[:body]) && !body.strip.empty? && body.start_with?("{")
166-
if body.is_a?(String) &&
167-
@response[:response_headers] &&
168-
@response[:response_headers][:content_type] =~ /json/
165+
if (body = @response[:body])
166+
if valid_json_body?(body)
169167

170168
Sawyer::Agent.serializer.decode(body)
171169
else
@@ -174,6 +172,14 @@ def data
174172
end
175173
end
176174

175+
def valid_json_body?(body)
176+
body.is_a?(String) &&
177+
@response[:response_headers] &&
178+
@response[:response_headers][:content_type] =~ /json/ &&
179+
!body.strip.empty? &&
180+
body.start_with?("{")
181+
end
182+
177183
def response_message
178184
case data
179185
when Hash

0 commit comments

Comments
 (0)