google-book 0.2.2 → 0.3.0
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/Gemfile +6 -0
- data/README.md +13 -0
- data/google.gemspec +0 -4
- data/lib/google/book.rb +48 -0
- data/lib/google/book/cover.rb +6 -6
- data/lib/google/book/entry.rb +71 -0
- data/lib/google/book/response.rb +13 -15
- data/lib/google/book/version.rb +1 -1
- data/spec/google/book/cover_spec.rb +1 -1
- data/spec/google/book/entry_spec.rb +182 -0
- data/spec/google/book/response_spec.rb +9 -9
- data/spec/google/book_spec.rb +28 -0
- data/spec/spec_helper.rb +1 -1
- metadata +8 -53
- data/lib/google.rb +0 -7
- data/lib/google/book/request.rb +0 -42
- data/lib/google/book/struct.rb +0 -53
- data/spec/google/book/request_spec.rb +0 -24
- data/spec/google/book/struct_spec.rb +0 -116
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,3 +2,16 @@ Google Book
|
|
2
2
|
===========
|
3
3
|
|
4
4
|
Google Book is a Ruby wrapper to the [Google Book Search Data API](http://code.google.com/apis/books/docs/gdata/developers_guide_protocol.html).
|
5
|
+
|
6
|
+
Usage
|
7
|
+
-----
|
8
|
+
|
9
|
+
entries = Google::Book.search('deleuze')
|
10
|
+
entry = entries.first
|
11
|
+
|
12
|
+
puts entry.title
|
13
|
+
=> "Difference and repetition"
|
14
|
+
puts entry.isbn
|
15
|
+
=> "9780826477156"
|
16
|
+
puts entry.cover.thumbnail
|
17
|
+
=> "http://bks0.books.google.com/books?id=..."
|
data/google.gemspec
CHANGED
@@ -16,10 +16,6 @@ Gem::Specification.new do |s|
|
|
16
16
|
|
17
17
|
s.add_dependency('bookland', '~> 1.0.0')
|
18
18
|
s.add_dependency('httparty', '~> 0.7.4')
|
19
|
-
s.add_development_dependency('rspec', '~> 2.5.0')
|
20
|
-
s.add_development_dependency('ruby-debug19', '~> 0.11.6')
|
21
|
-
s.add_development_dependency('vcr', '~> 1.7.0')
|
22
|
-
s.add_development_dependency('webmock', '~> 1.6.2')
|
23
19
|
|
24
20
|
s.files = `git ls-files`.split("\n")
|
25
21
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
data/lib/google/book.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'httparty'
|
3
|
+
require 'google/book/response'
|
4
|
+
|
5
|
+
module Google
|
6
|
+
|
7
|
+
# A simple wrapper around the Google Book Search API.
|
8
|
+
module Book
|
9
|
+
include HTTParty
|
10
|
+
format :xml
|
11
|
+
|
12
|
+
class << self
|
13
|
+
|
14
|
+
# The search parameters.
|
15
|
+
attr_accessor :parameters
|
16
|
+
|
17
|
+
# Queries the Google Book Search Data API. Takes a query string and an
|
18
|
+
# optional options hash.
|
19
|
+
#
|
20
|
+
# The options hash respects the following members:
|
21
|
+
#
|
22
|
+
# * `:page`, which specifies the page.
|
23
|
+
#
|
24
|
+
# * `:count`, which specifies the number of results per page.
|
25
|
+
def search(query, opts = {})
|
26
|
+
self.parameters = { 'q' => query }
|
27
|
+
parameters['start-index'] = opts[:page] if opts[:page]
|
28
|
+
parameters['max-results'] = opts[:count] if opts[:count]
|
29
|
+
|
30
|
+
Response.new(get(url.to_s))
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def query
|
36
|
+
parameters.
|
37
|
+
map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.
|
38
|
+
join('&')
|
39
|
+
end
|
40
|
+
|
41
|
+
def url
|
42
|
+
URI::HTTP.build(:host => 'books.google.com',
|
43
|
+
:path => '/books/feeds/volumes',
|
44
|
+
:query => query)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/google/book/cover.rb
CHANGED
@@ -6,28 +6,28 @@ module Google
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def thumbnail
|
9
|
-
|
9
|
+
urlify(5)
|
10
10
|
end
|
11
11
|
|
12
12
|
def small
|
13
|
-
|
13
|
+
urlify(1)
|
14
14
|
end
|
15
15
|
|
16
16
|
def medium
|
17
|
-
|
17
|
+
urlify(2)
|
18
18
|
end
|
19
19
|
|
20
20
|
def large
|
21
|
-
|
21
|
+
urlify(3)
|
22
22
|
end
|
23
23
|
|
24
24
|
def extra_large
|
25
|
-
|
25
|
+
urlify(6)
|
26
26
|
end
|
27
27
|
|
28
28
|
private
|
29
29
|
|
30
|
-
def
|
30
|
+
def urlify(zoom)
|
31
31
|
@url.
|
32
32
|
gsub('zoom=5', "zoom=#{zoom}").
|
33
33
|
gsub('&edge=curl', '')
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'bookland'
|
2
|
+
require 'google/book/cover'
|
3
|
+
|
4
|
+
module Google
|
5
|
+
module Book
|
6
|
+
class Entry
|
7
|
+
def initialize(hash)
|
8
|
+
@hash = hash
|
9
|
+
end
|
10
|
+
|
11
|
+
def cover
|
12
|
+
Cover.new(@hash['link'][0]['href'])
|
13
|
+
end
|
14
|
+
|
15
|
+
def creators
|
16
|
+
[@hash['dc:creator']].flatten.join(', ')
|
17
|
+
end
|
18
|
+
|
19
|
+
def date
|
20
|
+
@hash['dc:date']
|
21
|
+
end
|
22
|
+
|
23
|
+
def description
|
24
|
+
@hash['dc:description']
|
25
|
+
end
|
26
|
+
|
27
|
+
def format
|
28
|
+
[@hash['dc:format']].flatten.reject do |format|
|
29
|
+
format == 'book'
|
30
|
+
end.join(', ')
|
31
|
+
end
|
32
|
+
|
33
|
+
def info_url
|
34
|
+
@hash['link'][1]['href']
|
35
|
+
end
|
36
|
+
|
37
|
+
def preview_url
|
38
|
+
@hash['link'][2]['href']
|
39
|
+
end
|
40
|
+
|
41
|
+
def publisher
|
42
|
+
@hash['dc:publisher'].
|
43
|
+
gsub(/[ ,]+Inc.?$/, '').
|
44
|
+
gsub(/[ ,]+Llc.?$/i, '').
|
45
|
+
gsub(/[ ,]+Ltd.?$/, '').
|
46
|
+
gsub(/Intl( |$)/) { "International#{$1}" }.
|
47
|
+
gsub(/Pr( |$)/) { "Press#{$1}" }.
|
48
|
+
gsub(/ Pub$/, ' Publishers').
|
49
|
+
gsub(/ Pubns$/, ' Publications').
|
50
|
+
gsub('Pub Group', 'Publishing Group').
|
51
|
+
gsub(/Univ( |$)/) { "University#{$1}" }
|
52
|
+
end
|
53
|
+
|
54
|
+
def subjects
|
55
|
+
@hash['dc:subject']
|
56
|
+
end
|
57
|
+
|
58
|
+
def title
|
59
|
+
[@hash['dc:title']].flatten.join(': ')
|
60
|
+
end
|
61
|
+
|
62
|
+
def isbn
|
63
|
+
@hash['dc:identifier'].detect do |identifier|
|
64
|
+
identifier.match(/ISBN:([0-9X]{10,13})/)
|
65
|
+
end
|
66
|
+
|
67
|
+
ISBN.to_13($1) rescue nil
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/google/book/response.rb
CHANGED
@@ -1,26 +1,24 @@
|
|
1
|
+
require 'google/book/entry'
|
2
|
+
|
1
3
|
module Google
|
2
4
|
module Book
|
3
5
|
class Response
|
6
|
+
include Enumerable
|
7
|
+
|
4
8
|
def initialize(hash)
|
5
9
|
@feed = hash['feed']
|
6
10
|
end
|
7
11
|
|
8
|
-
def
|
9
|
-
|
12
|
+
def each(&block)
|
13
|
+
members =
|
14
|
+
if total_results == 0
|
15
|
+
[]
|
16
|
+
else
|
17
|
+
[@feed['entry']].flatten
|
18
|
+
end
|
10
19
|
|
11
|
-
|
12
|
-
|
13
|
-
Cover.new(hash['link'][0]['href']),
|
14
|
-
hash['link'][1]['href'],
|
15
|
-
hash['link'][2]['href'],
|
16
|
-
[hash['dc:creator']].flatten,
|
17
|
-
hash['dc:date'],
|
18
|
-
hash['dc:description'],
|
19
|
-
[hash['dc:format']].flatten,
|
20
|
-
hash['dc:identifier'],
|
21
|
-
hash['dc:publisher'],
|
22
|
-
hash['dc:subject'],
|
23
|
-
[hash['dc:title']].flatten)
|
20
|
+
members.each do |member|
|
21
|
+
block.call(Entry.new(member))
|
24
22
|
end
|
25
23
|
end
|
26
24
|
|
data/lib/google/book/version.rb
CHANGED
@@ -5,7 +5,7 @@ module Google
|
|
5
5
|
describe Cover do
|
6
6
|
use_vcr_cassette 'google'
|
7
7
|
|
8
|
-
subject {
|
8
|
+
subject { Book.search('deleuze').first.cover }
|
9
9
|
|
10
10
|
%w{thumbnail small medium large extra_large}.each do |attribute|
|
11
11
|
its(attribute) { should_not be_nil }
|
@@ -0,0 +1,182 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Google
|
4
|
+
module Book
|
5
|
+
describe Entry do
|
6
|
+
use_vcr_cassette 'google'
|
7
|
+
|
8
|
+
let(:entry) do
|
9
|
+
Entry.new({})
|
10
|
+
end
|
11
|
+
|
12
|
+
subject { Book.search('deleuze').first }
|
13
|
+
|
14
|
+
%w{cover creators date description format info_url preview_url publisher
|
15
|
+
subjects title isbn}.each do |attribute|
|
16
|
+
it { should respond_to attribute }
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#creators" do
|
20
|
+
it "should concatenate creators" do
|
21
|
+
entry.instance_variable_set(:@hash, {
|
22
|
+
'dc:creator' => ['Deleuze', 'Guattari']
|
23
|
+
})
|
24
|
+
|
25
|
+
entry.creators.should eql 'Deleuze, Guattari'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#format" do
|
30
|
+
it "should concatenate formats" do
|
31
|
+
entry.instance_variable_set(:@hash, {
|
32
|
+
'dc:format' => ['foo', 'bar']
|
33
|
+
})
|
34
|
+
|
35
|
+
entry.format.should eql 'foo, bar'
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should drop the word book" do
|
39
|
+
entry.instance_variable_set(:@hash, {
|
40
|
+
'dc:format' => ['foo', 'book']
|
41
|
+
})
|
42
|
+
|
43
|
+
entry.format.should eql 'foo'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#publisher" do
|
48
|
+
it "should drop Inc at the end of the name" do
|
49
|
+
entry.instance_variable_set(:@hash, {
|
50
|
+
'dc:publisher' => 'Publisher Inc'
|
51
|
+
})
|
52
|
+
|
53
|
+
entry.publisher.should eql 'Publisher'
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should drop Inc. at the end of the name" do
|
57
|
+
entry.instance_variable_set(:@hash, {
|
58
|
+
'dc:publisher' => 'Publisher Inc.'
|
59
|
+
})
|
60
|
+
|
61
|
+
entry.publisher.should eql 'Publisher'
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should drop Llc at the end of the name" do
|
65
|
+
entry.instance_variable_set(:@hash, {
|
66
|
+
'dc:publisher' => 'Publisher Llc'
|
67
|
+
})
|
68
|
+
|
69
|
+
entry.publisher.should eql 'Publisher'
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should drop Llc. at the end of the name" do
|
73
|
+
entry.instance_variable_set(:@hash, {
|
74
|
+
'dc:publisher' => 'Publisher Llc.'
|
75
|
+
})
|
76
|
+
|
77
|
+
entry.publisher.should eql 'Publisher'
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should drop LLC at the end of the name" do
|
81
|
+
entry.instance_variable_set(:@hash, {
|
82
|
+
'dc:publisher' => 'Publisher LLC'
|
83
|
+
})
|
84
|
+
|
85
|
+
entry.publisher.should eql 'Publisher'
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should drop Ltd at the end of the name" do
|
89
|
+
entry.instance_variable_set(:@hash, {
|
90
|
+
'dc:publisher' => 'Publisher Ltd'
|
91
|
+
})
|
92
|
+
|
93
|
+
entry.publisher.should eql 'Publisher'
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should format Intl" do
|
97
|
+
entry.instance_variable_set(:@hash, {
|
98
|
+
'dc:publisher' => 'Publisher Intl'
|
99
|
+
})
|
100
|
+
|
101
|
+
entry.publisher.should eql 'Publisher International'
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should format Pr" do
|
105
|
+
entry.instance_variable_set(:@hash, {
|
106
|
+
'dc:publisher' => 'Publisher Pr'
|
107
|
+
})
|
108
|
+
|
109
|
+
entry.publisher.should eql 'Publisher Press'
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should format Pub at the end of the name" do
|
113
|
+
entry.instance_variable_set(:@hash, {
|
114
|
+
'dc:publisher' => 'Publisher Pub'
|
115
|
+
})
|
116
|
+
|
117
|
+
entry.publisher.should eql 'Publisher Publishers'
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should format Pubns at the end of the name" do
|
121
|
+
entry.instance_variable_set(:@hash, {
|
122
|
+
'dc:publisher' => 'Publisher Pubns'
|
123
|
+
})
|
124
|
+
|
125
|
+
entry.publisher.should eql 'Publisher Publications'
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should format Pub Group" do
|
129
|
+
entry.instance_variable_set(:@hash, {
|
130
|
+
'dc:publisher' => 'Publisher Pub Group'
|
131
|
+
})
|
132
|
+
|
133
|
+
entry.publisher.should eql 'Publisher Publishing Group'
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should format Univ in the middle of the name" do
|
137
|
+
entry.instance_variable_set(:@hash, {
|
138
|
+
'dc:publisher' => 'Publisher Univ Press'
|
139
|
+
})
|
140
|
+
|
141
|
+
entry.publisher.should eql 'Publisher University Press'
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should format Univ at the end of the name" do
|
145
|
+
entry.instance_variable_set(:@hash, {
|
146
|
+
'dc:publisher' => 'Publisher Univ'
|
147
|
+
})
|
148
|
+
|
149
|
+
entry.publisher.should eql 'Publisher University'
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
describe "#title" do
|
154
|
+
it "should concatenate titles" do
|
155
|
+
entry.instance_variable_set(:@hash, {
|
156
|
+
'dc:title' => ['Thousand Plateaus', 'Capitalism and Schizophrenia']
|
157
|
+
})
|
158
|
+
|
159
|
+
entry.title.should eql 'Thousand Plateaus: Capitalism and Schizophrenia'
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
describe "#isbn" do
|
164
|
+
it "should return the ISBN" do
|
165
|
+
entry.instance_variable_set(:@hash, {
|
166
|
+
'dc:identifier' => ['8cp-Z_G42g4C','ISBN:0192802380']
|
167
|
+
})
|
168
|
+
|
169
|
+
entry.isbn.should eql '9780192802385'
|
170
|
+
end
|
171
|
+
|
172
|
+
it "should return nil if no ISBN is available" do
|
173
|
+
entry.instance_variable_set(:@hash, {
|
174
|
+
'dc:identifier' => []
|
175
|
+
})
|
176
|
+
|
177
|
+
entry.isbn.should eql nil
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
@@ -6,23 +6,23 @@ module Google
|
|
6
6
|
use_vcr_cassette 'google'
|
7
7
|
|
8
8
|
it "should set total results" do
|
9
|
-
response =
|
9
|
+
response = Book.search('deleuze')
|
10
10
|
response.total_results.should > 0
|
11
11
|
end
|
12
12
|
|
13
|
-
it "should return
|
14
|
-
response =
|
15
|
-
|
16
|
-
|
17
|
-
books.first.should be_a Struct
|
13
|
+
it "should return entries" do
|
14
|
+
response = Book.search('deleuze')
|
15
|
+
response.first.should be_an Entry
|
18
16
|
end
|
19
17
|
|
20
|
-
|
21
|
-
|
18
|
+
context "when there is a single match" do
|
19
|
+
it "should return entries" do
|
20
|
+
Book.search('9780826490780').first.should be_an Entry
|
21
|
+
end
|
22
22
|
end
|
23
23
|
|
24
24
|
it "should handle an empty query" do
|
25
|
-
|
25
|
+
Book.search('').to_a.should be_empty
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Google
|
4
|
+
describe Book do
|
5
|
+
use_vcr_cassette 'google'
|
6
|
+
|
7
|
+
describe "#search" do
|
8
|
+
it "should escape parameters" do
|
9
|
+
Book.search('foo bar')
|
10
|
+
Book.send(:query).should include 'q=foo+bar'
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should set the page" do
|
14
|
+
Book.search('foo bar', :page => 2)
|
15
|
+
Book.send(:query).should include 'start-index=2'
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should set the number of results per page" do
|
19
|
+
Book.search('foo bar', :count => 20)
|
20
|
+
Book.send(:query).should include 'max-results=20'
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should return a response" do
|
24
|
+
Book.search('foo bar').should be_a Book::Response
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: google-book
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.3.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Paper Cavalier
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-03-
|
13
|
+
date: 2011-03-28 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -35,50 +35,6 @@ dependencies:
|
|
35
35
|
version: 0.7.4
|
36
36
|
type: :runtime
|
37
37
|
version_requirements: *id002
|
38
|
-
- !ruby/object:Gem::Dependency
|
39
|
-
name: rspec
|
40
|
-
prerelease: false
|
41
|
-
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
-
none: false
|
43
|
-
requirements:
|
44
|
-
- - ~>
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
version: 2.5.0
|
47
|
-
type: :development
|
48
|
-
version_requirements: *id003
|
49
|
-
- !ruby/object:Gem::Dependency
|
50
|
-
name: ruby-debug19
|
51
|
-
prerelease: false
|
52
|
-
requirement: &id004 !ruby/object:Gem::Requirement
|
53
|
-
none: false
|
54
|
-
requirements:
|
55
|
-
- - ~>
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
version: 0.11.6
|
58
|
-
type: :development
|
59
|
-
version_requirements: *id004
|
60
|
-
- !ruby/object:Gem::Dependency
|
61
|
-
name: vcr
|
62
|
-
prerelease: false
|
63
|
-
requirement: &id005 !ruby/object:Gem::Requirement
|
64
|
-
none: false
|
65
|
-
requirements:
|
66
|
-
- - ~>
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: 1.7.0
|
69
|
-
type: :development
|
70
|
-
version_requirements: *id005
|
71
|
-
- !ruby/object:Gem::Dependency
|
72
|
-
name: webmock
|
73
|
-
prerelease: false
|
74
|
-
requirement: &id006 !ruby/object:Gem::Requirement
|
75
|
-
none: false
|
76
|
-
requirements:
|
77
|
-
- - ~>
|
78
|
-
- !ruby/object:Gem::Version
|
79
|
-
version: 1.6.2
|
80
|
-
type: :development
|
81
|
-
version_requirements: *id006
|
82
38
|
description: Google Book is a Ruby wrapper to the Google Book Search Data API.
|
83
39
|
email:
|
84
40
|
- code@papercavalier.com
|
@@ -96,16 +52,15 @@ files:
|
|
96
52
|
- README.md
|
97
53
|
- Rakefile
|
98
54
|
- google.gemspec
|
99
|
-
- lib/google.rb
|
55
|
+
- lib/google/book.rb
|
100
56
|
- lib/google/book/cover.rb
|
101
|
-
- lib/google/book/
|
57
|
+
- lib/google/book/entry.rb
|
102
58
|
- lib/google/book/response.rb
|
103
|
-
- lib/google/book/struct.rb
|
104
59
|
- lib/google/book/version.rb
|
105
60
|
- spec/google/book/cover_spec.rb
|
106
|
-
- spec/google/book/
|
61
|
+
- spec/google/book/entry_spec.rb
|
107
62
|
- spec/google/book/response_spec.rb
|
108
|
-
- spec/google/
|
63
|
+
- spec/google/book_spec.rb
|
109
64
|
- spec/spec_helper.rb
|
110
65
|
- spec/support/vcr.rb
|
111
66
|
has_rdoc: true
|
@@ -138,8 +93,8 @@ specification_version: 3
|
|
138
93
|
summary: A Ruby wrapper to the Google Book Search Data API
|
139
94
|
test_files:
|
140
95
|
- spec/google/book/cover_spec.rb
|
141
|
-
- spec/google/book/
|
96
|
+
- spec/google/book/entry_spec.rb
|
142
97
|
- spec/google/book/response_spec.rb
|
143
|
-
- spec/google/
|
98
|
+
- spec/google/book_spec.rb
|
144
99
|
- spec/spec_helper.rb
|
145
100
|
- spec/support/vcr.rb
|
data/lib/google.rb
DELETED
data/lib/google/book/request.rb
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
require 'cgi'
|
2
|
-
require 'httparty'
|
3
|
-
|
4
|
-
module Google
|
5
|
-
module Book
|
6
|
-
module Request
|
7
|
-
include HTTParty
|
8
|
-
format :xml
|
9
|
-
|
10
|
-
class << self
|
11
|
-
attr_accessor :parameters
|
12
|
-
|
13
|
-
# Queries the Google Book Search Data API.
|
14
|
-
#
|
15
|
-
# Optionally, specify a page and count to paginate through the
|
16
|
-
# result set.
|
17
|
-
#
|
18
|
-
# GoogleBook.find('deleuze', :page => 2, :count => 20)
|
19
|
-
#
|
20
|
-
def find(query, opts = {})
|
21
|
-
self.parameters = { 'q' => query }
|
22
|
-
parameters['start-index'] = opts[:page] if opts[:page]
|
23
|
-
parameters['max-results'] = opts[:count] if opts[:count]
|
24
|
-
|
25
|
-
Response.new(get(url.to_s))
|
26
|
-
end
|
27
|
-
|
28
|
-
private
|
29
|
-
|
30
|
-
def query
|
31
|
-
parameters.map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
|
32
|
-
end
|
33
|
-
|
34
|
-
def url
|
35
|
-
URI::HTTP.build(:host => 'books.google.com',
|
36
|
-
:path => '/books/feeds/volumes',
|
37
|
-
:query => query)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
data/lib/google/book/struct.rb
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
module Google
|
2
|
-
module Book
|
3
|
-
class Struct< Struct.new :cover,
|
4
|
-
:info,
|
5
|
-
:preview,
|
6
|
-
:creators,
|
7
|
-
:date,
|
8
|
-
:description,
|
9
|
-
:formats,
|
10
|
-
:identifiers,
|
11
|
-
:publisher,
|
12
|
-
:subjects,
|
13
|
-
:titles
|
14
|
-
|
15
|
-
include Bookland
|
16
|
-
|
17
|
-
def formatted_creators
|
18
|
-
creators.join(', ')
|
19
|
-
end
|
20
|
-
|
21
|
-
def formatted_formats
|
22
|
-
formats.reject do |format|
|
23
|
-
format == 'book'
|
24
|
-
end.join(', ')
|
25
|
-
end
|
26
|
-
|
27
|
-
def formatted_publisher
|
28
|
-
publisher.
|
29
|
-
gsub(/[ ,]+Inc.?$/, '').
|
30
|
-
gsub(/[ ,]+Llc.?$/i, '').
|
31
|
-
gsub(/[ ,]+Ltd.?$/, '').
|
32
|
-
gsub(/Intl( |$)/) { "International#{$1}" }.
|
33
|
-
gsub(/Pr( |$)/) { "Press#{$1}" }.
|
34
|
-
gsub(/ Pub$/, ' Publishers').
|
35
|
-
gsub(/ Pubns$/, ' Publications').
|
36
|
-
gsub('Pub Group', 'Publishing Group').
|
37
|
-
gsub(/Univ( |$)/) { "University#{$1}" }
|
38
|
-
end
|
39
|
-
|
40
|
-
def formatted_title
|
41
|
-
titles.join(': ')
|
42
|
-
end
|
43
|
-
|
44
|
-
def isbn
|
45
|
-
identifiers.detect do |identifier|
|
46
|
-
identifier.match(/ISBN:([0-9X]{10,13})/)
|
47
|
-
end
|
48
|
-
|
49
|
-
ISBN.to_13($1) rescue nil
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Google
|
4
|
-
module Book
|
5
|
-
describe Request do
|
6
|
-
use_vcr_cassette 'google'
|
7
|
-
|
8
|
-
it "should escape parameters" do
|
9
|
-
Request.find('foo bar')
|
10
|
-
Request.send(:query).should include 'foo+bar'
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should set page" do
|
14
|
-
Request.find('foo bar', :page => 2)
|
15
|
-
Request.send(:query).should include 'start-index=2'
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should set number of results per page" do
|
19
|
-
Request.find('foo bar', :count => 20)
|
20
|
-
Request.send(:query).should include 'max-results=20'
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,116 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Google
|
4
|
-
module Book
|
5
|
-
describe Struct do
|
6
|
-
use_vcr_cassette 'google'
|
7
|
-
|
8
|
-
let(:book) do
|
9
|
-
Struct.new
|
10
|
-
end
|
11
|
-
|
12
|
-
subject { Request.find('deleuze').to_books.first }
|
13
|
-
|
14
|
-
%w{cover info preview creators date description formats
|
15
|
-
identifiers isbn publisher subjects titles}.each do |attribute|
|
16
|
-
its(attribute) { should_not be_nil }
|
17
|
-
end
|
18
|
-
|
19
|
-
describe "#formatted_creators" do
|
20
|
-
it "should concatenate creators" do
|
21
|
-
book.creators = ['Deleuze', 'Guattari']
|
22
|
-
book.formatted_creators.should eql 'Deleuze, Guattari'
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
describe "#formatted_formats" do
|
27
|
-
it "should concatenate formats" do
|
28
|
-
book.formats = ['foo', 'bar']
|
29
|
-
book.formatted_formats.should eql 'foo, bar'
|
30
|
-
end
|
31
|
-
|
32
|
-
it "should drop the word book" do
|
33
|
-
book.formats = ['foo', 'book']
|
34
|
-
book.formatted_formats.should eql 'foo'
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
describe "#formatted_publisher" do
|
39
|
-
it "should drop Inc at the end of the name" do
|
40
|
-
book.publisher = 'Publisher Inc'
|
41
|
-
book.formatted_publisher.should eql 'Publisher'
|
42
|
-
|
43
|
-
book.publisher = 'Random House, Inc.'
|
44
|
-
book.formatted_publisher.should eql 'Random House'
|
45
|
-
end
|
46
|
-
|
47
|
-
it "should drop Llc at the end of the name" do
|
48
|
-
book.publisher = 'Publisher Llc'
|
49
|
-
book.formatted_publisher.should eql 'Publisher'
|
50
|
-
|
51
|
-
book.publisher = 'Publisher LLC'
|
52
|
-
book.formatted_publisher.should eql 'Publisher'
|
53
|
-
end
|
54
|
-
|
55
|
-
it "should drop Ltd at the end of the name" do
|
56
|
-
book.publisher = 'Publisher Ltd'
|
57
|
-
book.formatted_publisher.should eql 'Publisher'
|
58
|
-
end
|
59
|
-
|
60
|
-
it "should format Intl" do
|
61
|
-
book.publisher = 'Continuum Intl'
|
62
|
-
book.formatted_publisher.should eql 'Continuum International'
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should format Pr" do
|
66
|
-
book.publisher = 'Polity Pr'
|
67
|
-
book.formatted_publisher.should eql 'Polity Press'
|
68
|
-
end
|
69
|
-
|
70
|
-
it "should format Pub at the end of the name" do
|
71
|
-
book.publisher = 'Rowman & Littlefield Pub'
|
72
|
-
book.formatted_publisher.should eql 'Rowman & Littlefield Publishers'
|
73
|
-
end
|
74
|
-
|
75
|
-
it "should format Pubns at the end of the name" do
|
76
|
-
book.publisher = 'Publisher Pubns'
|
77
|
-
book.formatted_publisher.should eql 'Publisher Publications'
|
78
|
-
end
|
79
|
-
|
80
|
-
it "should format Pub Group" do
|
81
|
-
book.publisher = 'Continuum International Pub Group'
|
82
|
-
book.formatted_publisher.should eql 'Continuum International Publishing Group'
|
83
|
-
end
|
84
|
-
|
85
|
-
it "should format Univ in the middle of the name" do
|
86
|
-
book.publisher = 'Columbia Univ Press'
|
87
|
-
book.formatted_publisher.should eql 'Columbia University Press'
|
88
|
-
end
|
89
|
-
|
90
|
-
it "should format Univ at the end of the name" do
|
91
|
-
book.publisher = 'Columbia Univ'
|
92
|
-
book.formatted_publisher.should eql 'Columbia University'
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
describe "#formatted_title" do
|
97
|
-
it "should concatenate titles" do
|
98
|
-
book.titles = ['Thousand Plateaus', 'Capitalism and Schizophrenia']
|
99
|
-
book.formatted_title.should eql 'Thousand Plateaus: Capitalism and Schizophrenia'
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
describe "#isbn" do
|
104
|
-
it "should return the ISBN" do
|
105
|
-
book.identifiers = ['8cp-Z_G42g4C','ISBN:0192802380']
|
106
|
-
book.isbn.should eql '9780192802385'
|
107
|
-
end
|
108
|
-
|
109
|
-
it "should return nil if no ISBN is available" do
|
110
|
-
book.identifiers = []
|
111
|
-
book.isbn.should eql nil
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|