Skip to content

Commit

Permalink
moved hash to xml translation into a new gem
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiii committed Nov 29, 2010
1 parent 51fa0e6 commit bac4b4d
Show file tree
Hide file tree
Showing 15 changed files with 7 additions and 442 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* Added Savon::SOAP::XML#env_namespace to configure the SOAP envelope namespace. It defaults to :env
but can also be set to an empty String for SOAP envelope tags without a namespace.

* Replaced quite a lot of core extensions by moving the Hash to XML translation into a new gem called
Gyoku (http://rubygems.org/gems/gyoku).

## 0.8.0.beta.4 (2010-11-20)

* Fix for issue #107 (Soap Fault regex not working for API I connect).
Expand Down
45 changes: 0 additions & 45 deletions lib/savon/core_ext/array.rb

This file was deleted.

19 changes: 0 additions & 19 deletions lib/savon/core_ext/datetime.rb

This file was deleted.

87 changes: 0 additions & 87 deletions lib/savon/core_ext/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
require "savon"
require "savon/core_ext/object"
require "savon/core_ext/string"
require "savon/core_ext/symbol"
require "savon/core_ext/array"
require "savon/core_ext/datetime"

module Savon
module CoreExt
Expand All @@ -19,74 +16,6 @@ def find_soap_body
body_key ? envelope[body_key].map_soap_response : {}
end

# Translates the Hash into SOAP request compatible XML.
#
# { :find_user => { :id => 123, "wsdl:Key" => "api" } }.to_soap_xml
# # => "<findUser><id>123</id><wsdl:Key>api</wsdl:Key></findUser>"
#
# ==== Mapping
#
# * Hash keys specified as Symbols are converted to lowerCamelCase Strings
# * Hash keys specified as Strings are not converted and may contain namespaces
# * DateTime values are converted to xs:dateTime Strings
# * Objects responding to to_datetime (except Strings) are converted to xs:dateTime Strings
# * TrueClass and FalseClass objects are converted to "true" and "false" Strings
# * All other objects are expected to be converted to Strings using to_s
#
# An example:
#
# { :magic_request => {
# :perform_move => true,
# "perform_at" => DateTime.new(2010, 11, 22, 11, 22, 33)
# }
# }.to_soap_xml
#
# <magicRequest>
# <performMove>true</performMove>
# <perform_at>2012-06-11T10:42:21</perform_at>
# </magicRequest>
#
# ==== Escaped XML values
#
# By default, special characters in XML String values are escaped.
#
# ==== Fixed order of XML tags
#
# In case your service requires the tags to be in a specific order (parameterOrder), you have two
# options. The first is to specify your body as an XML string. The second is to specify the order
# through an additional array stored under the +:order!+ key.
#
# { :name => "Eve", :id => 123, :order! => [:id, :name] }.to_soap_xml
# # => "<id>123</id><name>Eve</name>"
#
# ==== XML attributes
#
# If you need attributes, you could either go with an XML string or add another hash under the
# +:attributes!+ key.
#
# { :person => "Eve", :attributes! => { :person => { :id => 666 } } }.to_soap_xml
# # => '<person id="666">Eve</person>'
def to_soap_xml
xml = Builder::XmlMarkup.new
attributes = delete(:attributes!) || {}

order.each do |key|
attrs = attributes[key] || {}
value = self[key]
escape_xml = key.to_s[-1, 1] != "!"
key = key.to_soap_key

case value
when ::Array then xml << value.to_soap_xml(key, escape_xml, attrs)
when ::Hash then xml.tag!(key, attrs) { xml << value.to_soap_xml }
when NilClass then xml.tag!(key, "xsi:nil" => "true")
else xml.tag!(key, attrs) { xml << (escape_xml ? value.to_soap_value : value.to_soap_value!) }
end
end

xml.target!
end

# Maps keys and values of a Hash created from SOAP response XML to more convenient Ruby Objects.
def map_soap_response
inject({}) do |hash, (key, value)|
Expand All @@ -112,22 +41,6 @@ def map_soap_response
end
end

private

# Deletes and returns an Array of keys stored under the :order! key. Defaults to return the actual
# keys of this Hash if no :order! key could be found. Raises an ArgumentError in case the :order!
# Array does not match the Hash keys.
def order
order = delete :order!
order = keys unless order.kind_of? ::Array

missing, spurious = keys - order, order - keys
raise ArgumentError, "Missing elements in :order! #{missing.inspect}" unless missing.empty?
raise ArgumentError, "Spurious elements in :order! #{spurious.inspect}" unless spurious.empty?

order
end

end
end
end
Expand Down
10 changes: 0 additions & 10 deletions lib/savon/core_ext/object.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "savon/core_ext/datetime"

module Savon
module CoreExt
module Object
Expand All @@ -9,14 +7,6 @@ def blank?
respond_to?(:empty?) ? empty? : !self
end unless defined? blank?

# Returns the Object as a SOAP request compliant value.
def to_soap_value
return to_s unless respond_to? :to_datetime
to_datetime.to_soap_value
end

alias_method :to_soap_value!, :to_soap_value

end
end
end
Expand Down
17 changes: 0 additions & 17 deletions lib/savon/core_ext/string.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "cgi"

require "savon/soap"

module Savon
Expand Down Expand Up @@ -52,21 +50,6 @@ def map_soap_response
self
end

# Returns the Object as a SOAP request compliant key.
def to_soap_key
self[-1, 1] == "!" ? chop : self
end

# Returns the String as a SOAP value. Escapes special characters for XML.
def to_soap_value
CGI.escapeHTML self
end

# Convert the String into a SOAP value without escaping special characters.
def to_soap_value!
self
end

end
end
end
Expand Down
16 changes: 0 additions & 16 deletions lib/savon/core_ext/symbol.rb

This file was deleted.

6 changes: 3 additions & 3 deletions lib/savon/soap/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def namespace_identifier
# Accessor for the <tt>Savon::WSSE</tt> object.
attr_accessor :wsse

# Accessor for the SOAP +body+. Expected to be a Hash that can be translated to XML via Hash.to_soap_xml
# Accessor for the SOAP +body+. Expected to be a Hash that can be translated to XML via Gyoku.xml
# or any other Object responding to to_s.
attr_accessor :body

Expand Down Expand Up @@ -132,7 +132,7 @@ def complete_namespaces

# Returns the SOAP header as an XML String.
def header_for_xml
@header_for_xml ||= header.to_soap_xml + wsse_header
@header_for_xml ||= Gyoku.xml(header) + wsse_header
end

# Returns the WSSE header or an empty String in case WSSE was not set.
Expand All @@ -142,7 +142,7 @@ def wsse_header

# Returns the SOAP body as an XML String.
def body_to_xml
body.respond_to?(:to_soap_xml) ? body.to_soap_xml : body.to_s
body.kind_of?(Hash) ? Gyoku.xml(body) : body.to_s
end

end
Expand Down
1 change: 1 addition & 0 deletions savon.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Gem::Specification.new do |s|
s.add_dependency "builder", "~> 2.1.2"
s.add_dependency "crack", "~> 0.1.8"
s.add_dependency "httpi", ">= 0.7.1"
s.add_dependency "gyoku", ">= 0.1.0"

s.add_development_dependency "rspec", "~> 2.0.0"
s.add_development_dependency "mocha", "~> 0.9.7"
Expand Down
49 changes: 0 additions & 49 deletions spec/savon/core_ext/array_spec.rb

This file was deleted.

21 changes: 0 additions & 21 deletions spec/savon/core_ext/datetime_spec.rb

This file was deleted.

Loading

0 comments on commit bac4b4d

Please sign in to comment.