google-book 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ spec/fixtures/cassette_library/*.yml
5
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ -cfd
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'http://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ Version 2, December 2004
3
+
4
+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
5
+
6
+ Everyone is permitted to copy and distribute verbatim or modified
7
+ copies of this license document, and changing it is allowed as long
8
+ as the name is changed.
9
+
10
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12
+
13
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
@@ -0,0 +1,4 @@
1
+ Google Book
2
+ ===========
3
+
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).
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ require 'rspec/core/rake_task'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ desc 'Run all specs in spec directory'
7
+ RSpec::Core::RakeTask.new(:spec) do |spec|
8
+ spec.pattern = 'spec/**/*_spec.rb'
9
+ end
10
+
11
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'google/book/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'google-book'
7
+ s.version = Google::Book::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Paper Cavalier']
10
+ s.email = ['code@papercavalier.com']
11
+ s.homepage = 'http://rubygems.org/gems/google-book'
12
+ s.summary = %q{A Ruby wrapper to the Google Book Search Data API}
13
+ s.description = %q{Google Book is a Ruby wrapper to the Google Book Search Data API.}
14
+
15
+ s.rubyforge_project = 'google-book'
16
+
17
+ s.add_dependency('bookland', ['~> 1.0.0'])
18
+ s.add_dependency('httparty', ['~> 0.7.4'])
19
+ s.add_development_dependency('rspec', ['~> 2.5.0'])
20
+ s.add_development_dependency('vcr', '~> 1.7.0')
21
+ s.add_development_dependency('webmock', '~> 1.6.2')
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
26
+ s.require_paths = ['lib']
27
+ end
@@ -0,0 +1,7 @@
1
+ require 'bookland'
2
+ require 'httparty'
3
+
4
+ require 'google/book/cover'
5
+ require 'google/book/request'
6
+ require 'google/book/response'
7
+ require 'google/book/struct'
@@ -0,0 +1,37 @@
1
+ module Google
2
+ module Book
3
+ class Cover
4
+ def initialize(url)
5
+ @url = url
6
+ end
7
+
8
+ def thumbnail
9
+ cover_url(5)
10
+ end
11
+
12
+ def small
13
+ cover_url(1)
14
+ end
15
+
16
+ def medium
17
+ cover_url(2)
18
+ end
19
+
20
+ def large
21
+ cover_url(3)
22
+ end
23
+
24
+ def extra_large
25
+ cover_url(6)
26
+ end
27
+
28
+ private
29
+
30
+ def cover_url(zoom)
31
+ @url.
32
+ gsub('zoom=5', "zoom=#{zoom}").
33
+ gsub('&edge=curl', '')
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,42 @@
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
@@ -0,0 +1,30 @@
1
+ module Google
2
+ module Book
3
+ class Response
4
+ def initialize(hash)
5
+ @feed = hash['feed']
6
+ end
7
+
8
+ def to_books
9
+ @feed['entry'].map do |hash|
10
+ book = Struct.new(
11
+ Cover.new(hash['link'][0]['href']),
12
+ hash['link'][1]['href'],
13
+ hash['link'][2]['href'],
14
+ [hash['dc:creator']].flatten,
15
+ hash['dc:date'],
16
+ hash['dc:description'],
17
+ [hash['dc:format']].flatten,
18
+ hash['dc:identifier'],
19
+ hash['dc:publisher'],
20
+ hash['dc:subject'],
21
+ [hash['dc:title']].flatten)
22
+ end
23
+ end
24
+
25
+ def total_results
26
+ @feed['openSearch:totalResults'].to_i
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,50 @@
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(/Intl( |$)/) { "International#{$1}" }.
31
+ gsub(/Pr( |$)/) { "Press#{$1}" }.
32
+ gsub(/ Pub$/, ' Publishers').
33
+ gsub('Pub Group', 'Publishing Group').
34
+ gsub(/Univ( |$)/) { "University#{$1}" }
35
+ end
36
+
37
+ def formatted_title
38
+ titles.join(': ')
39
+ end
40
+
41
+ def isbn
42
+ identifiers.detect do |identifier|
43
+ identifier.match(/ISBN:([0-9X]{10,13})/)
44
+ end
45
+
46
+ ISBN.to_13($1) rescue nil
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ module Google
2
+ module Book
3
+ VERSION = '0.2.0'
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ module Google
4
+ module Book
5
+ describe Cover do
6
+ use_vcr_cassette 'google'
7
+
8
+ subject { Request.find('deleuze').to_books.first.cover }
9
+
10
+ %w{thumbnail small medium large extra_large}.each do |attribute|
11
+ its(attribute) { should_not be_nil }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
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
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ module Google
4
+ module Book
5
+ describe Response do
6
+ use_vcr_cassette 'google'
7
+
8
+ let(:response) do
9
+ Request.find('deleuze')
10
+ end
11
+
12
+ it "should set total results" do
13
+ response.total_results.should > 0
14
+ end
15
+
16
+ it "should return books" do
17
+ books = response.to_books
18
+ books.first.should be_an_instance_of Struct
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,98 @@
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 word" 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 format Intl" do
48
+ book.publisher = 'Continuum Intl'
49
+ book.formatted_publisher.should eql 'Continuum International'
50
+ end
51
+
52
+ it "should format Pr" do
53
+ book.publisher = 'Polity Pr'
54
+ book.formatted_publisher.should eql 'Polity Press'
55
+ end
56
+
57
+ it "should format Pub" do
58
+ book.publisher = 'Rowman & Littlefield Pub'
59
+ book.formatted_publisher.should eql 'Rowman & Littlefield Publishers'
60
+ end
61
+
62
+ it "should format Pub Group" do
63
+ book.publisher = 'Continuum International Pub Group'
64
+ book.formatted_publisher.should eql 'Continuum International Publishing Group'
65
+ end
66
+
67
+ it "should format Univ in the middle of the word" do
68
+ book.publisher = 'Columbia Univ Press'
69
+ book.formatted_publisher.should eql 'Columbia University Press'
70
+ end
71
+
72
+ it "should format Univ at the end of the word" do
73
+ book.publisher = 'Columbia Univ'
74
+ book.formatted_publisher.should eql 'Columbia University'
75
+ end
76
+ end
77
+
78
+ describe "#formatted_title" do
79
+ it "should concatenate titles" do
80
+ book.titles = ['Thousand Plateaus', 'Capitalism and Schizophrenia']
81
+ book.formatted_title.should eql 'Thousand Plateaus: Capitalism and Schizophrenia'
82
+ end
83
+ end
84
+
85
+ describe "#isbn" do
86
+ it "should return the ISBN" do
87
+ book.identifiers = ['8cp-Z_G42g4C','ISBN:0192802380']
88
+ book.isbn.should eql '9780192802385'
89
+ end
90
+
91
+ it "should return nil if no ISBN is available" do
92
+ book.identifiers = []
93
+ book.isbn.should eql nil
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rspec'
4
+
5
+ require File.expand_path('../../lib/google', __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.default_cassette_options = { :record => :new_episodes }
6
+ c.stub_with :webmock
7
+ end
8
+
9
+ RSpec.configure do |config|
10
+ config.extend VCR::RSpec::Macros
11
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google-book
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.2.0
6
+ platform: ruby
7
+ authors:
8
+ - Paper Cavalier
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-05 00:00:00 +00:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: bookland
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: 1.0.0
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: httparty
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 0.7.4
36
+ type: :runtime
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: vcr
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ version: 1.7.0
58
+ type: :development
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: webmock
62
+ prerelease: false
63
+ requirement: &id005 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.6.2
69
+ type: :development
70
+ version_requirements: *id005
71
+ description: Google Book is a Ruby wrapper to the Google Book Search Data API.
72
+ email:
73
+ - code@papercavalier.com
74
+ executables: []
75
+
76
+ extensions: []
77
+
78
+ extra_rdoc_files: []
79
+
80
+ files:
81
+ - .gitignore
82
+ - .rspec
83
+ - Gemfile
84
+ - LICENSE
85
+ - README.md
86
+ - Rakefile
87
+ - google.gemspec
88
+ - lib/google.rb
89
+ - lib/google/book/cover.rb
90
+ - lib/google/book/request.rb
91
+ - lib/google/book/response.rb
92
+ - lib/google/book/struct.rb
93
+ - lib/google/book/version.rb
94
+ - spec/google/book/cover_spec.rb
95
+ - spec/google/book/request_spec.rb
96
+ - spec/google/book/response_spec.rb
97
+ - spec/google/book/struct_spec.rb
98
+ - spec/spec_helper.rb
99
+ - spec/support/vcr.rb
100
+ has_rdoc: true
101
+ homepage: http://rubygems.org/gems/google-book
102
+ licenses: []
103
+
104
+ post_install_message:
105
+ rdoc_options: []
106
+
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: "0"
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: "0"
121
+ requirements: []
122
+
123
+ rubyforge_project: google-book
124
+ rubygems_version: 1.5.2
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: A Ruby wrapper to the Google Book Search Data API
128
+ test_files:
129
+ - spec/google/book/cover_spec.rb
130
+ - spec/google/book/request_spec.rb
131
+ - spec/google/book/response_spec.rb
132
+ - spec/google/book/struct_spec.rb
133
+ - spec/spec_helper.rb
134
+ - spec/support/vcr.rb