Skip to content

Commit 007caef

Browse files
Merge pull request #7 from Springest/validation
Check response status for every request to not fail silently
2 parents 7f3b0fd + 08ae834 commit 007caef

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

Diff for: lib/help_scout.rb

+16-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ class HelpScout
55
class ValidationError < StandardError; end
66
class NotImplementedError < StandardError; end
77

8+
# Status codes used by Help Scout, not all are implemented in this gem yet.
9+
# http://developer.helpscout.net/help-desk-api/status-codes/
10+
HTTP_OK = 200
811
HTTP_CREATED = 201
12+
HTTP_NO_CONTENT = 204
913
HTTP_BAD_REQUEST = 400
1014

1115
attr_accessor :last_response
@@ -24,16 +28,9 @@ def initialize(api_key)
2428
def create_conversation(data)
2529
post("conversations", { body: data })
2630

27-
if last_response.code == HTTP_CREATED
28-
# Extract ID of created conversation from the Location header
29-
conversation_uri = last_response.headers["location"]
30-
return conversation_uri.match(/(\d+)\.json$/)[1]
31-
elsif last_response.code == HTTP_BAD_REQUEST
32-
# Validation failed so return the errors
33-
raise ValidationError, last_response.parsed_response["message"]
34-
else
35-
raise NotImplementedError, "Help Scout returned something that is not implemented by the help_scout gem yet: #{last_response.code}: #{last_response.parsed_response["message"] if last_response.parsed_response}"
36-
end
31+
# Extract ID of created conversation from the Location header
32+
conversation_uri = last_response.headers["location"]
33+
conversation_uri.match(/(\d+)\.json$/)[1]
3734
end
3835

3936
# Public: Get conversation
@@ -195,6 +192,14 @@ def request(method, path, options)
195192
}.merge(options)
196193

197194
@last_response = HTTParty.send(method, uri, options)
198-
@last_response.parsed_response
195+
196+
case @last_response.code
197+
when HTTP_OK, HTTP_CREATED, HTTP_NO_CONTENT
198+
@last_response.parsed_response
199+
when HTTP_BAD_REQUEST
200+
raise ValidationError, last_response.parsed_response["message"]
201+
else
202+
raise NotImplementedError, "Help Scout returned something that is not implemented by the help_scout gem yet: #{@last_response.code}: #{@last_response.parsed_response["message"] if @last_response.parsed_response}"
203+
end
199204
end
200205
end

0 commit comments

Comments
 (0)