I haven’t posted a technical entry in a while. I’ve been busy, and have been working on cool stuff. I just haven’t had anything ready to post.
I’ve been working off and on with a buddy (classmate) from The Big Nerd Ranch. We attended the Ruby on Rails class over a year ago together, and have been in communication through the mailing list and email. His business is growing, which is great.
He is to the point now where he has budget to pay for contract work, which is also great for me. I’d help him out just to be helpful and get some more exposure to facets of code and business that I don’t run across in my day job, but making money is also good.
He reached out to me a week or so ago needing some help with something, and I thought it would be a great little unit of work. Yes and No.
The Project
He needed to access an API for a service that he needs to use. Great! We are about to setup all kinds of services at work. This particular API was accessible via SOAP, though. Ouch.
The world is moving past SOAP. However, I had JUST received another inquiry about SOAP from another client the day before. So I thought “why not?” I could maybe even kill 2 birds with the same learning stone.
Getting Up To Speed
I did some research, and it turns out that consuming SOAP services is actually pretty easy in ruby. Yes and No. This works really well for an open API.
Some API methods require some sort of authentication to be submitted in the header, though. I had trouble getting that piece to work. I did find a post or two that showed me how to put stuff in the header, but none of them worked with the API I needed to talk to.
At it’s core SOAP requests are just XML, though. I stumbled across this post early in my digging around that suggested just posting XML to the service and not dealing with all that soap4r or WSDL mess.
I was still having trouble, though. I couldn’t get the XML formatted in the way that the API wanted it.
AH HA!
The main support guy at the company whose API I was trying to talk to pointed me to this site that looks at the WSDL and tells you what the XML will be. Yahtzee!
Now I could get cracking. And I did.
The end result is a hybrid. I authenticate with the SOAP::WSDLDriverFactory to get the session ID. Then I generate the XML for the requests and post them via HTTP. I then parse the result with ReXML using XPath.
I wanted to stick with the core ruby library if possible so that there wouldn’t be any gems to unpack into an overly complicated project.
Maybe this is an ugly hack. Maybe I am a genius. Maybe it’s somewhere in between. I don’t really know, but I had a deadline, and this happens to work. It also keeps things as bare-metal as possible so that we can see the requests in a format that is sort of human readable (XML) so that we can adjust as needed. There is a lot of magic in the soap4r and WSDL libraries that I never really got my head around.
I would say that it’s still pretty easy stuff now that I know what I am dealing with. I’m also more excited about setting up our REST architecture here at the office.