Skip to content

Instantly share code, notes, and snippets.

@veekram
Forked from kerlin/get_workers.rb
Created May 3, 2018 12:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veekram/c97ffb2ee9026910d0ec2e2f6f8e800a to your computer and use it in GitHub Desktop.
Save veekram/c97ffb2ee9026910d0ec2e2f6f8e800a to your computer and use it in GitHub Desktop.
Calling the Workday API in Ruby with Savon
#!/usr/bin/env ruby
#
# Example Ruby CLI script to retrieve worker data from Workday
# Call on command line with worker id; prints worker name
# add "request" or "response" after worker id and prints the
# outgoing xml or received hash and exits.
#
# Using Savon version 2 for the SOAP client (2.11.2)
#
# Savon defaults to making the message tag the same as the operation name
# and Workday needs them to be different.
# Also, the namespace for the message is different from the one
# Savon uses by default. (maybe https://github.com/savonrb/savon/issues/830)
#
require 'savon'
require 'base64'
require 'pp'
require 'rexml/document'
class WDClient
def initialize(emp_id)
wd_tenant = ENV["WD_TENANT"]
wd_user = "#{ENV['WD_ID']}@#{wd_tenant}"
wd_pass = ENV["WD_VALIDATION"]
wsdl_url = "https://services1.myworkday.com/ccx/service/#{wd_tenant}/Human_Resources/v27.2?wsdl"
@client = Savon.client(wsdl: wsdl_url, wsse_auth: [wd_user, wd_pass], convert_request_keys_to: :none, env_namespace: :soapenv, namespace_identifier: :ins0 )
@message = {
:Request_References => {
:Worker_Reference => {
:ID => emp_id,
:attributes! => {
:ID => {
:"ins0:type" => "Employee_ID"
}
}
}
}
}
@operation = :get_workers
@message_tag = :Get_Workers_Request
@message_attributes = {
:"ins0:version" => "v27.2"
}
end
def get_response
begin
response = @client.call(@operation, message: @message, message_tag: @message_tag, attributes: @message_attributes )
rescue Savon::SOAPFault => error
puts error.inspect
exit
#raise
end
return response
end
def operations
# a list of operations the client can perform
@client.operations
end
def request_xml
# just build the xml for debugging
# template is the xml with default message tag and empty message content
request_template = @client.operation(@operation)
# build the request xml
request = request_template.build(message: @message, message_tag: @message_tag, attributes: @message_attributes).to_s
end
end
emp_id = ARGV[0]
unless emp_id =~ /^\A[A-Z]\d{8}\Z/
puts "usage: #{File.basename($0)} EmployeeID [request|response]"
exit
end
workday = WDClient.new(emp_id)
if ARGV[1] == 'request'
REXML::Document.new(workday.request_xml).write($stdout, indent_spaces = 2)
puts "\n"
exit
end
response = workday.get_response
if ARGV[1] == 'response'
pp response.body
exit
end
if response.body[:get_workers_response][:response_results][:total_results] == '1'
data = response.body[:get_workers_response][:response_data][:worker][:worker_data][:personal_data][:name_data][:preferred_name_data][:name_detail_data][:"@wd:formatted_name"]
puts "ID: #{emp_id} Name: #{data}"
else
puts "No Data Found for #{emp_id}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment