Permalink
Browse files

Books can find & store titles with ISBNdb api, and we only pull in & …

…link to books that were found by the API
  • Loading branch information...
1 parent 3c512f8 commit fae3f57603a44e35ad8a940c88b49eefbf7f0866 @joemsak joemsak committed Jan 5, 2011
View
@@ -7,6 +7,7 @@ gem 'rails', '3.0.3'
gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'friendly_id'
+gem 'crack'
# Use unicorn as the web server
# gem 'unicorn'
View
@@ -31,6 +31,7 @@ GEM
arel (2.0.6)
babosa (0.2.0)
builder (2.1.2)
+ crack (0.1.8)
erubis (2.6.6)
abstract (>= 1.0.0)
friendly_id (3.1.8)
@@ -72,6 +73,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ crack
friendly_id
rails (= 3.0.3)
sqlite3-ruby
View
@@ -1,5 +1,18 @@
class Book < ActiveRecord::Base
belongs_to :account
+
+ default_scope where(['value != ?', "No title found"])
+
+ def self.add_title xml, book
+ res = Crack::XML.parse(xml)
+ possible_book = res["ISBNdb"]["BookList"]
+ if possible_book["BookData"]
+ title = possible_book["BookData"]["Title"]
+ else
+ title = "No title found"
+ end
+ book.update_attribute('value', title)
+ end
end
# == Schema Information
@@ -1 +1 @@
-<%= link_to book.key, "http://isbndb.com/search-all.html?kw=#{book.key}" %><br>
+<%= link_to book.value, "http://isbndb.com/search-all.html?kw=#{book.key}" %><br>
@@ -0,0 +1,9 @@
+class AddValueToBooks < ActiveRecord::Migration
+ def self.up
+ add_column :books, :value, :string
+ end
+
+ def self.down
+ remove_column :books, :value
+ end
+end
View
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110105013246) do
+ActiveRecord::Schema.define(:version => 20110105022753) do
create_table "accounts", :force => true do |t|
t.string "username"
@@ -24,6 +24,7 @@
t.integer "account_id"
t.datetime "created_at"
t.datetime "updated_at"
+ t.string "value"
end
create_table "names", :force => true do |t|
@@ -0,0 +1,12 @@
+require 'open-uri'
+
+desc "find & store the book titles"
+task :book_titles => :environment do
+ Book.all.each do |book|
+ api_url = "http://isbndb.com/api/books.xml?access_key=AIDOFPMK&index1=isbn&value1="
+ unless book.value.present?
+ api_url += book.key
+ Book.add_title open(api_url), book
+ end
+ end
+end

0 comments on commit fae3f57

Please sign in to comment.