Hey there, fellow Ruby enthusiast! Ready to dive into the world of UKG Pro Recruiting API integration? Let's roll up our sleeves and get coding!
UKG Pro Recruiting API is a powerful tool that lets you tap into a wealth of recruitment data. We're going to build an integration that'll make your recruiting process smoother than a freshly waxed surfboard.
Before we jump in, make sure you've got:
faraday
)json
gem for parsing responsesLet's get this party started:
mkdir ukg_pro_integration cd ukg_pro_integration bundle init
Now, crack open that Gemfile and add:
gem 'faraday' gem 'json'
Run bundle install
, and we're off to the races!
First things first, let's get that access token:
require 'faraday' require 'json' class UKGProClient BASE_URL = 'https://api.ultipro.com' def initialize(client_id, client_secret) @client_id = client_id @client_secret = client_secret end def authenticate response = Faraday.post("#{BASE_URL}/oauth/token") do |req| req.body = { grant_type: 'client_credentials', client_id: @client_id, client_secret: @client_secret } end JSON.parse(response.body)['access_token'] end end
Pro tip: In a real-world scenario, you'd want to handle token expiration and refreshing. But let's keep it simple for now, shall we?
Now that we're authenticated, let's make some noise:
def get_job_listings access_token = authenticate response = Faraday.get("#{BASE_URL}/recruiting/v1/job-openings") do |req| req.headers['Authorization'] = "Bearer #{access_token}" end JSON.parse(response.body) end
Let's add a method to submit applications:
def submit_application(job_id, candidate_data) access_token = authenticate response = Faraday.post("#{BASE_URL}/recruiting/v1/applications") do |req| req.headers['Authorization'] = "Bearer #{access_token}" req.headers['Content-Type'] = 'application/json' req.body = { jobOpeningId: job_id, candidateData: candidate_data }.to_json end JSON.parse(response.body) end
Don't let those pesky errors catch you off guard:
def api_request(method, endpoint, params = {}) response = Faraday.send(method, "#{BASE_URL}#{endpoint}") do |req| req.headers['Authorization'] = "Bearer #{authenticate}" req.body = params.to_json if [:post, :put].include?(method) end handle_response(response) rescue Faraday::Error => e log_error("API request failed: #{e.message}") raise "API request failed: #{e.message}" end def handle_response(response) case response.status when 200..299 JSON.parse(response.body) else log_error("API error: #{response.status} - #{response.body}") raise "API error: #{response.status} - #{response.body}" end end def log_error(message) # Implement your logging logic here puts "ERROR: #{message}" end
Time to put our code through its paces:
require 'minitest/autorun' require 'webmock/minitest' class UKGProClientTest < Minitest::Test def setup @client = UKGProClient.new('fake_id', 'fake_secret') end def test_authenticate stub_request(:post, "#{UKGProClient::BASE_URL}/oauth/token") .to_return(status: 200, body: '{"access_token":"fake_token"}') assert_equal 'fake_token', @client.authenticate end # Add more tests for other methods end
Remember, with great power comes great responsibility:
And there you have it, folks! You've just built a lean, mean, UKG Pro Recruiting API integration machine. Remember, this is just the tip of the iceberg. There's a whole world of recruitment data out there waiting for you to explore.
Now go forth and recruit like a boss! 🚀