Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Commit

Permalink
added bundler, started on author support
Browse files Browse the repository at this point in the history
  • Loading branch information
tonywok committed Sep 7, 2010
1 parent d5870e7 commit f8497e1
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 82 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
tmp
pkg
.bundle

19 changes: 19 additions & 0 deletions Gemfile
@@ -0,0 +1,19 @@
source "http://rubygems.org"

gem 'httparty'

group :development do
gem "ruby-debug"
end

group :test do
gem 'factory_girl'
gem 'fakeweb'
gem 'faker'
gem 'rspec', '~>2.0.0.beta.20'
gem 'capybara'
gem 'database_cleaner'
gem 'cucumber'
gem 'spork'
gem 'launchy'
end
80 changes: 80 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,80 @@
GEM
remote: http://rubygems.org/
specs:
builder (2.1.2)
capybara (0.3.9)
culerity (>= 0.2.4)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
selenium-webdriver (>= 0.0.3)
columnize (0.3.1)
configuration (1.1.0)
crack (0.1.8)
cucumber (0.8.5)
builder (~> 2.1.2)
diff-lcs (~> 1.1.2)
gherkin (~> 2.1.4)
json_pure (~> 1.4.3)
term-ansicolor (~> 1.0.4)
culerity (0.2.12)
database_cleaner (0.5.2)
diff-lcs (1.1.2)
factory_girl (1.3.2)
faker (0.3.1)
fakeweb (1.3.0)
ffi (0.6.3)
rake (>= 0.8.7)
gherkin (2.1.5)
trollop (~> 1.16.2)
httparty (0.6.1)
crack (= 0.1.8)
json_pure (1.4.6)
launchy (0.3.7)
configuration (>= 0.0.5)
rake (>= 0.8.1)
linecache (0.43)
mime-types (1.16)
nokogiri (1.4.3.1)
rack (1.2.1)
rack-test (0.5.4)
rack (>= 1.0)
rake (0.8.7)
rspec (2.0.0.beta.20)
rspec-core (= 2.0.0.beta.20)
rspec-expectations (= 2.0.0.beta.20)
rspec-mocks (= 2.0.0.beta.20)
rspec-core (2.0.0.beta.20)
rspec-expectations (2.0.0.beta.20)
diff-lcs (>= 1.1.2)
rspec-mocks (2.0.0.beta.20)
ruby-debug (0.10.3)
columnize (>= 0.1)
ruby-debug-base (~> 0.10.3.0)
ruby-debug-base (0.10.3)
linecache (>= 0.3)
rubyzip (0.9.4)
selenium-webdriver (0.0.28)
ffi (>= 0.6.1)
json_pure
rubyzip
spork (0.8.4)
term-ansicolor (1.0.5)
trollop (1.16.2)

PLATFORMS
ruby

DEPENDENCIES
capybara
cucumber
database_cleaner
factory_girl
faker
fakeweb
httparty
launchy
rspec (~> 2.0.0.beta.20)
ruby-debug
spork
9 changes: 4 additions & 5 deletions lib/book_worm.rb
@@ -1,5 +1,4 @@
require 'httparty'
require 'cgi'
require 'book_worm/searchable'
require 'book_worm/book'

module BookWorm
require 'book_worm/configuration'
require 'book_worm/searchable'
end
55 changes: 5 additions & 50 deletions lib/book_worm/book.rb
@@ -1,7 +1,9 @@
module BookWorm
class Book < Searchable

INDICES = [:isbn, :title, :combined, :full]
class Book
include Searchable
include HTTParty
format :xml
base_uri 'http://isbndb.com'

attr_accessor :publisher, :authors, :isbn,
:isbn13, :title, :long_title,
Expand All @@ -17,53 +19,6 @@ def initialize(book_data)
self.book_id = book_data["book_id"]
end

class << self

INDICES.each do |index|
index_name = (index.to_s =~ /full|combined/ ? "#{index}_index" : index)

define_method "find_by_#{index_name}" do |arg|
find(index, arg)
end

define_method "find_all_by_#{index_name}" do |arg|
find_all(index, arg)
end
end

# possible index/value pairs:
# isbn - pass in a valid isbn or isbn13 value.
# title - keyword search on title, long title and latinized title.
# combined - search across titles, authors, and publisher name
# full - search across itles, authors, publisher name, summary,
# notes, awards information, etc
def find(index, value)
normalize(query_isbndb(index, value))[0]
end

def find_all(index, value)
normalize(query_isbndb(index, value))
end

private

def normalize(results)
begin
[results['ISBNdb']['BookList']['BookData']].flatten.collect do |book|
Book.new(book)
end
rescue
[]
end
end

def query_isbndb(index, value)
get(query_base, :query => { :index1 => index,
:value1 => value,
:access_key => "2I9YKZ6J"})
end

end
end
end

55 changes: 49 additions & 6 deletions lib/book_worm/searchable.rb
Expand Up @@ -2,13 +2,56 @@
require 'net/http'

module BookWorm
class Searchable
include HTTParty
format :xml
base_uri 'http://isbndb.com'
module Searchable
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods

INDICES = [:isbn, :title, :combined, :full]

INDICES.each do |index|
index_name = (index.to_s =~ /full|combined/ ? "#{index}_index" : index)

define_method "find_by_#{index_name}" do |arg|
find(index, arg)
end

define_method "find_all_by_#{index_name}" do |arg|
find_all(index, arg)
end
end

def find(index, value)
normalize(query_isbndb(index, value))[0]
end

def find_all(index, value)
normalize(query_isbndb(index, value))
end

def query_base
"/api/#{self.to_s.gsub(/^\w+::/, '').downcase << 's'}.xml"
end

def query_isbndb(index, value)
get(query_base, :query => { :index1 => index,
:value1 => value,
:access_key => Configuration::API_KEY })
end

def normalize(results)
begin
[results['ISBNdb']['BookList']['BookData']].flatten.collect do |book|
debugger
Book.new(book)
end
rescue
[]
end
end

def self.query_base
"/api/#{self.to_s.gsub(/^\w+::/, '').downcase << 's'}.xml"
end
end
end
Expand Down
63 changes: 42 additions & 21 deletions spec/book_spec.rb
@@ -1,5 +1,6 @@
require 'spec_helper'
require 'lib/book_worm'
require 'lib/book_worm/book'

describe BookWorm do
describe '#find_book' do
Expand All @@ -9,12 +10,18 @@
end
end

# TODO: Decide if I want to support this.
# context 'when finding without a specified index' do
# it 'defaults to an isbn index' do
# BookWorm::Book.find('1934356085').isbn.should == '1934356085'
# end
# end
context 'when a search uses a non-existant index' do
it 'throws an exception' do
pending
end
end

context 'when finding without a specified index' do
it 'defaults to an isbn index' do
pending
# BookWorm::Book.find('1934356085').isbn.should == '1934356085'
end
end

before :each do
@book_data = { :isbn => "1934356085",
Expand Down Expand Up @@ -75,6 +82,16 @@
end
end

context 'when querying for a book by author' do

before { @author = @book_data[:authors].split(', ').first }

it 'finds the first book that matches the specified author' do
result = BookWorm::Book.find(:author, @author)
result.authors.should =~ /#{@author}/
end
end

context 'when querying for a book by combined index' do
before :each do
@combined_index = "#{@book_data[:title]} #{@book_data[:publisher]} #{@book_data[:authors]}"
Expand Down Expand Up @@ -116,7 +133,7 @@
end
end
end
end
end # end find

describe '#find_all' do
context 'when querying for a collection of books' do
Expand All @@ -125,12 +142,19 @@
end
end

# TODO: Decide if I want to support this.
# context 'when finding without a specified index' do
# it 'defaults to an isbn index' do
# BookWorm::Book.find('1934356085').isbn.should == '1934356085'
# end
# end
context 'when finding without a specified index' do
it 'defaults to an isbn index' do
pending
# BookWorm::Book.find('1934356085').isbn.should == '1934356085'
end
end

context 'when finding without a specified index' do
it 'defaults to an isbn index' do
pending
# BookWorm::Book.find('1934356085').isbn.should == '1934356085'
end
end

context 'when querying for a collection of books by isbn' do
it 'indexes by isbn' do
Expand Down Expand Up @@ -197,19 +221,16 @@

context 'when querying for a collection of books by a full index' do
it 'has results that match the full index' do
BookWorm::Book.find_all(:full, 'Ruby Rails').each do |book|
book.title.should =~ /ruby|rails/i
end
books = BookWorm::Book.find_all(:full, 'Ruby Rails Pragmatic Programming')
books.collect{ |book| book.title }.join('_').should =~ /ruby|rails/i
end

context 'when using #find_books helpers' do
it 'has results that contain the arguments for full index search' do
summary = "Ruby Rails Pragmatic Programming"
BookWorm::Book.find_all_by_full_index(summary).each do |book|
book.title.should =~ /ruby|rails/i
end
books = BookWorm::Book.find_all_by_full_index('Ruby Rails Pragmatic Programming')
books.collect{ |book| book.title }.join('_').should =~ /ruby|rails/i
end
end
end
end
end #end find_all
end

0 comments on commit f8497e1

Please sign in to comment.