Back

Step by Step Guide to Building a UKG Pro API Integration in Ruby

Aug 11, 20246 minute read

Hey there, fellow Ruby enthusiast! Ready to dive into the world of UKG Pro API integration? Buckle up, because we're about to embark on a journey that'll have you pulling employee data like a pro in no time.

Introduction

UKG Pro's API is a powerhouse for accessing workforce management data. Whether you're building a custom dashboard or syncing employee info, this guide will get you up and running faster than you can say "Ruby is awesome."

Prerequisites

Before we jump in, make sure you've got:

  • Ruby 2.7+ (because who doesn't love the latest and greatest?)
  • Your favorite HTTP client gem (we'll be using faraday)
  • json gem for parsing responses
  • UKG Pro API credentials (you know, the keys to the kingdom)

Setting Up the Project

Let's kick things off:

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!

Authentication

UKG Pro uses OAuth 2.0, so let's get that token:

require 'faraday' require 'json' client_id = 'your_client_id' client_secret = 'your_client_secret' conn = Faraday.new(url: 'https://api.ultipro.com') response = conn.post('/authentication/token') do |req| req.body = { client_id: client_id, client_secret: client_secret, grant_type: 'client_credentials' } end token = JSON.parse(response.body)['access_token']

Pro tip: Store that token securely and refresh it when needed. Your future self will thank you.

Making API Requests

Now for the fun part – let's grab some data:

api_conn = Faraday.new( url: 'https://api.ultipro.com', headers: { 'Authorization' => "Bearer #{token}" } ) response = api_conn.get('/personnel/v1/employees') employees = JSON.parse(response.body)

Boom! You've just pulled employee data. Feel the power!

Error Handling

API calls can be fickle beasts. Tame them with some error handling:

begin response = api_conn.get('/personnel/v1/employees') raise "API Error: #{response.status}" unless response.success? employees = JSON.parse(response.body) rescue Faraday::Error => e puts "Connection error: #{e.message}" rescue JSON::ParserError => e puts "JSON parsing error: #{e.message}" rescue StandardError => e puts "Unexpected error: #{e.message}" end

Data Parsing and Manipulation

Got the data? Great! Now let's make it dance:

active_employees = employees.select { |e| e['employmentStatus'] == 'Active' } employee_names = active_employees.map { |e| "#{e['firstName']} #{e['lastName']}" }

Building Reusable Components

Let's wrap this up in a neat little package:

class UKGProClient def initialize(client_id, client_secret) @client_id = client_id @client_secret = client_secret @token = nil end def get_employees authenticate if @token.nil? response = api_conn.get('/personnel/v1/employees') JSON.parse(response.body) end private def authenticate # Authentication code here end def api_conn # Faraday connection setup here end end

Now you're cooking with gas!

Testing

Don't forget to test your code. RSpec is your friend:

RSpec.describe UKGProClient do let(:client) { UKGProClient.new('fake_id', 'fake_secret') } it 'fetches employees' do VCR.use_cassette('employees') do employees = client.get_employees expect(employees).to be_an(Array) expect(employees.first).to have_key('firstName') end end end

Best Practices

  • Respect rate limits – UKG Pro isn't a fan of spam.
  • Log API calls for debugging (but keep those logs secure!).
  • Never, ever hardcode credentials. Use environment variables or a secure vault.

Conclusion

And there you have it! You're now armed and dangerous with UKG Pro API integration skills. Remember, with great power comes great responsibility – use your newfound abilities wisely.

Happy coding, and may your API calls always return 200 OK!