Permalink
Browse files

Add methods for getting books from user's shelves.

  • Loading branch information...
thegreatape committed Dec 4, 2012
1 parent fe44a07 commit d59993d1f0ae49159077189117f199ead09e7bb0
Showing with 2,604 additions and 0 deletions.
  1. +12 −0 README.md
  2. +2 −0 lib/goodreads/client.rb
  3. +26 −0 lib/goodreads/client/shelves.rb
  4. +42 −0 spec/client_spec.rb
  5. +10 −0 spec/fixtures/empty.xml
  6. +1,252 −0 spec/fixtures/to-read-p2.xml
  7. +1,260 −0 spec/fixtures/to-read.xml
View
@@ -106,6 +106,18 @@ review.book # => Book information
review.rating # => User rating
```
+### Shelves
+
+Get the books on a user's shelf:
+
+```ruby
+shelf = client.shelf(user_id, shelf_name)
+shelf.books # array of books on this shelf
+shelf.start # start index of this page of paginated results
+shelf.end # end index of this page of paginated results
+shelf.total # total number of books on this shelf
+```
+
## Contributions
Feel free to contribute any patches or new features.
View
@@ -3,6 +3,7 @@
require 'goodreads/client/reviews'
require 'goodreads/client/authors'
require 'goodreads/client/users'
+require 'goodreads/client/shelves'
module Goodreads
class Client
@@ -11,6 +12,7 @@ class Client
include Goodreads::Reviews
include Goodreads::Authors
include Goodreads::Users
+ include Goodreads::Shelves
attr_reader :api_key, :api_secret
@@ -0,0 +1,26 @@
+module Goodreads
+ module Shelves
+
+ # Get books from a user's shelf
+ def shelf(user_id, shelf_name, options={})
+ options = options.merge(:shelf => shelf_name, :v =>2)
+ data = request("/review/list/#{user_id}.xml", options)
+ reviews = data['reviews']['review']
+
+ books = []
+ unless reviews.nil?
+ # one-book results come back as a single hash
+ reviews = [reviews] if !reviews.instance_of?(Array)
+ books = reviews.map {|e| Hashie::Mash.new(e)}
+ end
+
+ Hashie::Mash.new({
+ :start => data['reviews']['start'].to_i,
+ :end => data['reviews']['end'].to_i,
+ :total => data['reviews']['total'].to_i,
+ :books => books
+ })
+ end
+
+ end
+end
View
@@ -125,4 +125,46 @@
@search.results.work.size.should == 3
@search.results.work.first.id.should == 6928276
end
+
+ it "should return list of books for a user's specified shelf" do
+ stub_with_key_get('/review/list/1.xml',
+ {:shelf => 'to-read', :v=> '2'},
+ 'to-read.xml')
+
+ proc { @shelf = @client.shelf('1', 'to-read') }.should_not raise_error
+ @shelf.respond_to?(:start).should == true
+ @shelf.respond_to?(:end).should == true
+ @shelf.respond_to?(:total).should == true
+ @shelf.respond_to?(:books).should == true
+ @shelf.start.should == 1
+ @shelf.end.should == 20
+ @shelf.total.should == 40
+ @shelf.books.length.should == 20
+ @shelf.books.first.id.should == '45590939'
+ @shelf.books.first.book.title.strip.should == 'The Demon-Haunted World: Science as a Candle in the Dark'
+ end
+
+ it "should paginate book lists from a user's shelf" do
+ stub_with_key_get('/review/list/1.xml',
+ {:shelf => 'to-read', :v=> '2', :page => '2'},
+ 'to-read-p2.xml')
+ proc { @shelf = @client.shelf('1', 'to-read', :page => 2) }.should_not raise_error
+ @shelf.start.should == 21
+ @shelf.end.should == 40
+ @shelf.total.should == 40
+ @shelf.books.length.should == 20
+ @shelf.books.first.id.should == '107804211'
+ @shelf.books.first.book.title.should =~ /Your Money or Your Life/
+ end
+
+ it "should return an empty array for empty shelves" do
+ stub_with_key_get('/review/list/1.xml',
+ {:shelf => 'to-read', :v=> '2'},
+ 'empty.xml')
+ proc { @shelf = @client.shelf('1', 'to-read') }.should_not raise_error
+ @shelf.start.should == 0
+ @shelf.end.should == 0
+ @shelf.total.should == 0
+ @shelf.books.length.should == 0
+ end
end
View
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<GoodreadsResponse>
+ <Request>
+ <authentication>true</authentication>
+ <key><![CDATA[SECRET_KEY]]></key>
+ <method><![CDATA[review_list]]></method>
+ </Request>
+ <reviews start="0" end="0" total="0"> </reviews>
+
+</GoodreadsResponse>
View

Large diffs are not rendered by default.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit d59993d

Please sign in to comment.