Consume SOAP WebServices Using Ruby With Savon

Surendran Sukumaran

51 sec read

Recently, we had a task of consuming a SOAP based web serivces which sends and receive complex objects.

We started taking Amazon Commerce webserices whose WSDL can be found at http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl
Now the fun part, you can find lots of ruby example for consuming simple datatype but very few for complete datatypes or objects.
Here is the worked out example posted it at GITHub https://github.com/spritle/awssoap4r. Please feel free to improve and send me a pull request.
Impatient to open github and see the core code, here is the snippet for you.
[source language=”ruby”]
module Aws
class Consumer
attr_reader :aws_access_key_id, :aws_secret_key, :search_index, :keyword
@@url = “http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl”
def initialize(aws_access_key_id, aws_secret_key, sIndex, keyword)
@aws_access_key_id = aws_access_key_id
@aws_secret_key = aws_secret_key
@search_index = sIndex
@keyword = keyword
@client = Savon::Client.new(@@url)
end
def timestamp_and_signature(operation)
timestamp = Time.now.gmtime.iso8601
hmac = HMAC::SHA256.new(@aws_secret_key)
hmac.update(“#{operation}#{timestamp}”)
# chomp to get rid of the newline
signature = Base64.encode64(hmac.digest).chomp
[timestamp, signature]
end
def search
operation = “ItemSearch”
timestamp, signature = timestamp_and_signature(operation)
@client.request :item_search do |soap|
soap.body= {
“Request” => {“Keywords” => @keyword, “SearchIndex” => @search_index},
“AssociateTag” => ‘Keywords’,
“Timestamp” => timestamp,
“AWSAccessKeyId” => @aws_access_key_id,
“Signature” => signature
}
end
end
end
end
[/source]

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *