Skip to content

Instantly share code, notes, and snippets.

@cpetersen
Created October 25, 2008 20:24
Show Gist options
  • Save cpetersen/19774 to your computer and use it in GitHub Desktop.
Save cpetersen/19774 to your computer and use it in GitHub Desktop.
Datamapper Book class that works with isbndb.com
require 'hpricot'
require 'open-uri'
class Book
include DataMapper::Resource
validates_is_unique :isbn
property :id, Serial
property :title, String
property :title_long, String
property :isbn, String
property :author, String
property :publisher_text, String
def self.find_by_isbn(isbn)
book = Book.first(:isbn => isbn)
unless book
book = Book.new(:isbn => isbn)
doc = Hpricot.parse(open("http://isbndb.com/api/books.xml?access_key=#{Book.access_key}&index1=isbn&value1=#{isbn}"))
book.title = (doc/:title)[0].inner_text
book.title_long = (doc/:titlelong)[0].inner_text
book.author = (doc/:authorstext)[0].inner_text
book.publisher_text = (doc/:publishertext)[0].inner_text
book.save
end
book
end
def self.access_key
# YOUR ISBNDB.COM ACCESS KEY HERE
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment