book_worm 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ tmp
2
+ pkg
3
+ .bundle
4
+
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format=documentation
data/.rvmrc ADDED
@@ -0,0 +1,2 @@
1
+ rvm use --create ruby-1.9.2@book_worm
2
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ #use this, specify dependencies in gmespec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ book_worm (0.0.1)
5
+ httparty
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ crack (0.1.8)
11
+ diff-lcs (1.1.2)
12
+ httparty (0.6.1)
13
+ crack (= 0.1.8)
14
+ rspec (2.0.0.beta.20)
15
+ rspec-core (= 2.0.0.beta.20)
16
+ rspec-expectations (= 2.0.0.beta.20)
17
+ rspec-mocks (= 2.0.0.beta.20)
18
+ rspec-core (2.0.0.beta.20)
19
+ rspec-expectations (2.0.0.beta.20)
20
+ diff-lcs (>= 1.1.2)
21
+ rspec-mocks (2.0.0.beta.20)
22
+
23
+ PLATFORMS
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ book_worm!
28
+ rspec
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'bundler'
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ Bundler::GemHelper.install_tasks
data/book_worm.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "book_worm/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "book_worm"
7
+ s.version = BookWorm::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Tony Schneider"]
10
+ s.email = ["tonywok@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{A simple gem for interacting with ISBNDB.com}
13
+ s.description = %q{A simple gem for interacting with ISBNDB.com}
14
+
15
+ s.rubyforge_project = "book_worm"
16
+
17
+ s.add_runtime_dependency "httparty"
18
+ s.add_development_dependency "rspec"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+ end
@@ -0,0 +1,18 @@
1
+ module BookWorm
2
+ class Book
3
+
4
+ attr_accessor :publisher, :authors, :isbn,
5
+ :isbn13, :title, :long_title,
6
+ :book_id
7
+
8
+ def initialize(book_data)
9
+ self.long_title = book_data["TitleLong"]
10
+ self.publisher = book_data["PublisherText"]
11
+ self.authors = book_data["AuthorsText"]
12
+ self.isbn = book_data["isbn"]
13
+ self.isbn13 = book_data["isbn13"]
14
+ self.title = book_data["Title"]
15
+ self.book_id = book_data["book_id"]
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ module BookWorm
2
+ class Configuration
3
+ # supply your ISBNDB.com API key here
4
+ API_KEY = ''
5
+ end
6
+ end
@@ -0,0 +1,70 @@
1
+ module BookWorm
2
+ require 'book_worm/configuration'
3
+ require 'book_worm/book'
4
+ require 'httparty'
5
+
6
+ include HTTParty
7
+ format :xml
8
+ base_uri 'http://isbndb.com'
9
+
10
+ module Searchable
11
+
12
+ INDICES = [:isbn, :title, :combined, :full]
13
+
14
+ INDICES.each do |index|
15
+ index_name = (index.to_s =~ /full|combined/ ? "#{index}_index" : index)
16
+
17
+ define_method "find_by_#{index_name}" do |arg|
18
+ find(index, arg)
19
+ end
20
+
21
+ define_method "find_all_by_#{index_name}" do |arg|
22
+ find_all(index, arg)
23
+ end
24
+ end
25
+
26
+ def find(*args)
27
+ if args.size == 1
28
+ index = :isbn
29
+ value = args[0]
30
+ else
31
+ index = INDICES.include?(args[0]) ? args[0] : :isbn
32
+ value = args[1]
33
+ end
34
+ normalize(query_isbndb(index, value))[0]
35
+ end
36
+
37
+ def find_all(*args)
38
+ if args.size == 1
39
+ index = :full
40
+ value = args[0]
41
+ else
42
+ index = INDICES.include?(args[0]) ? args[0] : :full
43
+ value = args[1]
44
+ end
45
+ normalize(query_isbndb(index, value))
46
+ end
47
+
48
+ private
49
+
50
+ def query_base
51
+ "/api/books.xml"
52
+ end
53
+
54
+ def query_isbndb(index, value)
55
+ get(query_base, :query => { :index1 => index,
56
+ :value1 => value,
57
+ :access_key => Configuration::API_KEY, })
58
+ end
59
+
60
+ def normalize(results)
61
+ begin
62
+ [results['ISBNdb']['BookList']['BookData']].flatten.collect do |book|
63
+ Book.new(book)
64
+ end
65
+ rescue
66
+ []
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ module BookWorm
2
+ VERSION = "0.0.2"
3
+ end
data/lib/book_worm.rb ADDED
@@ -0,0 +1,4 @@
1
+ module BookWorm
2
+ require 'book_worm/searchable'
3
+ extend Searchable
4
+ end
data/spec/book_spec.rb ADDED
@@ -0,0 +1,229 @@
1
+ require 'spec_helper'
2
+ require 'book_worm'
3
+
4
+ describe BookWorm do
5
+ describe '#find' do
6
+ context 'when a search results in no matches' do
7
+ it 'returns nil' do
8
+ BookWorm.find(:isbn, 'Jiveturkey').should be_nil
9
+ end
10
+ end
11
+
12
+ context 'when finding without a specified index' do
13
+ it 'defaults to an isbn index' do
14
+ BookWorm.find('1934356085').isbn.should == '1934356085'
15
+ end
16
+ end
17
+
18
+ context 'when finding with a non-supported index' do
19
+ it 'defaults to an isbn index' do
20
+ BookWorm.should_receive(:query_isbndb).with(:isbn, '1934356085')
21
+ BookWorm.find(:jiveturkey, '1934356085')
22
+ end
23
+ end
24
+
25
+ before :each do
26
+ @book_data = { :isbn => "1934356085",
27
+ :isbn13 => "9781934356081",
28
+ :book_id => "programming_ruby_1_9",
29
+ :publisher => "Pragmatic Bookshelf",
30
+ :title => 'Programming Ruby 1.9',
31
+ :long_title => "Programming Ruby 1.9: The Pragmatic Programmers' Guide",
32
+ :authors => "Dave Thomas, Chad Fowler, Andy Hunt, " }
33
+ end
34
+
35
+ context 'when querying for a book by isbn' do
36
+ it 'indexes by isbn.' do
37
+ result = BookWorm.find(:isbn, @book_data[:isbn])
38
+ @book_data.each do |key,val|
39
+ result.send(key).should == val
40
+ end
41
+ end
42
+
43
+ it 'indexes by isbn13.' do
44
+ result = BookWorm.find(:isbn, @book_data[:isbn13])
45
+ @book_data.each do |key,val|
46
+ result.send(key).should == val
47
+ end
48
+ end
49
+
50
+ context 'when using #find_book hepers' do
51
+ it 'indexes by isbn more easily.' do
52
+ result = BookWorm.find_by_isbn(@book_data[:isbn])
53
+ @book_data.each do |key,val|
54
+ result.send(key).should == val
55
+ end
56
+ end
57
+
58
+ it 'indexes by isbn13 more conveniently.' do
59
+ result = BookWorm.find_by_isbn(@book_data[:isbn13])
60
+ @book_data.each do |key,val|
61
+ result.send(key).should == val
62
+ end
63
+ end
64
+ end
65
+
66
+ context 'when querying for a book by title' do
67
+ it 'indexes books by title' do
68
+ result = BookWorm.find(:title, @book_data[:title])
69
+ @book_data.each do |key,val|
70
+ result.send(key).should == val
71
+ end
72
+ end
73
+
74
+ context 'when using #find_book helpers' do
75
+ it 'indexes by title more conveniently.' do
76
+ result = BookWorm.find_by_title(@book_data[:title])
77
+ @book_data.each do |key,val|
78
+ result.send(key).should == val
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ context 'when querying for a book by combined index' do
85
+ before :each do
86
+ @combined_index = "#{@book_data[:title]} #{@book_data[:publisher]} #{@book_data[:authors]}"
87
+ end
88
+
89
+ it 'indexes using several combined fields' do
90
+ @combined_index = "#{@book_data[:title]} #{@book_data[:publisher]} #{@book_data[:authors]}"
91
+ result = BookWorm.find(:combined, @combined_index)
92
+ @book_data.each do |key,val|
93
+ result.send(key).should == val
94
+ end
95
+ end
96
+
97
+ context 'when querying for a book by combined index' do
98
+ it 'uses combined index more conveniently.' do
99
+ result = BookWorm.find_by_combined_index(@combined_index)
100
+ @book_data.each do |key,val|
101
+ result.send(key).should == val
102
+ end
103
+ end
104
+ end
105
+ end
106
+
107
+ context 'when querying for a book by full index' do
108
+ before :each do
109
+ @summary = "making Ruby a favorite tool of intelligent, forward-thinking programmers"
110
+ end
111
+
112
+ it 'indexes using several full fields' do
113
+ result = BookWorm.find(:full, @summary)
114
+ @book_data.each do |key,val|
115
+ result.send(key).should == val
116
+ end
117
+ end
118
+
119
+ context 'when querying for a book by full index' do
120
+ it 'uses full index more conveniently.' do
121
+ result = BookWorm.find_by_full_index(@summary)
122
+ @book_data.each do |key,val|
123
+ result.send(key).should == val
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end # end find
130
+
131
+ describe '#find_all' do
132
+ context 'when querying for a collection of books' do
133
+ it 'returns an empty array when no results are found' do
134
+ BookWorm.find_all(:isbn, 'Jiveturkey').should be_empty
135
+ end
136
+ end
137
+
138
+ context 'when finding without a specified index' do
139
+ it 'defaults to full index' do
140
+ BookWorm.should_receive(:query_isbndb).with(:full, "foobarbaz")
141
+ BookWorm.find_all('foobarbaz')
142
+ end
143
+ end
144
+
145
+ context 'when finding with a non-supported index' do
146
+ it 'defaults to a full index' do
147
+ BookWorm.should_receive(:query_isbndb).with(:full, '1934356085')
148
+ BookWorm.find_all(:jiveturkey, '1934356085')
149
+ end
150
+ end
151
+
152
+ context 'when querying for a collection of books by isbn' do
153
+ it 'indexes by isbn' do
154
+ BookWorm.find_all(:isbn, '1934356085').each do |book|
155
+ book.isbn.should == '1934356085'
156
+ end
157
+ end
158
+
159
+ it 'indexes by isbn13' do
160
+ BookWorm.find_all(:isbn, '9781934356081').each do |book|
161
+ book.isbn13.should == '9781934356081'
162
+ end
163
+ end
164
+
165
+ context 'when using #find_all convenience helpers' do
166
+ it 'conveniently indexes by isbn' do
167
+ BookWorm.find_all_by_isbn('1934356085').each do |book|
168
+ book.isbn.should == '1934356085'
169
+ end
170
+ end
171
+
172
+ it 'conveniently indexes by isbn13' do
173
+ BookWorm.find_all_by_isbn('9781934356081').each do |book|
174
+ book.isbn13.should == '9781934356081'
175
+ end
176
+ end
177
+ end
178
+ end
179
+
180
+ context 'when querying for a collection of books by title' do
181
+ it 'has results that contain the title argument' do
182
+ BookWorm.find_all(:title, 'Ruby Rails').each do |book|
183
+ book.title.should =~ /ruby|rails/i
184
+ end
185
+ end
186
+
187
+ context 'when using #find_books helpers' do
188
+ it 'has results that contain the title argument' do
189
+ BookWorm.find_all_by_title('Ruby Rails').each do |book|
190
+ book.title.should =~ /ruby|rails/i
191
+ end
192
+ end
193
+ end
194
+ end
195
+
196
+ context 'when querying for a collection of books by a combined index' do
197
+ it 'has results that contain the arguments' do
198
+ BookWorm.find_all(:combined, 'Ruby Rails').each do |book|
199
+ book.title.should =~ /ruby|rails/i
200
+ end
201
+ end
202
+
203
+ context 'when using #find_books helpers' do
204
+ it 'has results that contain the arguments' do
205
+ BookWorm.find_all_by_combined_index('Ruby Hansson Pragmatic Rails').each do |book|
206
+ title = book.title =~ /ruby|rails/i
207
+ author = book.authors =~ /hansson/i
208
+ publisher = book.publisher =~ /pragmatic/i
209
+ (title || author || publisher).should be_true
210
+ end
211
+ end
212
+ end
213
+ end
214
+
215
+ context 'when querying for a collection of books by a full index' do
216
+ it 'has results that match the full index' do
217
+ books = BookWorm.find_all(:full, 'Ruby Rails Pragmatic Programming')
218
+ books.collect{ |book| book.title }.join('_').should =~ /ruby|rails/i
219
+ end
220
+
221
+ context 'when using #find_books helpers' do
222
+ it 'has results that contain the arguments for full index search' do
223
+ books = BookWorm.find_all_by_full_index('Ruby Rails Pragmatic Programming')
224
+ books.collect{ |book| book.title }.join('_').should =~ /ruby|rails/i
225
+ end
226
+ end
227
+ end
228
+ end #end find_all
229
+ end
@@ -0,0 +1 @@
1
+ require 'rspec'
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: book_worm
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Tony Schneider
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-12 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: httparty
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
46
+ description: A simple gem for interacting with ISBNDB.com
47
+ email:
48
+ - tonywok@gmail.com
49
+ executables: []
50
+
51
+ extensions: []
52
+
53
+ extra_rdoc_files: []
54
+
55
+ files:
56
+ - .gitignore
57
+ - .rspec
58
+ - .rvmrc
59
+ - Gemfile
60
+ - Gemfile.lock
61
+ - Rakefile
62
+ - book_worm.gemspec
63
+ - lib/book_worm.rb
64
+ - lib/book_worm/book.rb
65
+ - lib/book_worm/configuration.rb
66
+ - lib/book_worm/searchable.rb
67
+ - lib/book_worm/version.rb
68
+ - spec/book_spec.rb
69
+ - spec/spec_helper.rb
70
+ has_rdoc: true
71
+ homepage: ""
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options: []
76
+
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project: book_worm
98
+ rubygems_version: 1.3.7
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: A simple gem for interacting with ISBNDB.com
102
+ test_files:
103
+ - spec/book_spec.rb
104
+ - spec/spec_helper.rb