Skip to content
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

Add ability to create or delete candidates #44

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
8 changes: 8 additions & 0 deletions lib/greenhouse_io/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 53 additions & 3 deletions lib/greenhouse_io/api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
})

Expand All @@ -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

Expand All @@ -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

Expand Down