lib/apricoteatsgorilla/apricoteatsgorilla.rb in rubiii-apricoteatsgorilla-0.5.7 vs lib/apricoteatsgorilla/apricoteatsgorilla.rb in rubiii-apricoteatsgorilla-0.5.8
- old
+ new
@@ -8,10 +8,16 @@
# SOAP messages (XML) and Ruby Hashes and comes with some additional helpers
# for working with SOAP services.
class ApricotEatsGorilla
class << self
+ # SOAP dateTime format.
+ SOAPDateTimeFormat = "%Y-%m-%dT%H:%M:%S"
+
+ # SOAP dateTime Regexp.
+ SOAPDateTimeRegexp = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/
+
# SOAP namespaces by SOAP version.
SOAPNamespace = {
1 => "http://schemas.xmlsoap.org/soap/envelope/",
2 => "http://www.w3.org/2003/05/soap-envelope"
}
@@ -146,11 +152,13 @@
private
# Translates a single XML root node to a Ruby Hash.
def single_xml_root_node_to_hash(root)
- if root.first.children.first.kind_of? Hpricot::Text
+ if root.first.children.nil?
+ {}
+ elsif root.first.children.first.kind_of? Hpricot::Text
map_xml_value(root.first.children.to_s)
else
xml_node_to_hash(root.first)
end
end
@@ -249,11 +257,11 @@
end
# Converts XML values to more natural Ruby objects.
def map_xml_value(value)
case value
- when /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/
+ when SOAPDateTimeRegexp
DateTime.parse(value)
when "true"
true
when "false"
false
@@ -263,12 +271,12 @@
end
# Converts Hash values into valid XML values.
def map_hash_value(value)
if value.kind_of? DateTime
- value.strftime("%Y-%m-%dT%H:%M:%S")
- elsif value.respond_to? :to_datetime
- value.to_datetime.strftime("%Y-%m-%dT%H:%M:%S")
+ value.strftime(SOAPDateTimeFormat)
+ elsif !value.kind_of?(String) && value.respond_to?(:to_datetime)
+ value.to_datetime.strftime(SOAPDateTimeFormat)
elsif value.respond_to? :to_s
value.to_s
else
nil
end