The Ruby programming language is growing in popularity. In the last few years, we have seen this new dynamically typed language move from the obscurity of a small community of Linux/Unix systems administrators to the language behind Rails (Ruby on Rails, also abbreviated RoR). There are at least three new implementations of Ruby currently in development. If anyone has any doubt that Microsoft is serious about Ruby, please read this:
http://www.computerworld.com/action/article.do?command=viewArticleBasic&articleId=9091398
For now, the best stable implementation of Ruby is version 1.8.6 of the Matz Ruby Interpreter (or MRI). For more information, about Ruby itself and its history, start here:
http://www.ruby-lang.org/en/
The one-click installer is the best way to install Ruby on a Windows machine.
Most IT departments in large organizations contain a mix of systems from different vendors and some even have applications built on open source technologies. Let us assume a small (and perhaps rogue) development team has built a Ruby on Rails web application to drive a company’s community forums website? How could that integrate with K2 BlackPearl? Thanks to the power and simplicity of.NET Web Services, it’s not difficult. In theory, you could call from Ruby directly into a .NET Assembly using some of the newer bridge technologies. However, most Rails developers have found hosting Rails on Windows for production implementations to be untenable at present.
Let’s start by creating a simple ASP.NET Web Service created with Visual Studio 2005. Replace the code in the main “Service” source code file with this:
__________________________________________________________________
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using SourceCode.Workflow.Client;
[WebService(Namespace= "http://tempuri.org/")]
[WebServiceBinding(ConformsTo= WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service() {
//Uncommentthe following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string StartProcessInstance(stringProcessName, string UserName) {
string K2ServerName = "localhost";
ConnectionoCon = new Connection();
try
{
oCon.Open(K2ServerName);
}
catch(Exception eX)
{
return "Failure toopen connection to : " + K2ServerName + "\n"+
"Exception message: " +eX.Message + "AND" +eX.InnerException;
}
oCon.ImpersonateUser(UserName);
ProcessInstanceoNewProcessInstance = oCon.CreateProcessInstance(ProcessName);
try
{
oCon.StartProcessInstance(oNewProcessInstance);
}
catch(Exception eX){
return "Failed tostart process " + eX.Message + eX.InnerException;
}
oCon.Close();
oCon.Dispose();
return"Success!!";
}
}
__________________________________________________________________
Create a WebSite using Internet Information Services Manager. Enable “Allow anonymous access to this Web site” when on the Web Site Home Directory page of the IIS. Make sure the ApplicationPool Identity account used for this site is running an account with Impersonate rights in the Workflow Server -> Server Rights (setting found in K2 Workspace Management Console). Publish this WebService application to the new website.
Since we are using the ImpersonateUser method in the Workflow Client API, we only need the user name of the user starting the process.
Let’s see some Ruby code:
__________________________________________________________________
require 'soap/wsdlDriver'
#Creates a web service factory using the WSDL file of the web service (URL will be different for you app!)
factory =SOAP::WSDLDriverFactory.new("http://localhost:4000/Service.asmx?WSDL")
#Create an RPC driver object tied to that web service factory.
soap = factory.create_rpc_driver
#Call the StartProcesInstance method passing in the ProcessName and UserName values to the parameters in the webservice method
process_name = "RoleTests\\ProcessOne"
user_name = "K2DEMO\\CarolM"
soapResponse = soap.StartProcessInstance(:ProcessName =>process_name, :UserName => user_name)
#Resets the soap stream so it can be called again.
soap.reset_stream
#Outputs the web service response
puts soapResponse.startProcessInstanceResult
__________________________________________________________________
Please read the comments above and make any modifications that are required for your environment. Save the code above in a file with a “.rb” extension. Launch the windows command prompt and type in :
C:\>ruby filename.rb
This will run the ruby script (at this point, Ruby is an interpreted language). You should see some output like this:
__________________________________________________________________
ignored element: {http://schemas.xmlsoap.org/wsdl/soap12/}binding
ignored element:{http://schemas.xmlsoap.org/wsdl/soap12/}operation
ignored element:{http://schemas.xmlsoap.org/wsdl/soap12/}body
ignored element:{http://schemas.xmlsoap.org/wsdl/soap12/}address
Success!!
__________________________________________________________________
This last line is the return value from the WebService we wrote. Hopefully, it reads “Success!!” like we see above.
If you are interested in using SSL and HTTP Basic Authentication with the Ruby’s main SOAP client API, please see this blog post:
http://s2.diffuse.it/blog/show/62_Consume+SSL+protected+Web+Services+with+soap4r
--------------------------------------------------------------------------------
The statements and opinions made in my postings are my own, and do not reflect the opinions of SourceCode Technology Holdings, Inc. or its subsidiaries. All information is provided as is with no warranties, express or implied, and grants no rights or licenses.