Permalink
Browse files

update scripts to handle multiple library cards

since I was always running multiple cards I might as well make taht the
default behaviour.
  • Loading branch information...
1 parent 376a9d8 commit 0a6153362a84ecac049ff4c39256c08d1ce6feff @ChrisNolan committed Aug 26, 2012
Showing with 91 additions and 15 deletions.
  1. +1 −1 .gitignore
  2. +9 −4 README.markdown
  3. +3 −0 lib/your_account.rb
  4. +42 −0 lib/your_accounts.rb
  5. +33 −0 spec/your_accounts_spec.rb
  6. +3 −10 tpl-checked-out.rb
View
@@ -5,7 +5,7 @@
*.pydevproject
.project
.metadata
-bin/
+#bin/
tmp/
*.tmp
*.bak
View
@@ -4,6 +4,8 @@ Athenaeum -- A Toronto Public Library & Goodreads.com Interface
I've a heavy user of the [Toronto Public Library](http://tpl.ca/) and recently started using [Goodreads.com](http://goodreads.com/), so I decided to join them together.
For now I just automatically take my checked out items and add them to some shelves on goodreads.
+Read any [news about the project at my site](http://chrisnolan.ca/tag/athenaeum/).
+
How to Use
----------
@@ -14,19 +16,22 @@ The scripts and program libraries are written in [Ruby (v1.9.3)](http://www.ruby
* [oauth](http://rubygems.org/gems/oauth) (0.4.6)
* [rest-client](https://rubygems.org/gems/rest-client) (1.6.7)
-Copy the tpl.yml.example file to your home directory and name it .tpl; edit it with your values. If you manage multiple cards, then you'll have to set the 2nd (or more cards) in the 'tpl-checked-out.rb' script itself.
-Run the 'oauth_setup.rb' script in the project directory (i.e. "ruby oauth_setup.rb") and follow the prompts to create your HOME/.goodreads yaml config file
+After those are installed, manually:
+* Copy the tpl.yml.example file to your home directory and name it .tpl; edit it with your values. If you manage multiple cards, seperate them with commas.
+* Run the 'oauth_setup.rb' script in the project directory (i.e. "ruby oauth_setup.rb") and follow the prompts to create your HOME/.goodreads yaml config file.
+
+### Tests
To run the tests, do a 'rake test' from the project directory. Note: for now many of the tests are set to 'skip' as they aren't mocked against the web. You can comment out the skip line from each test you want to confirm.
### Regular use
-Just do a "ruby tpl-checked-out.rb" and it'll pull down your checked out items and push them over to goodreads for you.
+Just do a "ruby tpl-checked-out.rb" and it'll pull down your checked out items and push them over to goodreads for you.
Then goto your "Bookshelves" on Goodreads and you'll see the "checked-out" shelf.
Known Issues
------------
-
+* if a book is on another exclusive shelf it's erroring? (In my case, I 'returned-without-reading' but then checked it out again)
ToDo
----
View
@@ -23,6 +23,9 @@ def initialize(library_card=nil, library_password=nil)
end
raise "Library Card # required" if @library_card.nil?
raise "Library PIN required" if @library_password.nil?
+ # if @library_card/password has a comma in it, split it apart and just take the first one
+ @library_card = @library_card.to_s.split(',').map(&:strip).first
+ @library_password = @library_password.to_s.split(',').map(&:strip).first
end
def connect
View
@@ -0,0 +1,42 @@
+require 'rubygems'
+require 'yaml'
+require_relative "../lib/your_account.rb"
+
+class YourAccounts
+
+ attr_reader :library_cards, :library_passwords, :accounts
+
+ def initialize(library_cards=nil, library_passwords=nil)
+ unless library_cards && library_passwords
+ # automatically load personal account credentials from the yaml file either in your current directory, or your home
+ [ [File.dirname(__FILE__), 'tpl.yml'], [ENV['HOME'], '.tpl'] ].each do |path|
+ if File.exists?( file = File.join(*path) )
+ creds = YAML::load_file(file) and (library_cards, library_passwords = creds['library_card'], creds['pin']) and break
+ end
+ end
+ raise Errno::ENOENT, "tpl yaml library_card config" if library_cards.nil?
+ else
+ end
+ @library_cards = library_cards.to_s.split(',').map(&:strip)
+ @library_passwords = library_passwords.to_s.split(',').map(&:strip)
+ raise "Library Card # required" if @library_cards.nil?
+ raise "Library PIN required" if @library_passwords.nil?
+ init_accounts
+ end
+
+ def init_accounts
+ @accounts = []
+ @library_cards.each_with_index do |library_card, card_index|
+ @accounts << YourAccount.new(library_card, @library_passwords[card_index])
+ end
+ end
+
+ def checked_out
+ total_checked_out = []
+ @accounts.each do |your_account|
+ total_checked_out += your_account.checked_out
+ end
+ total_checked_out
+ end
+
+end
@@ -0,0 +1,33 @@
+require 'minitest/autorun'
+require_relative "../lib/your_accounts.rb"
+
+describe YourAccounts do
+
+ describe 'initialize' do
+ it 'handles a single card/pass passed to it' do
+ your_accounts = YourAccounts.new("card #","password")
+ your_accounts.library_cards.first.must_equal "card #"
+ your_accounts.library_passwords.last.must_equal "password"
+ your_accounts.accounts.size.must_equal 1
+ end
+ it 'handles multiple cards/passwords passed to it' do
+ your_accounts = YourAccounts.new("card #1, card #2","first password, second password")
+ your_accounts.library_cards.first.must_equal "card #1"
+ your_accounts.library_passwords.last.must_equal "second password"
+ your_accounts.accounts.size.must_equal 2
+ end
+ it 'handles a single card in the config file'
+ it 'handles multiple cards in the config file'
+ it 'errors appropriately with no cards in the config file'
+ it 'errors appropriately with no config file'
+ end
+
+ describe 'checked_out' do
+ it 'returns a list of checked out items across all cards' do
+ skip # until I can mock the retrieve properly -- otherwise un skip and edit the .tpl file to give different accounts
+ your_accounts = YourAccounts.new
+ your_accounts.checked_out.size.must_equal 30
+ end
+ end
+
+end
View
@@ -1,19 +1,12 @@
-load"lib/your_account.rb"
+load"lib/your_accounts.rb"
load"lib/library_detail.rb"
load"lib/goodreads.rb"
goodreads = Goodreads.new
goodreads.required_shelves_for_library_link
-your_account=YourAccount.new # card info coming from home/.tpl
-your_account.retrieve#_stub # _stub of course is just during testing
-total_checked_out = your_account.checked_out
-# If you don't manage multiple cards comment out the new few lines, otherwise fill in the values
-your_account=nil
-your_account=YourAccount.new("2nd library card", "2nd library card password") # get 2nd card # could I put both in the yaml file instead?
-your_account.retrieve
-total_checked_out += your_account.checked_out
-total_checked_out.each do |checkout|
+your_accounts=YourAccounts.new # card info coming from home/.tpl
+your_accounts.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

0 comments on commit 0a61533

Please sign in to comment.