Permalink
Browse files

added iterate_hash_array, to make it easier to add body and header me…

…ssages, and a minor tweak, renamed flags to options to make it more versatile
  • Loading branch information...
1 parent 183eadc commit e71e3f9756a9a85356f0c7f280d03e79e813ccdb @pgericson pgericson committed Jan 19, 2010
Showing with 15 additions and 10 deletions.
  1. +5 −3 lib/handsoap/service.rb
  2. +10 −7 lib/handsoap/xml_mason.rb
View
8 lib/handsoap/service.rb
@@ -236,6 +236,7 @@ def invoke(action, options = { :soap_action => :auto }, &block) # :yields: Hands
body.add(action)
end
end
+ puts doc.to_s
if block_given?
yield doc.find(action)
end
@@ -329,13 +330,14 @@ def async(user_block, &block) # :yields: Handsoap::AsyncDispatch
#Used to iterate over a Hash, that can include Hash, Array or String/Float/Integer etc and insert it in the correct element.
def iterate_hash_array(element, hash_array)
- hash_array.each {|hash| iterate_hash_array(element, hash)} if hash_array.is_a?(Array)
hash_array.each do |name,value|
if value.is_a?(Hash)
element.add(name){|subelement| iterate_hash_array(subelement, value)}
elsif value.is_a?(Array)
- value.each do |item|
- element.add(name, iterate_hash_array(element,item)) if item.is_a?(Hash)
+ element.add(name) do |subelement|
+ value.each do |item|
+ iterate_hash_array(subelement, item) if item.is_a?(Hash)
+ end
end
else
puts "#{name.to_s}: #{name.class.to_s} - #{value.to_s}:#{value.class.to_s}"
View
17 lib/handsoap/xml_mason.rb
@@ -15,9 +15,9 @@ class Node
def initialize
@namespaces = {}
end
- def add(node_name, value = nil, *flags) # :yields: Handsoap::XmlMason::Element
+ def add(node_name, value = nil, options = {}) # :yields: Handsoap::XmlMason::Element
prefix, name = parse_ns(node_name)
- node = append_child Element.new(self, prefix, name, value, flags)
+ node = append_child Element.new(self, prefix, name, value, options)
if block_given?
yield node
end
@@ -105,7 +105,7 @@ def to_s(indentation = '')
end
class Element < Node
- def initialize(parent, prefix, node_name, value = nil, flags = []) # :yields: Handsoap::XmlMason::Element
+ def initialize(parent, prefix, node_name, value = nil, options = {}) # :yields: Handsoap::XmlMason::Element
super()
# if prefix.to_s == ""
# raise "missing prefix"
@@ -115,8 +115,11 @@ def initialize(parent, prefix, node_name, value = nil, flags = []) # :yields: Ha
@node_name = node_name
@children = []
@attributes = {}
+ if options[:attributes]
+ @attributes = options[:attributes]
+ end
if not value.nil?
- set_value value.to_s, *flags
+ set_value value.to_s, options
end
if block_given?
yield self
@@ -142,14 +145,14 @@ def append_child(node)
end
# Sets the inner text of this element.
#
- # By default the string is escaped, but you can pass the flag :raw to inject XML.
+ # By default the string is escaped, but you can pass the option flag :raw to inject XML.
#
# You usually won't need to call this method, but will rather use +add+
- def set_value(value, *flags)
+ def set_value(value, options = {})
if @children.length > 0
raise "Element already has children. Can't set value"
end
- if flags && flags.include?(:raw)
+ if options && options.include?(:raw)
@children = [RawContent.new(value)]
else
@children = [TextNode.new(value)]

0 comments on commit e71e3f9

Please sign in to comment.