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.



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/google4r/checkout/notifications.rb', line 496

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