Skip to content

Commit

Permalink
handle creating candidate that already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
mpapis committed Apr 16, 2015
1 parent 029f546 commit 96f59f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lib/workable/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,36 @@ def do_request(url, type, &block)
# parse the api response
def parse!(response)
case response.code.to_i
when 204, 205
when 204, 205 # handled no response
nil
when 200...300
when 200...300 # handled with response
JSON.parse(response.body)
when 401
raise Errors::NotAuthorized, response.body
raise Errors::NotAuthorized, JSON.parse(response.body)["error"]
when 404
raise Errors::NotFound, response.body
raise Errors::NotFound, JSON.parse(response.body)["error"]
when 422
handle_response_422(response)
when 503
raise Errors::RequestToLong, response.body
else
raise Errors::InvalidResponse, "Response code: #{response.code} message: #{response.body}"
end
end

def handle_response_422(response)
data = JSON.parse(response.body)
if
data["validation_errors"] &&
data["validation_errors"]["email"] &&
data["validation_errors"]["email"].include?("candidate already exists")
then
raise Errors::AlreadyExists, data["error"]
else
raise Errors::NotFound, data["error"]
end
end

# default headers for authentication and JSON support
def headers
{
Expand Down
1 change: 1 addition & 0 deletions lib/workable/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class InvalidConfiguration < WorkableError; end
class NotAuthorized < WorkableError; end
class InvalidResponse < WorkableError; end
class NotFound < WorkableError; end
class AlreadyExists < WorkableError; end
class RequestToLong < WorkableError; end
end
end

0 comments on commit 96f59f0

Please sign in to comment.