Using TargetProcess SOAP API in Ruby Application


It is quite easy to access TargetProcess SOAP API via Ruby.

Install soap4r

First thing you need to install soap4r. SOAP4R is a Ruby library for accessing Web Services via SOAP.

gem install soap4r --include-dependencies

Generate Classes From WSDL

For example, you want to use ProjectService API. You have to generate some classes using wsdl2ruby.

[path_to_wdsl2ruby]wsdl2ruby.rb –wsdl [targetprocess_url]/Services/ProjectService.asmx?WSDL –type client

Real command may look like:

/Library/Ruby/Gems/1.8/gems/soap4r-1.5.8/bin/wsdl2ruby.rb –wsdl http://localhost/targetprocess2/Services/ProjectService.asmx?WSDL –type client

This command will generate 4 classes. You may rename them if you want, in this example we use them as is.

default.rb
defaultDriver.rb
defaultMappingRegistry.rb
ProjectServiceClient.rb

Authentication

TargetProcess uses WSE, so it is required to provide correct authentication. First, create the following class in wsse_authentication.rb file in lib folder. Specify correct login and password. You may use System User credentials (login into TargetProcess and navigate to Admin section to set them).

require 'soap/header/simplehandler'

class WsseAuthHeader < SOAP::Header::SimpleHandler
  NAMESPACE = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'
  USERNAME  = 'admin'
  PASSWORD  = 'admin_password'

  def initialize()
    super(XSD::QName.new(NAMESPACE, 'Security'))
  end

  def on_simple_outbound
    {"UsernameToken" => {"Username" => USERNAME, "Password" => PASSWORD}}
  end
end

Usage

Let’s assume you have DashboardController in your application and want to show specific project name. Here is the code that will extract project name via web services.

require "defaultDriver.rb"
require 'wsse_authentication.rb'

class DashboardController < ApplicationController
  def index
    driver = ProjectServiceSoap.new     

    #Add authentication
    driver.headerhandler << WsseAuthHeader.new()
    # Print project name
    project = driver.getByID(:id => 2)
    @project_name = project.getByIDResult.name
  end
end

blog comments powered by Disqus