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.
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."
Before we jump in, make sure you've got:
faraday
)json
gem for parsing responsesLet'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!
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.
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!
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
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']}" }
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!
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
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!