Module: AmazonProductAdvertisingApi::Operations::Item::Common

Included in:
ItemLookup, ItemSearch, SimilarityLookup
Defined in:
lib/amazon_product_advertising_api/operations/item.rb

Overview

Gets mixed into each of the classes in AmazonProductAdvertisingApi::Operations::Item for common behaviours.

Instance Method Summary collapse

Instance Method Details

#parseObject

Item methods return xml consisting of an Items tag with several Item tags inside.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/amazon_product_advertising_api/operations/item.rb', line 9

def parse
  self.response.add_element("Items", AmazonProductAdvertisingApi::Operations::Base::Element.new)

  (self.hpricot_data/'Items > Item').each do |element|
    new_element = AmazonProductAdvertisingApi::Operations::Base::Element.new
    self.response.items << new_element

    queue = []
    queue << [new_element, element.containers]
    
    queue.each do |pair|
      current_element    = pair[0]
      current_children = pair[1]
  
      current_children.each do |child|
        if child.containers.size == 0
          current_element.add_element(child.name, child.inner_html)
        else
          if parent_a_container?(child)
             new_element = AmazonProductAdvertisingApi::Operations::Base::Element.new
            current_element << new_element
            queue           << [new_element, child.containers]
          else
            new_element = current_element.add_element(child.name, AmazonProductAdvertisingApi::Operations::Base::Element.new)
            queue << [new_element, child.containers]
          end
        end
      end
    end
  end
end