Skip to content

Commit

Permalink
added iterate_hash_array, to make it easier to add body and header me…
Browse files Browse the repository at this point in the history
…ssages, and a minor tweak, renamed flags to options to make it more versatile
  • Loading branch information
pgericson committed Jan 19, 2010
1 parent 183eadc commit e71e3f9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
8 changes: 5 additions & 3 deletions lib/handsoap/service.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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}"
Expand Down
17 changes: 10 additions & 7 deletions lib/handsoap/xml_mason.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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)]
Expand Down

0 comments on commit e71e3f9

Please sign in to comment.