Skip to content

Instantly share code, notes, and snippets.

@lwe
Created December 16, 2009 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lwe/257950 to your computer and use it in GitHub Desktop.
Save lwe/257950 to your computer and use it in GitHub Desktop.
Handle SOAP multi-ref entries a bit more gracefully...
# Basically replaces just all `<arg href="#xyz"/>`-entries with the "real" value.
module NokogiriResponse
def xml_body
@xml_body ||= NokogiriResponse.create_doc_and_process_multirefs(@response.body)
end
def to_hash_from_xml_body
@hash_body ||= Crack::XML.parse(xml_body.to_s).find_regexp([/.+:Envelope/, /.+:Body/, /.+/]).map_soap_response
end
private
def self.create_doc_and_process_multirefs(xml)
# find all references
doc = Nokogiri::XML(xml)
doc.css('[href]').select { |n| !n.blank? && n.attributes.size == 1 }.map { |n| n['href'] }.sort.uniq.each do |ref|
ref_node = doc.css(ref).first
unless ref_node.nil?
doc.css("[href='#{ref}']").each do |node|
ref_node.children.each { |child| node.add_child(child.clone) }
ref_node.attributes.each { |name, value| node[name] = value unless ["id", "root"].include?(name) }
end
ref_node.remove # delete node from document
end
end
doc
end
end
Savon::Response.send(:include, NokogiriResponse)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment