#!/usr/bin/env ruby

def info(str)
  puts "INFO  > #{str}"
end

def debug(str)
  puts "DEBUG > #{str}"
end

def run_hpricot_tests(xml_str)
  begin
    info "loading hpricot without rubygems"
    require 'hpricot'
  rescue LoadError => e
    info "failed, as expected, try again with rubygems"
    require 'rubygems'
    require 'hpricot'
  end
  begin
    Hpricot(nil) # fail, to find where files are in stack-trace
  rescue ArgumentError => e
    info "Found Hpricot in #{e.backtrace.first}"
  end

  xdoc = Hpricot.XML(xml_str)
  info "listing all root-level elements"
  xdoc.children.each {|e| info "  #{e.name}" }

  items = xdoc.search("//Items/Item")
  if items
    info "found #{items.size} item#{items.size==1 ? '' : 's'}"
    items.each do |item|
      item.children.each do |e|
        info "  #{e.name}" 
        if e.name =~ /ItemAttributes/
          e.children.each do |e2|
            info "    #{e2.name} => #{e2.inner_text};"
            debug "      #{e2.inspect}"
          end
        end
      end
      pg = item.at('ItemAttributes/ProductGroup')
      if pg
        info "found ProductGroup >>#{pg.inner_text}<<"
      else
        info "ProductGroup not found"
      end
    end
  else
    info "items not found"
  end

end

