lib/apricoteatsgorilla.rb in smacks-apricoteatsgorilla-0.3.3 vs lib/apricoteatsgorilla.rb in smacks-apricoteatsgorilla-0.3.6

- old
+ new

@@ -5,24 +5,37 @@ # # It translates between SOAP response messages (XML) and Ruby Hashes and may # be used to build a SOAP request envelope. It is based on CobraVsMongoose but # uses Hpricot instead of REXML and doesn't follow the BadgerFish convention. # -# === Translating an XML String into a Ruby Hash +# === xml_to_hash(xml, root_node = nil) # -# xml = "<apricot><eats>Gorilla</eats></apricot>" -# ApricotEatsGorilla[xml] -# # => { :eats => "Gorilla" } +# xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> +# <soap:Body> +# <ns2:authenticateResponse xmlns:ns2="http://v1_0.ws.example.com/"> +# <return> +# <authValue> +# <token>secret</token> +# <client>example</client> +# </authValue> +# </return> +# </ns2:authenticateResponse> +# </soap:Body> +# </soap:Envelope>' # -# === Translating a Ruby Hash into an XML String +# ApricotEatsGorilla[xml, "//return"] +# # => { :auth_value => { :token => "secret", :client => "example" } } # +# === hash_to_xml(hash) +# # hash = { :apricot => { :eats => "Gorilla" } } +# # ApricotEatsGorilla[hash] # # => "<apricot><eats>Gorilla</eats></apricot>" # -# === Creating a SOAP request envelope -# +# === soap_envelope(namespaces = {}) +# # ApricotEatsGorilla.soap_envelope { "<authenticate>me</authenticate>" } # # # => '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> # # => <env:Body> # # => <authenticate>me</authenticate> @@ -74,10 +87,12 @@ # # => { :lots_of => "Gorillas" } def xml_to_hash(xml, root_node = nil) xml = clean_xml(xml) doc = Hpricot.XML(xml) root = root_node ? doc.at(root_node) : doc.root + + return nil if root.nil? xml_node_to_hash(root) end # Converts a given +hash+ into an XML String. # @@ -162,10 +177,10 @@ when Array this_node[key] << value when nil this_node[key] = value else - this_node[key] = [current, value] + this_node[key] = [current.dup, value] end end this_node end \ No newline at end of file