googlebooks 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in gbooks.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # require "bundler/gem_tasks"
2
+
3
+ require 'bundler'
4
+ require 'rspec/core/rake_task'
5
+
6
+ Bundler::GemHelper.install_tasks
7
+
8
+ desc 'Run all specs in spec directory'
9
+ RSpec::Core::RakeTask.new(:spec) do |spec|
10
+ spec.pattern = 'spec/**/*_spec.rb'
11
+ end
12
+
13
+ task :default => :spec
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "googlebooks"
7
+ s.version = GoogleBooks::VERSION
8
+ s.authors = ["Zean Tsoi"]
9
+ s.email = ["zean.tsoi@gmail.com"]
10
+ s.homepage = "https://github.com/zeantsoi/googlebooks"
11
+ s.summary = %q{GoogleBooks is a lightweight Ruby wrapper that queries the Google API to search for publications in the Google Books repository.}
12
+
13
+ s.description = %q{GoogleBooks is a lightweight Ruby wrapper that queries the Google API to search for publications in the Google Books repository. It is inspired by the google-book gem which relies on the deprecated Google GData Books API, but is updated to hook into the current Google API.}
14
+
15
+ s.rubyforge_project = "googlebooks"
16
+
17
+ s.add_dependency('httparty')
18
+ s.add_development_dependency('rspec')
19
+
20
+ #s.files = `git ls-files`.split("\n")
21
+ s.files = Dir["{lib}/**/*", "googlebooks.gemspec", "Gemfile", "Rakefile"]
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
25
+
26
+ # specify any dependencies here; for example:
27
+ # s.add_development_dependency "rspec"
28
+ # s.add_runtime_dependency "rest-client"
29
+ end
data/lib/book/item.rb ADDED
@@ -0,0 +1,49 @@
1
+ module GoogleBooks
2
+
3
+ class Item
4
+ attr_reader :kind, :id, :title, :authors, :publisher, :published_date, :description, :isbn, :isbn_10, :isbn_13, :page_count, :print_type, :categories, :average_rating, :ratings_count, :language, :preview_link, :info_link
5
+
6
+ def initialize(item)
7
+ @item = item
8
+ @volume_info = @item['volumeInfo']
9
+ retrieve_attribute
10
+ end
11
+
12
+ # Enables image_link attribute to be customized via passing
13
+ # optional zoom and edge arguments as a hash
14
+ def image_link(opts = {})
15
+ opts[:zoom] ||= 1
16
+ opts[:curl] ||= false
17
+ @volume_info['imageLinks']['thumbnail'].gsub('zoom=1', "zoom=#{opts[:zoom]}").gsub('&edge=curl', "&edge=#{opts[:curl] ? 'curl' : 'none'}") rescue nil
18
+ end
19
+
20
+ private
21
+
22
+ def retrieve_attribute
23
+ @kind = @item['kind']
24
+ @id = @item['id']
25
+ @title = build_title
26
+ @authors = [@volume_info['authors']].flatten.join(', ')
27
+ @publisher = @volume_info['publisher']
28
+ @published_date = @volume_info['publishedDate']
29
+ @description = @volume_info['description']
30
+ @isbn = @volume_info['industryIdentifiers'][1]['identifier'] rescue nil
31
+ @isbn_10 = @volume_info['industryIdentifiers'][0]['identifier'] rescue nil
32
+ @isbn_13 = @isbn
33
+ @page_count = @volume_info['pageCount']
34
+ @print_type = @volume_info['printType']
35
+ @categories = [@volume_info['categories']].flatten.join(', ')
36
+ @average_rating = @volume_info['averageRating']
37
+ @ratings_count = @volume_info['ratingsCount']
38
+ @language = @volume_info['language']
39
+ @preview_link = @volume_info['previewLink']
40
+ @info_link = @volume_info['infoLink']
41
+ end
42
+
43
+ def build_title
44
+ title = [@volume_info['title']].flatten.join(': ')
45
+ @volume_info['subtitle'].nil? ? title : title + ": " + @volume_info['subtitle']
46
+ end
47
+ end
48
+
49
+ end
@@ -0,0 +1,27 @@
1
+ require 'book/item'
2
+
3
+ module GoogleBooks
4
+ class Response
5
+ include Enumerable
6
+
7
+ def initialize(response)
8
+ @response = response
9
+ end
10
+
11
+ # Returns nil if no records are returned. Otherwise, response returns
12
+ # hash of generally unusable Google API specific data.
13
+ def each(&block)
14
+ return [] if total_items == 0
15
+ @response['items'].each do |item|
16
+ block.call(Item.new(item))
17
+ end
18
+ end
19
+
20
+ # Total items returnable based on query, not total items in response
21
+ # (which is throttled by maxResults)
22
+ def total_items
23
+ @response['totalItems'].to_i
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,58 @@
1
+ require 'version'
2
+ require 'book/response'
3
+
4
+ require 'httparty'
5
+ require 'cgi'
6
+
7
+ module GoogleBooks
8
+
9
+ include HTTParty
10
+ format :json
11
+
12
+ class << self
13
+
14
+ attr_accessor :parameters
15
+
16
+ # Submits query to the current Google API for Books.
17
+ #
18
+ # 1st param passes all varieties of acceptable query strings
19
+ #
20
+ # 2nd param passes options hash:
21
+ # * :count passes number of results to display per page (default=5)
22
+ # * :page passes the page number (default=1)
23
+ #
24
+ # 3rd parameter optionally passes user's IP address
25
+ # * User IP may be require in order for request to be made to the
26
+ # Google API from applications residing on decentralized cloud servers
27
+ # See http://www.google.com/support/forum/p/booksearch-apis/thread?tid=2034bed9a98c15cb&hl=en
28
+
29
+ def search(query, opts = {}, remote_ip = nil)
30
+ (headers 'X-Forwarded-For' => remote_ip.to_s) unless remote_ip.nil?
31
+ self.parameters = { 'q' => query }
32
+ opts[:page] ||= 1
33
+ opts[:count] ||= 5
34
+ parameters['startIndex'] = opts[:count] * (opts[:page] - 1)
35
+ parameters['maxResults'] = opts[:count]
36
+
37
+ Response.new(get(url.to_s))
38
+ end
39
+
40
+ private
41
+
42
+ def query
43
+ parameters.
44
+ map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.
45
+ join('&')
46
+ end
47
+
48
+ # Queries the new Google API. The former Google Book Search API is deprecated
49
+ # http://code.google.com/apis/books/docs/gdata/developers_guide_protocol.html
50
+ def url
51
+ URI::HTTPS.build(:host => 'www.googleapis.com',
52
+ :path => '/books/v1/volumes',
53
+ :query => query)
54
+ end
55
+ end
56
+ end
57
+
58
+
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module GoogleBooks
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: googlebooks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Zean Tsoi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-07 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: &16098280 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *16098280
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &16097780 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *16097780
36
+ description: GoogleBooks is a lightweight Ruby wrapper that queries the Google API
37
+ to search for publications in the Google Books repository. It is inspired by the
38
+ google-book gem which relies on the deprecated Google GData Books API, but is updated
39
+ to hook into the current Google API.
40
+ email:
41
+ - zean.tsoi@gmail.com
42
+ executables: []
43
+ extensions: []
44
+ extra_rdoc_files: []
45
+ files:
46
+ - lib/book/response.rb
47
+ - lib/book/item.rb
48
+ - lib/version.rb
49
+ - lib/googlebooks.rb
50
+ - googlebooks.gemspec
51
+ - Gemfile
52
+ - Rakefile
53
+ homepage: https://github.com/zeantsoi/googlebooks
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project: googlebooks
73
+ rubygems_version: 1.8.10
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: GoogleBooks is a lightweight Ruby wrapper that queries the Google API to
77
+ search for publications in the Google Books repository.
78
+ test_files: []