Calling MailBuild AddWithCustomFields with Ruby

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:

require ’soap/wsdlDriver’

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.

u  = User.find(:first)
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:

def add_with_is_admin_custom_field(user)
  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:

Technorati Tags: , ,

Share if you like:
  • Digg
  • StumbleUpon
  • del.icio.us
  • Technorati
  • Ma.gnolia
  • Reddit
  • description
Related posts:

Comments (4)

  1. Jason wrote:

    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!

    Monday, April 21, 2008 at 6:18 pm #
  2. Barry wrote:

    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!

    Monday, April 21, 2008 at 8:52 pm #
  3. 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

    Friday, October 17, 2008 at 5:14 am #
  4. Barry wrote:

    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.

    Friday, October 17, 2008 at 8:12 am #