Hey there, fellow developer! Ready to dive into the world of UKG Pro Workforce Management API integration? Buckle up, because we're about to embark on a Ruby-powered journey that'll have you managing workforce data like a pro in no time.
UKG Pro's Workforce Management API is a powerhouse for handling employee data, time tracking, and scheduling. We're going to build an integration that'll make your HR department jump for joy. Trust me, they'll love you for this.
Before we jump in, make sure you've got:
httparty
, json
, and dotenv
Got all that? Great! Let's get this show on the road.
First things first, we need to get that sweet, sweet access token. Here's how:
require 'httparty' require 'json' require 'dotenv/load' def get_access_token response = HTTParty.post( "#{ENV['UKG_BASE_URL']}/auth/token", headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }, body: { grant_type: 'client_credentials', client_id: ENV['UKG_CLIENT_ID'], client_secret: ENV['UKG_CLIENT_SECRET'] } ) JSON.parse(response.body)['access_token'] end
Pro tip: Store your token and check its expiration. No one likes a stale token!
Now that we're authenticated, let's set up our basic request structure:
def make_api_request(endpoint, method = :get, body = nil) url = "#{ENV['UKG_BASE_URL']}/#{endpoint}" headers = { 'Authorization' => "Bearer #{get_access_token}", 'Content-Type' => 'application/json' } HTTParty.send(method, url, headers: headers, body: body&.to_json) end
Time to get our hands dirty with some real data:
# Get employee data def get_employee(employee_id) make_api_request("employees/#{employee_id}") end # Record time punch def record_time_punch(employee_id, punch_type) make_api_request('time-management/punches', :post, { employeeId: employee_id, punchType: punch_type, punchTime: Time.now.iso8601 }) end # Get employee schedule def get_schedule(employee_id, start_date, end_date) make_api_request("schedules?employeeId=#{employee_id}&startDate=#{start_date}&endDate=#{end_date}") end
Don't let those pesky errors catch you off guard:
def handle_api_response(response) case response.code when 200..299 JSON.parse(response.body) else logger.error("API Error: #{response.code} - #{response.body}") raise "API Error: #{response.code}" end end
Let's make sense of all this data:
def process_employee_data(employee_data) # Your data processing logic here # Maybe store it in a database? end
Time to wrap it all up in a nice, tidy class:
class UKGIntegration def initialize # Initialize with your config end def get_employee(id) # Your method here end # Add other methods... end
Don't forget to test! Here's a quick example using RSpec:
RSpec.describe UKGIntegration do let(:integration) { UKGIntegration.new } it "fetches employee data" do employee = integration.get_employee('12345') expect(employee).to have_key('firstName') end end
Keep things speedy with some caching:
def get_access_token return @token if @token && @token_expiry > Time.now # Get new token if expired end
Keep those secrets secret! Use environment variables and never, ever commit sensitive data to your repo.
And there you have it! You've just built a robust UKG Pro Workforce Management API integration in Ruby. Pat yourself on the back, you coding wizard!
Remember, this is just the beginning. There's always more to explore and optimize. Keep experimenting, keep learning, and most importantly, keep coding!
Now go forth and manage that workforce data like a boss! 🚀