Connecting to a SOAP service in Ruby

When connecting to services and APIs, I tend to follow the rule that simpler is always better. SOAP is one of these services where you don’t need any more complexity than strictly required. This post eventually led me on the right path.

I like that it only uses the standard library and that it simply works. No need to install any gem; it works on the largest number of systems out there. I did refactor the consumer class a little bit though, caching a few variables.

require 'soap/wsdlDriver'

class DeliveryAgent
def initialize(wsdl = 'http://path/to/wsdl.php')
@wsdl = wsdl
@soap = driver.create_rpc_driver
end

def add_recipient(user)
@soap.addRecipient(login, groups, recipient_data(user), true, false)
end

private

def driver
SOAP::WSDLDriverFactory.new @wsdl
end

def login
{
:username => 'username',
:password => 'password'
}
end

def groups
[107]
end

def recipient_data(user)
{
:email => user.email,
:voornaam => user.firstname,
:achternaam => user.lastname,
:woonplaats => user.city,
:leeftijd => user.age,
:jongen_meisje => user.gender
}
end
end