ruby - How do I find elements in this hpricot object? -


given hpricot xml @ bottom of post, how select "item" without having use .each? every single piece of documentation uses variation of

@res.items.each |item|   # stuff end 

which pointless in case because there ever 1 "item". been tearing y hair out last ages trying right.

edited add more information:

ok judging comments, i'm missing point somewhere i'll provide more information.

i'm using ruby gem called amazon-ecs retrieve product information amazon. on gem's site described as

a generic ruby amazon product advertising api (previously known e-commerce rest api) using hpricot. uses response , element wrapper classes easy access rest api xml output. generic, can extend amazon::ecs support other not-implemented operations easily; , response object wraps around hpricot element object, instead of providing one-to-one object/attributes xml elements map.

now hones don't understand means suspect bit wrapping response object what's making difficult!

basically, when this:

@res = amazon::ecs.item_lookup(ean, options_hash) 

and print out "debug @res", have below.

hope helps! end edit

hpricot xml:

<amazon::ecs::response:0xa4449cc @doc=#<hpricot::doc  {xmldecl "<?xml version=\"1.0\" ?>"}  {elem <itemlookupresponse xmlns="http://webservices.amazon.com/awsecommerceservice/2005-10-05">    {elem <operationrequest>      {elem <httpheaders>        {emptyelem <header name="useragent" value="ruby">}      </httpheaders>}      {elem <requestid> "b89bad91-f5a1-4daf-87f2-d309dded35d6" </requestid>}      {elem <arguments>        {emptyelem <argument name="operation" value="itemlookup">}        {emptyelem <argument name="searchindex" value="books">}        {emptyelem <argument name="signature" value="dasdasdsadsadsafdfdsfsdsasadsadsd">}        {emptyelem <argument name="itemid" value="9780307463746">}        {emptyelem <argument name="idtype" value="isbn">}        {emptyelem <argument name="awsaccesskeyid" value="sdasdsadsadsadsadsadd">}        {emptyelem <argument name="timestamp" value="2011-02-17t15:08:09z">}        {emptyelem <argument name="service" value="awsecommerceservice">}      </arguments>}      {elem <requestprocessingtime> "0.0252220000000000" </requestprocessingtime>}    </operationrequest>}    {elem <items>      {elem <request>        {elem <isvalid> "true" </isvalid>}        {elem <itemlookuprequest>          {elem <condition> "new" </condition>}          {elem <deliverymethod> "ship" </deliverymethod>}          {elem <idtype> "isbn" </idtype>}          {elem <merchantid> "amazon" </merchantid>}          {elem <offerpage> "1" </offerpage>}          {elem <itemid> "9780307463746" </itemid>}          {elem <responsegroup> "small" </responsegroup>}          {elem <reviewpage> "1" </reviewpage>}          {elem <searchindex> "books" </searchindex>}        </itemlookuprequest>}      </request>}      {elem <item>        {elem <asin> "0307463745" </asin>}        {elem <detailpageurl> "http://rads.stackoverflow.com/amzn/click/0307463745" </detailpageurl>}        {elem <smallimage>          {elem <url> "http://ecx.images-amazon.com/images/i/41qz6afdrll._sl75_.jpg" </url>}          {elem <height units="pixels"> "75" </height>}          {elem <width units="pixels"> "50" </width>}        </smallimage>}        {elem <mediumimage>          {elem <url> "http://ecx.images-amazon.com/images/i/41qz6afdrll._sl160_.jpg" </url>}          {elem <height units="pixels"> "160" </height>}          {elem <width units="pixels"> "106" </width>}        </mediumimage>} {elem <largeimage> {elem <url> "http://ecx.images-amazon.com/images/i/41qz6afdrll.jpg" </url>} {elem <height units="pixels"> "500" </height>} {elem <width units="pixels"> "331" </width>} </largeimage>} {elem <imagesets> {elem <imageset category="primary"> {elem <swatchimage> {elem <url> "http://ecx.images-amazon.com/images/i/41qz6afdrll._sl30_.jpg" </url>} {elem <height units="pixels"> "30" </height>} {elem <width units="pixels"> "20" </width>} </swatchimage>} {elem <smallimage> {elem <url> "http://ecx.images-amazon.com/images/i/41qz6afdrll._sl75_.jpg" </url>} {elem <height units="pixels"> "75" </height>} {elem <width units="pixels"> "50" </width>} </smallimage>} {elem <mediumimage> {elem <url> "http://ecx.images-amazon.com/images/i/41qz6afdrll._sl160_.jpg" </url>} {elem <height units="pixels"> "160" </height>} {elem <width units="pixels"> "106" </width>} </mediumimage>} {elem <largeimage> {elem <url> "http://ecx.images-amazon.com/images/i/41qz6afdrll.jpg" </url>} {elem <height units="pixels"> "500" </height>} {elem <width units="pixels"> "331" </width>} </largeimage>} </imageset>} </imagesets>}       {elem <itemattributes>          {elem <author> "jason fried" </author>}          {elem <author> "david heinemeier hansson" </author>}          {elem <manufacturer> "crown business" </manufacturer>}          {elem <productgroup> "book" </productgroup>}          {elem <title> "rework" </title>}        </itemattributes>}      </item>}    </items>}  </itemlookupresponse>} 

first, extract hpricot object @res (from docs).

doc = @res.doc 

then, should able use hpricot object:

puts (doc/:item).inner_html 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -