public
Last active

Handle SOAP multi-ref entries a bit more gracefully...

  • Download Gist
savon_nokogiri_response_hack.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# 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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.