# -------------------------------------------------------------------
# Copyright (c) 2009-2010 Sem4r sem4ruby@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# -------------------------------------------------------------------
module Sem4r
class SoapMessageV13
attr_reader :response
attr_reader :counters
def initialize(connector, credentials)
@credentials = credentials
@connector = connector
@response = nil
@counters = {}
end
def body=(soap_body_content)
@soap_body_content = soap_body_content
end
def send(service_url, soap_action)
soap_message = build_soap_message
response_xml = @connector.send(service_url, soap_action, soap_message)
# erase namespace 'nsX'so it more simple parsing the xml
response_xml = response_xml.gsub(/ns\d:/, "")
@response = REXML::Document.new(response_xml)
# extract information from header
#
# 16
# 5
# 5
# abade53d3dbecd45600e7d14563f10f1
#
header = REXML::XPath.first(@response, "//soapenv:Header")
if header
@counters = {
:response_time => header.elements['responseTime'].text.to_i,
:operations => header.elements['operations'].text.to_i,
:units => header.elements['units'].text.to_i
}
end
# check soap fault
#
#
#
# soapenv:Server.generalException
# An internal error has occurred. Please retry your request.
#
#
# 0
# An internal error has occurred. Please retry your request.
#
#
#
#
#
fault_el = REXML::XPath.first(@response, "//soapenv:Fault")
if fault_el
fault_code = fault_el.elements['faultcode'].text
fault_string = fault_el.elements['faultstring'].text
raise SoapError, "#{fault_code}: '#{fault_string}'"
end
self
end
private
def build_soap_header(credentials)
str= <<-EOFS
#{credentials.email}
#{credentials.password}
EOFS
if credentials.client_email
str += "#{credentials.client_email}"
end
str += <<-EOFS
#{credentials.useragent}
#{credentials.developer_token}
EOFS
str
end
def build_soap_message
soap_message = ''
soap_message +=<<-EOFS
EOFS
soap_message += build_soap_header(@credentials)
soap_message += ""
soap_message += @soap_body_content
soap_message += <<-EOFS
EOFS
soap_message
end
end
end