XML_DATA =<<EOS
<?xml version="1.0" ?><ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2008-08-19"><OperationRequest><HTTPHeaders><Header Name="UserAgent" Value="Ruby/1.8.7 Palatina/0.1.0"></Header></HTTPHeaders><RequestId>721ba5c3-ec98-4c48-9742-fdf01550d387</RequestId><Arguments><Argument Name="Operation" Value="ItemLookup"></Argument><Argument Name="Service" Value="AWSECommerceService"></Argument><Argument Name="ItemId" Value="3897215012"></Argument><Argument Name="IdType" Value="ASIN"></Argument><Argument Name="AWSAccessKeyId" Value="0J356Z09CN88KB743582"></Argument><Argument Name="Timestamp" Value="2009-09-25T18:15:54Z"></Argument><Argument Name="Signature" Value="ASPJMFi1F14wSgw3oUv9+7ME736D5/QtBgvC8upJrRA="></Argument><Argument Name="ResponseGroup" Value="ItemAttributes,Images"></Argument><Argument Name="Version" Value="2008-08-19"></Argument></Arguments><RequestProcessingTime>0.1082440000000000</RequestProcessingTime></OperationRequest><Items><Request><IsValid>True</IsValid><ItemLookupRequest><Condition>New</Condition><DeliveryMethod>Ship</DeliveryMethod><IdType>ASIN</IdType><MerchantId>Amazon</MerchantId><OfferPage>1</OfferPage><ItemId>3897215012</ItemId><ResponseGroup>ItemAttributes</ResponseGroup><ResponseGroup>Images</ResponseGroup><ReviewPage>1</ReviewPage><ReviewSort>-SubmissionDate</ReviewSort><VariationPage>All</VariationPage></ItemLookupRequest></Request><Item><ASIN>3897215012</ASIN><DetailPageURL>http://www.amazon.com/Linux-kurz-gut-Daniel-Barrett/dp/3897215012%3FSubscriptionId%3D0J356Z09CN88KB743582%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D3897215012</DetailPageURL><ItemLinks><ItemLink><Description>Technical Details</Description><URL>http://www.amazon.com/Linux-kurz-gut-Daniel-Barrett/dp/tech-data/3897215012%3FSubscriptionId%3D0J356Z09CN88KB743582%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D3897215012</URL></ItemLink><ItemLink><Description>Add To Baby Registry</Description><URL>http://www.amazon.com/gp/registry/baby/add-item.html%3Fasin.0%3D3897215012%26SubscriptionId%3D0J356Z09CN88KB743582%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D3897215012</URL></ItemLink><ItemLink><Description>Add To Wedding Registry</Description><URL>http://www.amazon.com/gp/registry/wedding/add-item.html%3Fasin.0%3D3897215012%26SubscriptionId%3D0J356Z09CN88KB743582%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D3897215012</URL></ItemLink><ItemLink><Description>Add To Wishlist</Description><URL>http://www.amazon.com/gp/registry/wishlist/add-item.html%3Fasin.0%3D3897215012%26SubscriptionId%3D0J356Z09CN88KB743582%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D3897215012</URL></ItemLink><ItemLink><Description>Tell A Friend</Description><URL>http://www.amazon.com/gp/pdp/taf/3897215012%3FSubscriptionId%3D0J356Z09CN88KB743582%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D3897215012</URL></ItemLink><ItemLink><Description>All Customer Reviews</Description><URL>http://www.amazon.com/review/product/3897215012%3FSubscriptionId%3D0J356Z09CN88KB743582%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D3897215012</URL></ItemLink><ItemLink><Description>All Offers</Description><URL>http://www.amazon.com/gp/offer-listing/3897215012%3FSubscriptionId%3D0J356Z09CN88KB743582%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D3897215012</URL></ItemLink></ItemLinks><SmallImage><URL>http://ecx.images-amazon.com/images/I/514sg7P10ML._SL75_.jpg</URL><Height Units="pixels">75</Height><Width Units="pixels">45</Width></SmallImage><MediumImage><URL>http://ecx.images-amazon.com/images/I/514sg7P10ML._SL160_.jpg</URL><Height Units="pixels">160</Height><Width Units="pixels">97</Width></MediumImage><LargeImage><URL>http://ecx.images-amazon.com/images/I/514sg7P10ML.jpg</URL><Height Units="pixels">420</Height><Width Units="pixels">254</Width></LargeImage><ImageSets><ImageSet Category="primary"><SwatchImage><URL>http://ecx.images-amazon.com/images/I/514sg7P10ML._SL30_.jpg</URL><Height Units="pixels">30</Height><Width Units="pixels">18</Width></SwatchImage><SmallImage><URL>http://ecx.images-amazon.com/images/I/514sg7P10ML._SL75_.jpg</URL><Height Units="pixels">75</Height><Width Units="pixels">45</Width></SmallImage><ThumbnailImage><URL>http://ecx.images-amazon.com/images/I/514sg7P10ML._SL75_.jpg</URL><Height Units="pixels">75</Height><Width Units="pixels">45</Width></ThumbnailImage><TinyImage><URL>http://ecx.images-amazon.com/images/I/514sg7P10ML._SL110_.jpg</URL><Height Units="pixels">110</Height><Width Units="pixels">67</Width></TinyImage><MediumImage><URL>http://ecx.images-amazon.com/images/I/514sg7P10ML._SL160_.jpg</URL><Height Units="pixels">160</Height><Width Units="pixels">97</Width></MediumImage><LargeImage><URL>http://ecx.images-amazon.com/images/I/514sg7P10ML.jpg</URL><Height Units="pixels">420</Height><Width Units="pixels">254</Width></LargeImage></ImageSet></ImageSets><ItemAttributes><Author>Daniel J. Barrett</Author><Binding>Paperback</Binding><EAN>9783897215016</EAN><Format>Import</Format><ISBN>3897215012</ISBN><Label>O'Reilly Vlg. GmbH &amp; Co.</Label><Languages><Language><Name>German</Name><Type>Original Language</Type></Language><Language><Name>German</Name><Type>Unknown</Type></Language><Language><Name>German</Name><Type>Published</Type></Language></Languages><Manufacturer>O'Reilly Vlg. GmbH &amp; Co.</Manufacturer><NumberOfPages>204</NumberOfPages><PackageDimensions><Height Units="hundredths-inches">79</Height><Length Units="hundredths-inches">685</Length><Weight Units="hundredths-pounds">53</Weight><Width Units="hundredths-inches">425</Width></PackageDimensions><ProductGroup>Book</ProductGroup><ProductTypeName>ABIS_BOOK</ProductTypeName><PublicationDate>2004-09-30</PublicationDate><Publisher>O'Reilly Vlg. GmbH &amp; Co.</Publisher><Studio>O'Reilly Vlg. GmbH &amp; Co.</Studio><Title>Linux kurz und gut</Title></ItemAttributes></Item></Items></ItemLookupResponse>
EOS

# run tests

run_hpricot_tests(XML_DATA)


