Created
October 29, 2009 12:42
-
-
Save ambethia/221414 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require "httparty" | |
require "isbn" | |
module Book; end unless defined? Book | |
module Book::Search | |
include HTTParty | |
base_uri "books.google.com"; format :xml | |
class << self | |
attr_reader :asin, :book | |
def [](asin) | |
@asin = asin | |
feed = get("/books/feeds/volumes", :query => { :q => "isbn:#{asin}" })["feed"] | |
@book = feed ? feed["entry"] : nil | |
return @book ? self : nil | |
end | |
def extract(field) | |
entry = book.is_a?(Array) ? book[0]["dc:#{field}"] : book["dc:#{field}"] | |
return entry.is_a?(Array) ? entry[0] : entry | |
rescue | |
"" | |
end | |
def method_missing(method) | |
extract(method) | |
end | |
def copyright | |
date.to_i | |
end | |
def author | |
creator | |
end | |
def isbn | |
ISBN.thirteen(asin) | |
end | |
end | |
end | |
if __FILE__ == $0 | |
require 'test/unit' | |
class BookSearchTest < Test::Unit::TestCase | |
def setup | |
@book = Book::Search["0674880145"] | |
end | |
def test_isbn | |
assert_equal "9780674880146", @book.isbn | |
end | |
def test_author | |
assert_equal "John Rawls", @book.author | |
end | |
def test_title | |
assert_equal "A Theory of Justice", @book.title | |
end | |
def test_publisher | |
assert_equal "Harvard Univ Pr", @book.publisher | |
end | |
def test_copyright | |
assert_equal 1971, @book.copyright | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment