Class: Hash
Instance Method Summary collapse
-
#find_regexp(regexp) ⇒ Object
Expects an Array of Regexp Objects of which every Regexp recursively matches a key to be accessed.
-
#map_soap_response ⇒ Object
Maps keys and values of a Hash created from SOAP response XML to more convenient Ruby Objects.
-
#to_soap_xml ⇒ Object
Returns the Hash translated into SOAP request compatible XML.
Instance Method Details
#find_regexp(regexp) ⇒ Object
Expects an Array of Regexp Objects of which every Regexp recursively matches a key to be accessed. Returns the value of the last Regexp filter found in the Hash or an empty Hash in case the path of Regexp filters did not match the Hash structure.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/savon/core_ext/hash.rb', line 7 def find_regexp(regexp) regexp = [regexp] unless regexp.kind_of? Array result = dup regexp.each do |pattern| result_key = result.keys.find { |key| key.to_s.match pattern } result = result[result_key] ? result[result_key] : {} end result end |
#map_soap_response ⇒ Object
Maps keys and values of a Hash created from SOAP response XML to more convenient Ruby Objects.
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/savon/core_ext/hash.rb', line 43 def map_soap_response inject({}) do |hash, (key, value)| key = key.strip_namespace.snakecase.to_sym value = case value when Hash then value["xsi:nil"] ? nil : value.map_soap_response when Array then value.map { |a_value| a_value.map_soap_response rescue a_value } when String then value.map_soap_response end hash.merge key => value end end |
#to_soap_xml ⇒ Object
Returns the Hash translated into SOAP request compatible XML.
To control the order of output, add a key of :@inorder with the value being an array listing the other keys in order.
Example
{ :find_user => { :id => 666 } }.to_soap_xml
=> "<findUser><id>666</id></findUser>"
{ :find_user => { :name => 'Lucy', :id => 666, :@inorder => [:id,:name] } }.to_soap_xml
=> "<findUser><id>666</id><name>Lucy</name></findUser>"
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/savon/core_ext/hash.rb', line 30 def to_soap_xml @soap_xml = Builder::XmlMarkup.new i = self.delete :@inorder # retrieve the hash element keyed by :@inorder if it exists k = self.keys i = k unless (i and i.class == Array) raise "missing elements in :@inorder #{(k - i).inspect}" unless (k - i).empty? raise "spurious elements in :@inorder #{(i - k).inspect}" unless (i - k).empty? i.each {|key| nested_data_to_soap_xml key, self[key] } @soap_xml.target! end |