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.



671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
# File 'lib/google4r/checkout/notifications.rb', line 671

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