Skip to content

Instantly share code, notes, and snippets.

@kerlin
Last active November 17, 2021 02:03
Show Gist options
  • Save kerlin/a1c2659fbbb37205d5da5e474ae7b286 to your computer and use it in GitHub Desktop.
Save kerlin/a1c2659fbbb37205d5da5e474ae7b286 to your computer and use it in GitHub Desktop.

Revisions

  1. kerlin revised this gist Oct 27, 2017. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions get_workers.rb
    Original file line number Diff line number Diff line change
    @@ -5,11 +5,12 @@
    # 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.
    # 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.
    # 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'
  2. kerlin created this gist Oct 26, 2017.
    108 changes: 108 additions & 0 deletions get_workers.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,108 @@
    #!/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.
    #
    # 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.
    #

    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