Problems parsing Google Data Booksearch API XML in Ruby

Posted by FrogBot on Stack Overflow See other posts from Stack Overflow or by FrogBot
Published on 2011-11-19T01:12:56Z Indexed on 2011/11/19 1:50 UTC
Read the original article Hit count: 100

Filed under:
|
|
|

I'm trying to parse some XML I've gotten from the Google Data Booksearch API and I'm having trouble trying to target a specific element. Currently my code looks like so:

require 'gdata'

client = GData::Client::BookSearch.new
feed = client.get("http://books.google.com/books/feeds/volumes?q=Foundation").to_xml

books = []

feed.elements.each('entry') do |entry|
  book = {
  :title   => entry.elements['title'].text,
  :author  => entry.elements['dc:creator'].text,
  :book_id => entry.elements['dc:identifier'].text
  }

  books.push(book)
end

p books

and that all works fine, but I want to add a thumbnail URL to the book hash. The tag with each book's thumbnail URL looks like so:

<feed>
  <entry>
    ...
    <link rel="http://schemas.google.com/books/2008/thumbnail" type="image/x-unknown" href="http://bks6.books.google.com/books?id=ID5P7xbmcO8C&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_gdata"/>
    ...
  </entry>
</feed>

I want to grab the contents of the href attribute from this element and I'm not exactly sure how. Can anyone help me out here?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about Xml