googlebook 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ spec/fixtures/cassette_library/*.yml
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ -cfd
data/CHANGELOG.md ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in googlebooks.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ googlebook (0.0.1)
5
+ httparty (~> 0.6.1)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ addressable (2.2.2)
11
+ crack (0.1.8)
12
+ diff-lcs (1.1.2)
13
+ httparty (0.6.1)
14
+ crack (= 0.1.8)
15
+ json (1.4.6)
16
+ rdiscount (1.6.5)
17
+ rdoc (2.4.3)
18
+ rspec (2.0.1)
19
+ rspec-core (~> 2.0.1)
20
+ rspec-expectations (~> 2.0.1)
21
+ rspec-mocks (~> 2.0.1)
22
+ rspec-core (2.0.1)
23
+ rspec-expectations (2.0.1)
24
+ diff-lcs (>= 1.1.2)
25
+ rspec-mocks (2.0.1)
26
+ rspec-core (~> 2.0.1)
27
+ rspec-expectations (~> 2.0.1)
28
+ sdoc (0.2.20)
29
+ json (>= 1.1.3)
30
+ rdoc (= 2.4.3)
31
+ sdoc-helpers (0.1.4)
32
+ sdoc (~> 0.2)
33
+ vcr (1.2.0)
34
+ webmock (1.4.0)
35
+ addressable (>= 2.2.2)
36
+ crack (>= 0.1.7)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ googlebook!
43
+ httparty (~> 0.6.1)
44
+ rdiscount (~> 1.6.5)
45
+ rspec (~> 2.0.1)
46
+ sdoc-helpers (~> 0.1.4)
47
+ vcr (~> 1.2.0)
48
+ webmock (~> 1.4.0)
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2010 Paper Cavalier
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ Google Book
2
+ ===========
3
+
4
+ Google Book is a work-in-progress Ruby class in tight embrace with the [Google Book Search Data API](http://code.google.com/apis/books/docs/gdata/developers_guide_protocol.html).
5
+
6
+ books = GoogleBook.find('deleuze')
7
+
8
+ book = books.first
9
+ book.title
10
+ => "Foucault"
11
+ book.isbn
12
+ => "9780826490780"
13
+
14
+ Paginate through the result set, sort of.
15
+
16
+ GoogleBook.find('deleuze', :page => 2, :count => 20)
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler"
2
+ require "rspec/core/rake_task"
3
+ require "sdoc_helpers"
4
+
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ desc "Run all specs in spec directory"
8
+ RSpec::Core::RakeTask.new(:spec) do |spec|
9
+ spec.pattern = "spec/**/*_spec.rb"
10
+ end
11
+
12
+ task :default => :spec
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "googlebook/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "googlebook"
7
+ s.version = GoogleBook::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Hakan Ensari"]
10
+ s.email = ["hakan.ensari@papercavalier.com"]
11
+ s.homepage = "http://gloss.papercavalier.com/googlebook"
12
+ s.summary = %q{A Ruby class in tight embrace with the Google Book Search Data API}
13
+ s.description = %q{Google Book is a Ruby class in tight embrace with the Google Book Search Data API.}
14
+
15
+ s.rubyforge_project = "googlebook"
16
+
17
+ s.add_dependency("httparty", ["~> 0.6.1"])
18
+ s.add_development_dependency("rdiscount", "~> 1.6.5")
19
+ s.add_development_dependency("rspec", ["~> 2.0.1"])
20
+ s.add_development_dependency("sdoc-helpers", "~> 0.1.4")
21
+ s.add_development_dependency("vcr", "~> 1.2.0")
22
+ s.add_development_dependency("webmock", "~> 1.4.0")
23
+
24
+ s.files = `git ls-files`.split("\n")
25
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
26
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
27
+ s.require_paths = ["lib"]
28
+ end
data/lib/googlebook.rb ADDED
@@ -0,0 +1,24 @@
1
+ require File.dirname(__FILE__) + '/googlebook/finder'
2
+
3
+ # A Ruby class in tight embrace with the Google Book Search Data API
4
+ class GoogleBook
5
+ attr_accessor :thumbnail,
6
+ :info,
7
+ :preview,
8
+ :creator,
9
+ :date,
10
+ :description,
11
+ :format,
12
+ :identifier,
13
+ :publisher,
14
+ :subject,
15
+ :title
16
+
17
+ def initialize(args={})
18
+ args.each { |k, v| self.send "#{k}=", v }
19
+ end
20
+
21
+ def isbn
22
+ identifier.detect { |i| i.match(/ISBN:([0-9]{13})/) }; $1
23
+ end
24
+ end
@@ -0,0 +1,63 @@
1
+ require 'cgi'
2
+ require 'httparty'
3
+
4
+ # A classy finder
5
+ class GoogleBook
6
+ include HTTParty
7
+ format :xml
8
+
9
+ class << self
10
+ # Query parameters passed on to Google
11
+ attr_accessor :parameters
12
+
13
+ # Total results for last query
14
+ attr_accessor :total_results
15
+
16
+ # Queries the Google Book Search Data API
17
+ #
18
+ # Optionally, specify a page and count to (sort-of) paginate through
19
+ # the result set.
20
+ #
21
+ # GoogleBook.find('deleuze', :page => 2, :count => 20)
22
+ #
23
+ def find(query, opts={})
24
+ self.parameters = { 'q' => query }
25
+
26
+ if opts[:page] && opts[:page].to_i > 0
27
+ parameters['start-index'] = opts[:page]
28
+ end
29
+
30
+ if opts[:count] && (10..20).include?(opts[:count].to_i)
31
+ parameters['max-results'] = opts[:count]
32
+ end
33
+
34
+ response = self.get(uri.to_s)
35
+ self.total_results = response['feed']['openSearch:totalResults'].to_i
36
+ response['feed']['entry'].map do |book|
37
+ new :thumbnail => book['link'][0]['href'],
38
+ :info => book['link'][1]['href'],
39
+ :preview => book['link'][2]['href'],
40
+ :creator => book['dc:creator'],
41
+ :date => book['dc:date'],
42
+ :description => book['dc:description'],
43
+ :format => book['dc:format'],
44
+ :identifier => book['dc:identifier'],
45
+ :publisher => book['dc:publisher'],
46
+ :subject => book['dc:subject'],
47
+ :title => book['dc:title']
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ def query
54
+ parameters.map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
55
+ end
56
+
57
+ def uri
58
+ URI::HTTP.build :host => 'books.google.com',
59
+ :path => '/books/feeds/volumes',
60
+ :query => query
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ class GoogleBook
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoogleBook do
4
+ use_vcr_cassette 'googlebook', :record => :new_episodes
5
+
6
+ context "Finder" do
7
+ it "should escape parameters" do
8
+ GoogleBook.find('foo bar')
9
+ GoogleBook.send(:query).should include 'foo+bar'
10
+ end
11
+
12
+ it "should set page" do
13
+ GoogleBook.find('foo bar', :page => 2)
14
+ GoogleBook.send(:query).should include 'start-index=2'
15
+ end
16
+
17
+ it "should set number of results per page" do
18
+ GoogleBook.find('foo bar', :count => 20)
19
+ GoogleBook.send(:query).should include 'max-results=20'
20
+ end
21
+
22
+ it "should validate page" do
23
+ %w{-1 0 foo}.each do |page|
24
+ GoogleBook.find('foo bar', :page => page)
25
+ GoogleBook.send(:query).should_not include 'start-index'
26
+ end
27
+ end
28
+
29
+ it "should validate results per page" do
30
+ %w{9 21 foo}.each do |count|
31
+ GoogleBook.find('foo bar', :count => count)
32
+ GoogleBook.send(:query).should_not include 'max-results'
33
+ end
34
+ end
35
+
36
+ it "should set total results" do
37
+ GoogleBook.find('deleuze')
38
+ GoogleBook.total_results.should > 0
39
+ end
40
+
41
+ it "should return books" do
42
+ GoogleBook.find('deleuze').first.should be_an_instance_of GoogleBook
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoogleBook do
4
+ use_vcr_cassette 'googlebook', :record => :new_episodes
5
+
6
+ subject { GoogleBook.find('deleuze').first }
7
+
8
+ %w{thumbnail info preview creator date description format
9
+ identifier isbn publisher subject title}.each do |attribute|
10
+ its(attribute) { should_not be_nil }
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ require "rspec"
4
+
5
+ require File.expand_path("../../lib/googlebook", __FILE__)
6
+
7
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
@@ -0,0 +1,11 @@
1
+ require 'vcr'
2
+
3
+ VCR.config do |c|
4
+ c.cassette_library_dir = File.dirname(__FILE__) + '/../fixtures/cassette_library'
5
+ c.http_stubbing_library = :webmock
6
+ c.default_cassette_options = { :record => :new_episodes }
7
+ end
8
+
9
+ RSpec.configure do |config|
10
+ config.extend VCR::RSpec::Macros
11
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: googlebook
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Hakan Ensari
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-26 00:00:00 +01: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
+ - 6
31
+ - 1
32
+ version: 0.6.1
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rdiscount
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 1
45
+ - 6
46
+ - 5
47
+ version: 1.6.5
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rspec
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 2
60
+ - 0
61
+ - 1
62
+ version: 2.0.1
63
+ type: :development
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: sdoc-helpers
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ~>
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ - 1
76
+ - 4
77
+ version: 0.1.4
78
+ type: :development
79
+ version_requirements: *id004
80
+ - !ruby/object:Gem::Dependency
81
+ name: vcr
82
+ prerelease: false
83
+ requirement: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ~>
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 1
90
+ - 2
91
+ - 0
92
+ version: 1.2.0
93
+ type: :development
94
+ version_requirements: *id005
95
+ - !ruby/object:Gem::Dependency
96
+ name: webmock
97
+ prerelease: false
98
+ requirement: &id006 !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ segments:
104
+ - 1
105
+ - 4
106
+ - 0
107
+ version: 1.4.0
108
+ type: :development
109
+ version_requirements: *id006
110
+ description: Google Book is a Ruby class in tight embrace with the Google Book Search Data API.
111
+ email:
112
+ - hakan.ensari@papercavalier.com
113
+ executables: []
114
+
115
+ extensions: []
116
+
117
+ extra_rdoc_files: []
118
+
119
+ files:
120
+ - .gitignore
121
+ - .rspec
122
+ - CHANGELOG.md
123
+ - Gemfile
124
+ - Gemfile.lock
125
+ - LICENSE
126
+ - README.md
127
+ - Rakefile
128
+ - googlebook.gemspec
129
+ - lib/googlebook.rb
130
+ - lib/googlebook/finder.rb
131
+ - lib/googlebook/version.rb
132
+ - spec/googlebook/finder_spec.rb
133
+ - spec/googlebook_spec.rb
134
+ - spec/spec_helper.rb
135
+ - spec/support/vcr.rb
136
+ has_rdoc: true
137
+ homepage: http://gloss.papercavalier.com/googlebook
138
+ licenses: []
139
+
140
+ post_install_message:
141
+ rdoc_options: []
142
+
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ segments:
151
+ - 0
152
+ version: "0"
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ segments:
159
+ - 0
160
+ version: "0"
161
+ requirements: []
162
+
163
+ rubyforge_project: googlebook
164
+ rubygems_version: 1.3.7
165
+ signing_key:
166
+ specification_version: 3
167
+ summary: A Ruby class in tight embrace with the Google Book Search Data API
168
+ test_files:
169
+ - spec/googlebook/finder_spec.rb
170
+ - spec/googlebook_spec.rb
171
+ - spec/spec_helper.rb
172
+ - spec/support/vcr.rb