Last active
March 2, 2020 10:18
-
-
Save brunofrank/41a022a74782aecf840b61195edd0182 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'nokogiri, '~> 1.10' | |
gem 'httparty' | |
gem 'wasabi' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SoapEnvelopeBuilder | |
SCHEMA_TYPES = { | |
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema', | |
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance' | |
} | |
SOAP_NAMESPACE = { | |
1 => { 'soap11' => 'http://schemas.xmlsoap.org/soap/envelope/' }, | |
2 => { 'soap12' => 'http://www.w3.org/2003/05/soap-envelope' } | |
} | |
attr_accessor :soap_version, :wsdl | |
def initialize(wsdl:, soap_version: 2) | |
@wsdl = wsdl | |
@soap_version = soap_version | |
end | |
def build(operation, message:) | |
input = wsdl.operations[operation][:input] | |
xml = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |doc| | |
doc.Envelope(SCHEMA_TYPES) { | |
doc.parent.namespace = doc.parent.add_namespace_definition(soap_namespace, soap_namespace_uri) | |
doc[soap_namespace].Body { | |
doc.send(input, 'xmlns' => wsdl.namespace) { | |
parameters(operation, message, doc) | |
} | |
} | |
} | |
end | |
xml.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::AS_XML).strip | |
end | |
private | |
def parameters(operation, message, xml_doc) | |
if wsdl.operations[operation][:parameters].blank? | |
xml_doc.parent << message | |
else | |
wsdl.operations[operation][:parameters].each do |key, value| | |
xml_doc.send(value[:name]) { | |
xml_doc.parent << (message.class == Hash ? message[key] : message) | |
} | |
end | |
end | |
end | |
def soap_namespace | |
@soap_namespace ||= SOAP_NAMESPACE[soap_version].keys.first | |
end | |
def soap_namespace_uri | |
@soap_namespace_uri ||= SOAP_NAMESPACE[soap_version][soap_namespace] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment