Permalink
Browse files

the 'currently-checked-out' shelf

Added 'book_ids_on_shelf', 'remove_from_shelf', and 'empty_shelf' to the
goodreads library so I can have a single shelf with my
currently-checked-out items
  • Loading branch information...
1 parent 5aeae23 commit 212784c04fd8741b00badbba6db2b9d1301e933a @ChrisNolan committed Aug 26, 2012
Showing with 74 additions and 3 deletions.
  1. +8 −1 bin/tpl-checked-out.rb
  2. +34 −1 lib/goodreads.rb
  3. +32 −1 spec/goodreads_spec.rb
@@ -2,11 +2,17 @@
load"lib/library_detail.rb"
load"lib/goodreads.rb"
+puts "Connecting to Goodreads..."
goodreads = Goodreads.new
goodreads.required_shelves_for_library_link
+puts "Clearing 'currently-checked-out'..."
+goodreads.empty_shelf('currently-checked-out')
your_accounts=YourAccounts.new # card info coming from home/.tpl
-your_accounts.checked_out.each do |checkout|
+puts "Retrieving account info..."
+checked_out = your_accounts.checked_out
+puts "Processing #{checked_out.size} items..."
+checked_out.each do |checkout|
next unless checkout.local_format == "Book" # skip DVDs and CDs and magazines etc
library_detail = LibraryDetail.retrieve_stub(checkout.library_id) # _stub for testing
goodreads_book_id = goodreads.book_isbn_to_id library_detail.isbn
@@ -18,6 +24,7 @@
shelves << 'checked-out'
shelves << "checked-out-#{Time.now.year}"
#goodreads.add_to_shelf "checked-out-#{Time.now.year}-#{Time.now.month}", goodreads_book_id # track what was checked out each month... but first add check to walk back 3 weeks from the renew date so it doesn't fill multiple months un-necessarily
+ shelves << 'currently-checked-out'
goodreads.add_to_shelf shelves, goodreads_book_id
else
puts " *** Unable to get BookID for #{checkout.title}"
View
@@ -99,19 +99,52 @@ def required_shelves(shelves)
shelves.each {|shelf| add_shelf(shelf) unless shelf_names.include?(shelf)}
end
- def add_to_shelf(names, book_id)
+ def add_to_shelf(names, book_id, a=nil)
# TODO remove any duplicates from the shelves it's already on to avoid the extra api calls
names = [names] if names.kind_of? String
names.each do |name|
sleep 0.1 # API requires me not to hammer their system sleep 1
response = @access_token.post('/shelf/add_to_shelf.xml ', {
'name' => name,
'book_id' => book_id,
+ 'a' => a,
})
# TODO check the response code
end
end
+ def remove_from_shelf(names, book_id)
+ add_to_shelf(names, book_id, 'remove')
+ end
+
+ def empty_shelf(shelf)
+ books = book_ids_on_shelf(shelf)
+ books.each do |book_id|
+ remove_from_shelf(shelf, book_id)
+ end
+ end
+
+ def book_ids_on_shelf(shelf)
+ # http://www.goodreads.com/api#reviews.list
+ response = @access_token.post("/review/list/#{@user_id}.xml", {
+ 'v' => '2',
+ 'shelf' => shelf,
+ 'per_page' => 200,
+ })
+ hash = Hash.from_xml(response.body)[:GoodreadsResponse]
+ hash.delete(:Request)
+ reviews_list = hash
+ book_ids = []
+ if reviews_list[:reviews].kind_of? Hash
+ # TODO test to make sure the <reviews total="x"> (reviews_list[:reviews][:attributes][:total) isn't higher than I asked for -- otherwise will have to retrieve multiple pages worth
+ reviews = reviews_list[:reviews][:review]
+ reviews.each do |review|
+ book_ids << review[:book][:id]
+ end
+ end
+ book_ids
+ end
+
def required_shelves_for_library_link
shelves = %w(checked-out currently-checked-out on-hold)
shelves << "checked-out-#{Time.now.year}"
@@ -70,7 +70,8 @@
skip "so I don't hit the api when running my test for now"
goodreads = Goodreads.new
goodreads.add_to_shelf("test-required-shelf-2", "4514")
- # TODO actually confirm the given book is on that shelf -- for now I'm just looking on the website
+ goodreads.shelf_names_by_book("4514").must_include "test-required-shelf-2"
+ goodreads.remove_from_shelf("test-required-shelf-2", "4514")
end
it "adds a book to all the given shelves" do
skip "so I don't hit the api when running tests for now"
@@ -81,5 +82,35 @@
shelves.must_include "ruby-computer-programming-language"
end
end
+ describe "remove from shelves" do
+ it 'removes a book from a given shelf' do
+ skip "so I don't hit the api when running my test for now"
+ goodreads = Goodreads.new
+ goodreads.add_to_shelf("test-required-shelf-2", "4514")
+ goodreads.shelf_names_by_book("4514").must_include "test-required-shelf-2"
+ goodreads.remove_from_shelf("test-required-shelf-2", "4514")
+ goodreads.shelf_names_by_book("4514").wont_include "test-required-shelf-2"
+ end
+ it 'gets list of books on a shelf' do
+ skip "so I don't hit the api when running my test for now"
+ goodreads = Goodreads.new
+ goodreads.add_to_shelf("test-required-shelf-2", "4514")
+ goodreads.add_to_shelf("test-required-shelf-2", "285679")
+ books = goodreads.book_ids_on_shelf('test-required-shelf-2')
+ books.size.must_equal 2
+ books.must_include 4514
+ goodreads.remove_from_shelf("test-required-shelf-2", "4514")
+ goodreads.remove_from_shelf("test-required-shelf-2", "285679")
+ end
+ it 'removes all books from a given shelf' do
+ skip "so I don't hit the api when running my test for now"
+ goodreads = Goodreads.new
+ goodreads.add_to_shelf("test-required-shelf-2", "4514")
+ goodreads.add_to_shelf("test-required-shelf-2", "285679")
+ goodreads.empty_shelf('test-required-shelf-2')
+ books = goodreads.book_ids_on_shelf('test-required-shelf-2')
+ books.size.must_equal 0
+ end
+ end
end
end

0 comments on commit 212784c

Please sign in to comment.