Today I had a fun trial-and-error exploration of Ruby SOAP messaging. By “fun” I mean “hair-rippingly excruciating.”
Basic SOAP calls are pretty easy with the wsdlDriver inherent in Ruby. Here’s how you’d wrap MailBuild’s Subscriber.Add function in Ruby:
class MailBuildWrapper
attr_accessor :api_key, :list_id
def initialize(api_key, list_id)
@api_key = api_key
@list_id = list_id
end
def add_subscriber(user)
soap = wsdl.create_rpc_driver
response = soap.addSubscriber \
:ApiKey => api_key,
:ListID => list_id,
:Email => user.email,
:Name => user.full_name
soap.reset_stream
response.subscriber_AddWithCustomFieldsResult
end
private
def wsdl
SOAP::WSDLDriverFactory.new("http://iridesco.createsend.com/api/api.asmx?WSDL")
end
end
Calling a simple add subscriber is, well, simple.
mb = MailBuildWrapper.new
mb.add_subscriber(u)
Adding with custom fields is different. This is because an array of “SubscriberCustomField” objects is necessary. Finding the magic Ruby incantation to build this SOAP array was a trick. I never found anywhere that said how to do this - Google was not my friend. Here is an example MailBuildWrapper method adding a subscriber with an ‘is_admin’ custom field:
soap = wsdl.create_rpc_driver
response = soap.addSubscriberWithCustomFields \
:ApiKey => api_key,
:ListID => list_id,
:Email => user.email,
:Name => user.full_name,
:CustomFields => {:SubscriberCustomField =>
[{:Key => ‘is_admin’, :Value => user.is_admin?.to_s}]}
soap.reset_stream
response.subscriber_AddWithCustomFieldsResult
end
So the ‘CustomFields’ hash item needed to point to a hash with a ‘SubscriberCustomField’ item, which itself points to an array of hashes containing ‘Key’ and ‘Value’. So multiple custom fields would be specified as follows:
:CustomFields => {:SubscriberCustomField =>
[ {:Key => ‘is_admin’, :Value => user.is_admin?.to_s},
{:Key => ‘is_loser’, :Value => user.is_loser?.to_s}]}
Some useful links from my first experience consuming a SOAP web service with Ruby:
- Consuming SOAP services in Ruby by Ryan Heath
- MailBuild API
- See raw xml in soap4r
- Ruby + SOAP4R + WSDL Hell (wsdl2ruby) by Brendon Wilson
- Getting Started with SOAP4R by Mark Thomas
- Ruby SOAP client communication with Microsoft .NET webservice
- Harvest because that’s why I was looking at this in the first place
Technorati Tags: MailBuild, Ruby, SOAP
Related posts:






Comments (4)
You beat me to it! Well..not exactly but pretty darn close. I’m doing the same thing but through CampaignMonitor rather than MailBuild.
The custom fields was my big hangup. Google was no help the LAST TIME I looked but then you made this excellent post!
Any idea why you reset_stream? I haven’t been doing that in my code.
Thanks again!
Jason, thanks for the kind words. Unfortunately, I don’t know exactly why I included the reset_stream bit. Most likely because I aped Ryan Heath’s implementation (linked above). Please let me know if you do more research into it!
Hi Barry,
Your post helped me a lot. However, there is still an issue I can’t solve.
XML elements of my request have attributes. Do you know how can I add them with this undocumented-param-building-method?
Thanks
Boy, Jean-Baptiste, I don’t know. I haven’t touched our integration since, well, February.
I do know that since that time there has been a MailBuild gem release. It appears that gem has active development, so perhaps it can help you. If you really need to mess with the XML, you might have to go outside of any web service API’s, unfortunately.