Class: Google4R::Checkout::PrivateDataParser

Inherits:
Object
  • Object
show all
Defined in:
lib/google4r/checkout/notifications.rb

Overview

Class with static methods to parse the <merchant-private(-item)-data> tags.

Class Method Summary collapse

Class Method Details

.element_to_value(element) ⇒ Object

Returns a Hash with the representation of the structure in item/merchant-private-item-data.



745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
# File 'lib/google4r/checkout/notifications.rb', line 745

def self.element_to_value(element)
  # The return value; We will iterate over all children below. When we see a REXML::Element
  # child then we set result to a Hash if it is nil. After the result is a Hash, we will
  # ignore all REXML::Text children.
  # Otherwise, result will be set to the REXML::Text node's value if it is not whitespace
  # only.
  result = nil
  
  element.each_child do |child|
    case child
    when REXML::Element
      result ||= Hash.new
      child_value = self.element_to_value(child)

      # <foo>bar</foo>               becomes 'foo' => 'bar
      # <foo>foo</foo><foo>bar</foo> becomes 'foo' => [ 'foo', 'bar' ]
      if result[child.name].nil? then
        result[child.name] = child_value
      elsif result[child.name].kind_of?(Array) then
        result[child.name] << child_value
      else
        tmp = result[child.name]
        result[child.name] = [ tmp, child_value ]
      end
    when REXML::Text
      next if result.kind_of?(Hash) # ignore text if we already found a tag
      str = child.value.strip
      result = str if str.length > 0
    else
      # ignore
    end
  end
  return result
end