googlebooks 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/LICENSE +15 -0
- data/README.md +161 -0
- data/googlebooks.gemspec +3 -2
- data/lib/version.rb +1 -1
- data/spec/googlebooks/book/item_spec.rb +51 -0
- data/spec/googlebooks/book/response_spec.rb +20 -0
- data/spec/googlebooks/googlebooks_spec.rb +51 -0
- data/spec/spec_helper.rb +5 -0
- metadata +29 -11
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
This file is part of GoogleBooks.
|
2
|
+
|
3
|
+
GoogleBooks is free software: you can redistribute it and/or modify
|
4
|
+
it under the terms of the GNU General Public License as published by
|
5
|
+
the Free Software Foundation, either version 3 of the License, or
|
6
|
+
(at your option) any later version.
|
7
|
+
|
8
|
+
GoogleBooks is distributed in the hope that it will be useful,
|
9
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11
|
+
GNU General Public License for more details.
|
12
|
+
|
13
|
+
You should have received a copy of the GNU General Public License
|
14
|
+
along with GoogleBooks. If not, see <http://www.gnu.org/licenses/>.
|
15
|
+
|
data/README.md
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
GoogleBooks
|
2
|
+
===========
|
3
|
+
|
4
|
+
GoogleBooks is a lightweight Ruby wrapper that queries the Google API to search for publications in the Google Books repository.
|
5
|
+
|
6
|
+
It is inspired by the [google-book](https://github.com/papercavalier/google-book) gem which relies on the deprecated [Google GData Books API](http://code.google.com/apis/books/docs/gdata/developers_guide_protocol.html), but is updated to hook into the current Google API and expanded to provide additional methods and search options.
|
7
|
+
|
8
|
+
Basic Usage
|
9
|
+
-----------
|
10
|
+
|
11
|
+
Using GoogleBooks is simple. There's just one class, `GoogleBooks`, and one method, `search`. Queries return an enumerable collection of results, each with their own set of attributes that can be individually configured.
|
12
|
+
|
13
|
+
require 'googlebooks' # unless you're using Bundler
|
14
|
+
|
15
|
+
books = GoogleBooks.search('The Great Gatsby')
|
16
|
+
first_book = book.first
|
17
|
+
|
18
|
+
first_book.author #=> 'F. Scott Fitzgerald'
|
19
|
+
first_book.isbn #=> '9781443411080'
|
20
|
+
first_book.image_link(:zoom => 6) #=> 'http://bks2.books.google.com/books?id=...'
|
21
|
+
|
22
|
+
|
23
|
+
Options Hash
|
24
|
+
-----------
|
25
|
+
|
26
|
+
The `search` method allows for an options hash of up to four parameters. These parameters are `page`, `count`, `order_by`, and `api_key`. Necessity of a Google API key is application dependent; further information is available in [Google's API documentation](http://code.google.com/apis/books/docs/v1/using.html).
|
27
|
+
|
28
|
+
require 'googlebooks'
|
29
|
+
|
30
|
+
GoogleBooks.search('The Great Gatsby', {:count => 10})
|
31
|
+
#=> returns first ten results (default = 5)
|
32
|
+
|
33
|
+
GoogleBooks.search('The Great Gatsby', {:page => 2})
|
34
|
+
#=> returns second page of results (default = 1)
|
35
|
+
|
36
|
+
GoogleBooks.search('The Great Gatsby', {:count => 3, :page => 4, :api_key => 'THIS_IS_YOUR_API_KEY'})
|
37
|
+
#=> returns three results, beginning with the 10th overall result
|
38
|
+
|
39
|
+
By default, results are returned in order of relevance. However, results also be ordered from most recently to least recently published by passing `newest` to the `order_by` key. Passing anything other than `newest` will return results by relevance. Google [does not currently support](https://groups.google.com/a/googleproductforums.com/forum/#!searchin/books/publication$20date/books/wKuq9TLGYsc/SRIk-YiiPHQJ) querying any values other than `relevance` and `newest`.
|
40
|
+
|
41
|
+
GoogleBooks.search('The Great Gatsby', {:order_by => 'newest'})
|
42
|
+
#=> returns results in order of most recently to least recently published
|
43
|
+
|
44
|
+
GoogleBooks.search('The Great Gatsby')
|
45
|
+
GoogleBooks.search('The Great Gatsby', {:order_by => `ANYTHING ELSE`})
|
46
|
+
#=> both return results in order of relevance
|
47
|
+
|
48
|
+
Special Keywords
|
49
|
+
----------------
|
50
|
+
|
51
|
+
There are special keywords you can specify in the search terms to search in particular fields, such as:
|
52
|
+
|
53
|
+
* `intitle` *Returns results where the text following this keyword is found in the title.*
|
54
|
+
* `inauthor` *Returns results where the text following this keyword is found in the author.*
|
55
|
+
* `inpublisher` *Returns results where the text following this keyword is found in the publisher.*
|
56
|
+
* `subject` *Returns results where the text following this keyword is listed in the category list of the volume.*
|
57
|
+
* `isbn` *Returns results where the text following this keyword is the ISBN number.*
|
58
|
+
|
59
|
+
Examples:
|
60
|
+
|
61
|
+
require 'googlebooks'
|
62
|
+
|
63
|
+
books = GoogleBooks.search('isbn:9781443411080') # yields a collection of one result
|
64
|
+
book = books.first # the one result
|
65
|
+
|
66
|
+
book.title #=> 'The Great Gatsby'
|
67
|
+
|
68
|
+
Attributes
|
69
|
+
----------
|
70
|
+
|
71
|
+
By default, GoogleBooks can query the following attributes (note that not all attributes are available to all books):
|
72
|
+
|
73
|
+
* `title`
|
74
|
+
* `authors` *Returns all authors as a comma delimited string*
|
75
|
+
* `publisher`
|
76
|
+
* `published_date`
|
77
|
+
* `description`
|
78
|
+
* `isbn` *Attempts to return 13-digit first, then 10-digit, then nil*
|
79
|
+
* `isbn_10` *Returns 10-digit form only*
|
80
|
+
* `isbn_13` *Returns 13-digit form only*
|
81
|
+
* `page_count`
|
82
|
+
* `print_type`
|
83
|
+
* `categories` *Returns all categories as a comma delimited string*
|
84
|
+
* `average_rating`
|
85
|
+
* `ratings_count`
|
86
|
+
* `language`
|
87
|
+
* `preview_link`
|
88
|
+
* `info_link`
|
89
|
+
* `image_link` *See details below*
|
90
|
+
|
91
|
+
Sale and retail information is available on some volumes as of Google Books API v1. GoogleBooks returns a hash of these attributes (where available) under the `sale_info` attribute.
|
92
|
+
|
93
|
+
book = GoogleBooks.search(9780345508553).first # Do Androids Dream of Electric Sheep?
|
94
|
+
|
95
|
+
book['country'] #=> 'US'
|
96
|
+
book['saleability'] #=> 'FOR_SALE'
|
97
|
+
book['isEbook'] #=> true
|
98
|
+
book['listPrice']['amount'] #=> 9.99
|
99
|
+
book['listPrice']['currencyCode'] #=> 'USD'
|
100
|
+
book['retailPrice']['amount'] #=> 9.99
|
101
|
+
book['retailPrice']['currencyCode'] #=> 'USD'
|
102
|
+
book['buyLink'] #=> 'http://books.google.com/books?id=jUX8N9kiCiQC&dq=9780345508553&buy=&source=gbs_api'
|
103
|
+
|
104
|
+
Note that the `sale_info` attribute is only available for some volumes and will sometimes return `nil` for nested attributes. Use with caution as GoogleBooks does not provide built-in error handling for instances of `nil`.
|
105
|
+
|
106
|
+
Image Links
|
107
|
+
-----------
|
108
|
+
|
109
|
+
When possible, GoogleBooks will return links to an image of a book's cover. The size and appearance of the cover can be configured by passing an optional hash of arguments to the attribute.
|
110
|
+
|
111
|
+
The size of this image is configurable by passing an numerical argument between 1-6 to the image_link attribute, which is passed to the zoom attribute in the URL query.
|
112
|
+
|
113
|
+
* `:zoom` *Pass numeric value between 1-6 to the zoom attribute* (default = 1)
|
114
|
+
|
115
|
+
GoogleBooks will, by default, return the image_link for an aesthetically unmodified cover. However, if you'd like Google to return an image with a "curled" corner (when available), you can pass true to this key.
|
116
|
+
|
117
|
+
* `:curl` *Pass true to return image_link of cover with curled corner* (default = false)
|
118
|
+
|
119
|
+
Examples:
|
120
|
+
|
121
|
+
book = GoogleBooks.search("The Great Gatsby").first
|
122
|
+
|
123
|
+
# return image_link with default options
|
124
|
+
book.image_link # zoom=1, edge=none
|
125
|
+
|
126
|
+
# return image_link of varying sizes
|
127
|
+
book.image_link # small
|
128
|
+
book.image_link(:zoom => 5) # thumbnail
|
129
|
+
book.image_link(:zoom => 2) # medium
|
130
|
+
book.image_link(:zoom => 3) # large
|
131
|
+
book.image_link(:zoom => 4) # extra large
|
132
|
+
|
133
|
+
# return image_link with curled corners
|
134
|
+
book.image_link(:curl => true)
|
135
|
+
|
136
|
+
# return thumbnail image_link with curled corners
|
137
|
+
book.image_link(:zoom => 5, :curl => true)
|
138
|
+
|
139
|
+
Support for distributed production servers
|
140
|
+
------------------------------------------
|
141
|
+
|
142
|
+
The current Google API for Books generally requires that the requesting server's IP address be geolocatable. [This can cause responses to be restricted](http://www.google.com/support/forum/p/booksearch-apis/thread?tid=2034bed9a98c15cb&hl=en) when requested from distributed/clustered servers such as Heroku.
|
143
|
+
|
144
|
+
GoogleBooks can resolve these issues by adding an X-Forwarded-For HTTP header to each request that contains the IP address of the user. In most cases, you can pass the user's IP address as the third parameter in your search request:
|
145
|
+
|
146
|
+
# Request made from Heroku server without passing user's IP address to HTTP header
|
147
|
+
GoogleBooks.search("Search Query", {options_hash}) #=> returns error hash
|
148
|
+
|
149
|
+
# Request made from Heroku server with user's IP address
|
150
|
+
user_ip = request.remote_ip # assuming user is accessing from a valid IP address
|
151
|
+
GoogleBooks.search("Search Query", {options_hash}, user_ip) #=> returns valid hash of results
|
152
|
+
|
153
|
+
Doing this will, for the most part, enable you to query the Google API from apps that reside across distributed nodes.
|
154
|
+
|
155
|
+
Etcetera
|
156
|
+
--------
|
157
|
+
|
158
|
+
GoogleBooks is licensed under the GNU GPL. Modify and distribute freely.
|
159
|
+
|
160
|
+
Please feel free to contact the developer with any questions or suggestions. Forking and merge/pull requests are encouraged for those who would like to take part in improving this gem.
|
161
|
+
|
data/googlebooks.gemspec
CHANGED
@@ -16,9 +16,10 @@ Gem::Specification.new do |s|
|
|
16
16
|
|
17
17
|
s.add_dependency('httparty')
|
18
18
|
s.add_development_dependency('rspec')
|
19
|
+
s.add_development_dependency('webmock')
|
19
20
|
|
20
|
-
|
21
|
-
s.files = Dir["{lib}/**/*", "googlebooks.gemspec", "Gemfile", "Rakefile"]
|
21
|
+
s.files = `git ls-files`.split("\n")
|
22
|
+
#s.files = Dir["{lib}/**/*", "googlebooks.gemspec", "Gemfile", "Rakefile"]
|
22
23
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
23
24
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
24
25
|
s.require_paths = ["lib"]
|
data/lib/version.rb
CHANGED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module GoogleBooks
|
4
|
+
class Item
|
5
|
+
describe Item do
|
6
|
+
|
7
|
+
example = GoogleBooks.search('isbn:9780062132345').first
|
8
|
+
|
9
|
+
it "should append a subtitle to the title if it exists" do
|
10
|
+
example.title.should eq "Freakonomics: A Rogue Economist Explores the Hidden Side of Everything"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should return a string authors delineated by a comma" do
|
14
|
+
example.authors.should eq "Steven D. Levitt, Stephen J. Dubner"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should have an isbn that is 13 digits" do
|
18
|
+
example.isbn.should_not eq nil
|
19
|
+
example.isbn.should eq '9780062132345'
|
20
|
+
example.isbn.to_s.length.should eq 13
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should convert to a 10 digit isbn" do
|
24
|
+
example.isbn_10.should eq '0062132342'
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should make a 13 digit isbn duplicated from the default isbn" do
|
28
|
+
example.isbn_13.should eq '9780062132345'
|
29
|
+
example.isbn_13.should eq example.isbn
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "image_link" do
|
33
|
+
it "should have all zoom varieties and show 1 as a default" do
|
34
|
+
example.image_link.should include "zoom=1"
|
35
|
+
example.image_link(:zoom => 2).should include "zoom=2"
|
36
|
+
example.image_link(:zoom => 3).should include "zoom=3"
|
37
|
+
example.image_link(:zoom => 4).should include "zoom=4"
|
38
|
+
example.image_link(:zoom => 5).should include "zoom=5"
|
39
|
+
example.image_link(:zoom => 6).should include "zoom=6"
|
40
|
+
example.image_link(:zoom => 7).should include "zoom=7"
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should default to 'edge=none' and curl when dictated" do
|
44
|
+
example.image_link.should include "edge=none"
|
45
|
+
example.image_link(:curl => true).should include "edge=curl"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module GoogleBooks
|
4
|
+
describe Response do
|
5
|
+
|
6
|
+
response = GoogleBooks.search('the great gatsby')
|
7
|
+
|
8
|
+
it "should set total results" do
|
9
|
+
response.total_items.should > 0
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should return entries" do
|
13
|
+
response.first.should be_an Item
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should handle an empty query" do
|
17
|
+
GoogleBooks.search('').to_a.should be_empty
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe GoogleBooks do
|
4
|
+
describe "#search" do
|
5
|
+
it "should escape spaces" do
|
6
|
+
GoogleBooks.search('the great gatsby')
|
7
|
+
GoogleBooks.send(:query).should include 'q=the+great+gatsby'
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'startIndex' do
|
11
|
+
it "should default to 0 if no page is specified" do
|
12
|
+
GoogleBooks.search('the great gatsby')
|
13
|
+
GoogleBooks.send(:query).should include 'startIndex=0'
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should calculate based on default count of 5 if no count is given" do
|
17
|
+
GoogleBooks.search("the great gatsby", :page => 4)
|
18
|
+
GoogleBooks.send(:query).should include 'startIndex=15'
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should set based on page number and count" do
|
22
|
+
GoogleBooks.search('the great gatsby', {:page => 3, :count => 12})
|
23
|
+
GoogleBooks.send(:query).should include 'startIndex=24'
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should set the number of results per page" do
|
29
|
+
GoogleBooks.search('the great gatsby', :count => 20)
|
30
|
+
GoogleBooks.send(:query).should include 'maxResults=20'
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should join parameters" do
|
34
|
+
GoogleBooks.search('the great gatsby', :count => 20, :page => 2)
|
35
|
+
GoogleBooks.send(:query).should include 'startIndex=2'
|
36
|
+
GoogleBooks.send(:query).should include 'maxResults=20'
|
37
|
+
GoogleBooks.send(:query).should include 'q=the+great+gatsby'
|
38
|
+
GoogleBooks.send(:query).count('&').should eq 2
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should return the proper number results based on the count passed in" do
|
42
|
+
results = GoogleBooks.search('F. Scott Fitzgerald', :count => 20)
|
43
|
+
results.count.should eq 20
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should return a response" do
|
47
|
+
GoogleBooks.search('the great gatsby').should be_a GoogleBooks::Response
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: googlebooks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-20 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: httparty
|
16
|
-
requirement: &
|
16
|
+
requirement: &83731200 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *83731200
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &83730990 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,18 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *83730990
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: webmock
|
38
|
+
requirement: &83730780 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *83730780
|
36
47
|
description: GoogleBooks is a lightweight Ruby wrapper that queries the Google API
|
37
48
|
to search for publications in the Google Books repository. It is inspired by the
|
38
49
|
google-book gem which relies on the deprecated Google GData Books API, but is updated
|
@@ -43,13 +54,20 @@ executables: []
|
|
43
54
|
extensions: []
|
44
55
|
extra_rdoc_files: []
|
45
56
|
files:
|
46
|
-
-
|
47
|
-
- lib/googlebooks.rb
|
48
|
-
- lib/book/response.rb
|
49
|
-
- lib/book/item.rb
|
50
|
-
- googlebooks.gemspec
|
57
|
+
- .gitignore
|
51
58
|
- Gemfile
|
59
|
+
- LICENSE
|
60
|
+
- README.md
|
52
61
|
- Rakefile
|
62
|
+
- googlebooks.gemspec
|
63
|
+
- lib/book/item.rb
|
64
|
+
- lib/book/response.rb
|
65
|
+
- lib/googlebooks.rb
|
66
|
+
- lib/version.rb
|
67
|
+
- spec/googlebooks/book/item_spec.rb
|
68
|
+
- spec/googlebooks/book/response_spec.rb
|
69
|
+
- spec/googlebooks/googlebooks_spec.rb
|
70
|
+
- spec/spec_helper.rb
|
53
71
|
homepage: https://github.com/zeantsoi/googlebooks
|
54
72
|
licenses: []
|
55
73
|
post_install_message:
|