diff --git a/lib/greenhouse_io/api.rb b/lib/greenhouse_io/api.rb index ec521de..6aca5e1 100644 --- a/lib/greenhouse_io/api.rb +++ b/lib/greenhouse_io/api.rb @@ -8,6 +8,14 @@ def post_response(url, options) self.class.post(url, options) end + def patch_response(url, options) + self.class.patch(url, options) + end + + def delete_response(url, options) + self.class.delete(url, options) + end + def parse_json(response) JSON.parse(response.body, symbolize_names: GreenhouseIo.configuration.symbolize_keys) end diff --git a/lib/greenhouse_io/api/client.rb b/lib/greenhouse_io/api/client.rb index 453d52d..7dd2ff0 100644 --- a/lib/greenhouse_io/api/client.rb +++ b/lib/greenhouse_io/api/client.rb @@ -32,6 +32,19 @@ def activity_feed(id, options = {}) get_from_harvest_api "/candidates/#{id}/activity_feed", options end + def create_candidate(candidate_hash, on_behalf_of) + post_to_harvest_api \ + "/candidates", + candidate_hash, + { 'On-Behalf-Of' => on_behalf_of.to_s } + end + + def delete_candidate(candidate_id, on_behalf_of) + delete_with_harvest_api \ + "/candidates/#{candidate_id}", + { 'On-Behalf-Of' => on_behalf_of.to_s } + end + def create_candidate_note(candidate_id, note_hash, on_behalf_of) post_to_harvest_api( "/candidates/#{candidate_id}/activity_feed/notes", @@ -96,7 +109,7 @@ def permitted_options(options) def get_from_harvest_api(url, options = {}) response = get_response(url, { - :query => permitted_options(options), + :query => permitted_options(options), :basic_auth => basic_auth }) @@ -105,7 +118,7 @@ def get_from_harvest_api(url, options = {}) if response.code == 200 parse_json(response) else - raise GreenhouseIo::Error.new(response.code) + raise GreenhouseIo::Error.new("Response code: #{response.code}, message: #{response.body}") end end @@ -120,8 +133,45 @@ def post_to_harvest_api(url, body, headers) if response.code == 200 parse_json(response) + elsif response.code == 201 + parse_json(response) + else + raise GreenhouseIo::Error.new("Response code: #{response.code}, message: #{response.body}") + end + end + + def patch_with_harvest_api(url, body, headers) + response = patch_response(url, { + :body => JSON.dump(body), + :basic_auth => basic_auth, + :headers => headers + }) + + set_headers_info(response.headers) + + if response.code == 200 + parse_json(response) + elsif response.code == 201 + parse_json(response) + else + raise GreenhouseIo::Error.new("Response code: #{response.code}, message: #{response.body}") + end + end + + def delete_with_harvest_api(url, headers) + response = delete_response(url, { + :basic_auth => basic_auth, + :headers => headers + }) + + set_headers_info(response.headers) + + if response.code == 200 + parse_json(response) + elsif response.code == 201 + parse_json(response) else - raise GreenhouseIo::Error.new(response.code) + raise GreenhouseIo::Error.new("Response code: #{response.code}, message: #{response.body}") end end