Skip to content

@lwe /savon_nokogiri_response_hack.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.