Skip to content

Check response status for every request to not fail silently #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions lib/help_scout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ class HelpScout
class ValidationError < StandardError; end
class NotImplementedError < StandardError; end

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

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

if last_response.code == HTTP_CREATED
# Extract ID of created conversation from the Location header
conversation_uri = last_response.headers["location"]
return conversation_uri.match(/(\d+)\.json$/)[1]
elsif last_response.code == HTTP_BAD_REQUEST
# Validation failed so return the errors
raise ValidationError, last_response.parsed_response["message"]
else
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}"
end
# Extract ID of created conversation from the Location header
conversation_uri = last_response.headers["location"]
conversation_uri.match(/(\d+)\.json$/)[1]
end

# Public: Get conversation
Expand Down Expand Up @@ -195,6 +192,14 @@ def request(method, path, options)
}.merge(options)

@last_response = HTTParty.send(method, uri, options)
@last_response.parsed_response

case @last_response.code
when HTTP_OK, HTTP_CREATED, HTTP_NO_CONTENT
@last_response.parsed_response
when HTTP_BAD_REQUEST
raise ValidationError, last_response.parsed_response["message"]
else
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}"
end
end
end