/ informatica

Interrogare un servizio SOAP con Ruby

Come spesso accade Windows & Co. fanno le cose a modo loro. àˆ il caso del web service SOAP (quando REST funziona bene e anche meglio) che, causa lavoro, ho dovuto interrogare. Per farlo ho utilizzato Ruby con l'aiuto della libreria soap/wsdlDriver.

Qui di seguito un codice di esempio per connettersi al web service e gestire la risposta alla chiamata a getCourses208913

Scarica il file SOAP-wsdlDriver.rb

#
# SOAP service query example
#
# Usage:
#
# course = CourseManagement.new
# course.getCourses
#
# Author::    Tommaso Visconti  (mailto:tommaso.visconti@kreations.it)
# Copyright:: Copyright (c) 2009 Kreations Agency
# License::   General Public License v.2

require 'soap/wsdlDriver'

class CourseManagement

  # Initialize the SOAP service
	def initialize
		@SOAPUrl = "http://HOST_URL/ws/ws208913.asmx?WSDL"
		@service=SOAP::WSDLDriverFactory.new(@SOAPUrl).create_rpc_driver
		@service.generate_explicit_type = true
		@service.wiredump_dev = STDOUT if $DEBUG
	end

	# Get the possible callbacks
	def getMethods
	 @service.methods.sort
	end

	#
	# Get the courses
	#
	# The first call use :IDParent => 0 which indicates the root of the tree
	#
	def getCourses
		result = @service.getCourses208913(:IDParent => 0)

		if result.getCourses208913Result.to_a.size > 0
			puts "# Code => " + result.getCourses208913Result.codeDescription.code
			puts "# Description => " + result.getCourses208913Result.codeDescription.description
			puts

			self.getCourse(result.getCourses208913Result.codeDescription.code, 1)
		end
	end

	def getCourse(idParent, count)
		result = @service.getCourses208913(:IDParent => idParent)

		if result.getCourses208913Result.to_a.size > 0

			result.getCourses208913Result.codeDescription.each do |course|
				puts " "*count*2 + "# Code => " + course.code
				puts " "*count*2 + "# Description => " + course.description
				puts

				# Recoursive call
				self.getCourse(course.code, count+1) if result.getCourses208913Result.to_a.size > 1
			end
		end
	end
end