google_book 0.2.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 34025d0e5be1f2a4b0ef1dd93f32d53af55f5899
4
+ data.tar.gz: 88ea8dc02c2e0f6d780919a49df67c32695c0128
5
+ SHA512:
6
+ metadata.gz: bc827dacb2afeb540e7284cfd2db63fee4925c716bb2cc36688ddc05d6fb0b1f498cc0a47468b605d308c734d4e80f89c40db1e419ab4f8e119c7c6131396f7c
7
+ data.tar.gz: 935404e9528281c8da1315174e2fe8eb634e0042ab1142dc921699aeaa82146163b0a4fa407e358769080726debbfa3c32ef13c499f5d99ba1c70a9d3284f1af
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'rspec'
4
+ gem 'rake'
5
+ gem 'webmock'
data/README.md ADDED
@@ -0,0 +1,130 @@
1
+ <html>
2
+ <head>
3
+ <title>Google Book API Integration</title>
4
+ </head>
5
+ <body>
6
+
7
+ <div>
8
+ <a href = "https://codeclimate.com/repos/51fa3a5356b102601a00d3e1/feed"><img src="https://codeclimate.com/repos/51fa3a5356b102601a00d3e1/badges/8db2d24d585c3d2ac1f1/gpa.png" /></a>
9
+ <a href = "https://travis-ci.org/rajcybage/google_book"><img src= "https://travis-ci.org/rajcybage/google_book.png"/></a>
10
+ </div>
11
+
12
+ <h1>Google Book ApI Integration</h1><br/>
13
+ <p>You can use it in your web application as well as desktop application to fetch google books</p>
14
+ <div>
15
+ <h2>Installation</h2>
16
+ <p>In Gemfile
17
+ <code>gem 'google_book', git: 'git@github.com:rajcybage/google_book.git'</code>
18
+ </p>
19
+ </div>
20
+ <div>
21
+ <h2>System Requirements</h2>
22
+ <code>Ruby >= 1.9.3 or jruby >=1.7.x</code>
23
+ <code>rails >= 3.0.x</code>
24
+ </div>
25
+
26
+ <div>
27
+ <h2>Usage</h2>
28
+ <p>
29
+ <code>
30
+ b=GoogleBook::Book.new(:api_key => "YOUR_GOOOGLE_API_KEY")
31
+ </code><br/>
32
+ <code>
33
+ b.search("Name of the book you want to search",type) #type should be a number
34
+ </code>
35
+ </p>
36
+ <ul>
37
+ <li> type = 1 ===> intitle: Returns results where the text following this keyword is found in the title.</li>
38
+ <li> type = 2 ===> inauthor: Returns results where the text following this keyword is found in the author.</li>
39
+ <li> type = 3 ====> inpublisher: Returns results where the text following this keyword is found in the publisher.</li>
40
+ <li>type = 4 ===> subject: Returns results where the text following this keyword is listed in the category list of the volume.</li>
41
+ <li>type = 5 ====>isbn: Returns results where the text following this keyword is the ISBN number.</li>
42
+ <li>ltype = 6 ====> ccn: Returns results where the text following this keyword is the Library of Congress Control Number.</li>
43
+ <li>type = 7 ===> oclc: Returns results where the text following this keyword is the Online Computer Library Center number.
44
+ <li> type = 8 ===> Returns the book which can be downloadable</li>
45
+ </li>
46
+ </ul>
47
+ <p>
48
+ <code>b=GoogleBook::Book.new(:api_key => "YOUR_GOOOGLE_API_KEY")</code> <br/>
49
+ <code> b.search("keyword or book name",1)</code> <br/>
50
+ <code>b.book_info.get_all_titles #to get the all titles of your search book</code> <br/>
51
+ <code>b.book_info.get_all_subtitles # to get all the subtitles of your search book</code>
52
+ <code>b.total_count #get the search results count</code>
53
+ </p>
54
+
55
+
56
+ </div>
57
+
58
+ <h2>If you want to access each and every book as an instance of book</h2>
59
+ <p>you can then create a instance of books which was getting by search by following ways</p>
60
+ <div>
61
+ <code>b=GoogleBook::Book.new(:api_key => "YOUR_GOOOGLE_API_KEY")</code> <br/>
62
+ <code> b.search("Name of the book you want to search",1)</code> <br/>
63
+ <code>b.books #create the instance of all the books of your search</code><br/>
64
+ <code>b.books.first.get_title #get the first book title</code><br/>
65
+ <code>b.books.first.get_sub_title#get the first book sub title</code><br/>
66
+ <code>b.books.first.authors #get the first book authors</code><br/>
67
+ <code>b.books.first.publish_date #get the first book published date</code><br/>
68
+ <code>b.books.first.preview_link #get the first book preview link</code><br/>
69
+ <code>b.books.first.info_link #get the first book information</code><br/>
70
+ <code>b.books.first.thumbnail_image #get the first book thumbnail image</code><br/>
71
+ <code>b.books.first.small_thumbnail_link #get the first book thumbnail image</code><br/>
72
+ </div>
73
+
74
+
75
+
76
+
77
+ <h2>Want to search Downloadable Books</h2>
78
+ <p>
79
+ <div>
80
+ <code>b=GoogleBook::Book.new(:api_key => "YOUR_GOOOGLE_API_KEY")</code> <br/>
81
+ <code>b.search('downloadable book name',8)</code>
82
+ <code>b.books.first.downloadable?#check whther the book is downloadable</code><br/>
83
+ <code>b.books.first.buyable?#check whether the book is buyable</code><br/>
84
+ <code>b.books.first.buy_link</code><br/>
85
+ <code>b.books.first.download_link</code>
86
+ </div>
87
+ </p>
88
+
89
+
90
+ <h2>Want to get Magazines only</h2>
91
+ <p>
92
+ <div>
93
+ <code>b=GoogleBook::Book.new(:api_key => "YOUR_GOOOGLE_API_KEY")</code> <br/>
94
+ <code>b.search('downloadable book name',<b>9</b>) #type is 9 for only magazines</code>
95
+ <code>b.books #create the magaize instances</code><br/>
96
+ <code>b.books.first.magazine?</code><br/>
97
+ <span> so on..now get all the info(title,subtitle,author,preview....) of each and every magazine instanaces as stated above</span>
98
+ </div>
99
+ </p>
100
+ <h2>Filter your Search</h2>
101
+ <p>
102
+ <div>
103
+ <ul>
104
+ <li> filter_type = 1 free-ebooks - Only returns results that are free Google eBooks.</li>
105
+ <li> filter_type = 2 paid-ebooks - Only returns results that are Google eBooks with a price.</li>
106
+ <li> filter_type = 3 full - Only returns results where all of the text is viewable.</li>
107
+ <li> filter_type = 4 ebooks - Only returns results that are Google eBooks, paid or free.
108
+ Examples of non-eBooks would be publisher content that is available in limited preview and not for sale,
109
+ or magazines.</li>
110
+ </ul>
111
+ </div>
112
+
113
+ <div>
114
+ <code>b=GoogleBook::Book.new(:api_key => "YOUR_GOOOGLE_API_KEY")</code><br/>
115
+ <code>b.filter('ruby language', 1)#here one is <b>filter_type</b> as defined above</code><br/>
116
+ <span>Now create all books instance as usual</span><br/>
117
+ <code>b.books</code>
118
+ <span><b>If you select your filter as free ebook then it will be downloadable and buyable</b></span><br/>
119
+
120
+ <code>b.books.first.downloadable?#check whther the book is downloadable</code><br/>
121
+ <code>b.books.first.buyable?#check whether the book is buyable</code><br/>
122
+ <code>b.books.first.buy_link</code><br/>
123
+ <code>b.books.first.download_link</code>
124
+
125
+ </div>
126
+ </p>
127
+
128
+
129
+ </body>
130
+ </html>
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ require 'rspec'
4
+ require 'rspec/core'
5
+ require 'rspec/mocks'
6
+ require 'rspec/expectations'
7
+ require 'rspec/core/rake_task'
8
+ RSpec::Core::RakeTask.new(:spec) do |t|
9
+ t.pattern = 'spec/*_spec.rb'
10
+ t.rspec_opts ||= []
11
+ t.rspec_opts << '--color'
12
+ t.rspec_opts << '-f documentation'
13
+ end
14
+
15
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ require "google_book/base.rb"
2
+ require 'google_book/book_info.rb'
3
+ require 'google_book/book_item.rb'
@@ -0,0 +1,167 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'openssl'
4
+ require 'json'
5
+ require_relative 'book_info.rb'
6
+ require_relative 'book_item.rb'
7
+
8
+ module GoogleBook
9
+ class Book
10
+ attr_accessor :api_key, :total_count, :items, :books
11
+
12
+ def initialize(api_key)
13
+ unless api_key.nil?
14
+ @api_key = api_key[:api_key]
15
+ else
16
+ raise "Api key can not be nil. Please given your api_key"
17
+ end
18
+ end
19
+
20
+ #Google books search
21
+ def search(query,type = nil)
22
+ checking_type(type)
23
+ type = set_type(type) unless type.nil?
24
+ if query.nil?
25
+ puts 'Enter the text to search'
26
+ text = gets
27
+ end
28
+ json = JSON.parse(connect_google(@api_key,type,query))
29
+ @total_count = json["totalItems"]
30
+ @items = json["items"]
31
+ end
32
+
33
+ #Google books filtration
34
+ def filter(query, type = nil)
35
+ checking_filter_type(type)
36
+ filter_type = set_filter_type(type) unless type.nil?
37
+ json = JSON.parse(connect_google(@api_key,nil,query,filter_type))
38
+ @total_count = json["totalItems"]
39
+ @items = json["items"]
40
+ end
41
+
42
+ #get all the books information
43
+ def book_info
44
+ BookInfo.new(items: @items)
45
+ end
46
+
47
+ #create all book instances
48
+ def books
49
+ @books = []
50
+ @items.each do |item|
51
+ @books << BookItem.new(item: item)
52
+ end
53
+ return @books
54
+ end
55
+
56
+ #use for rails application
57
+ def api_key
58
+ @api_key = YAML.load_file("#{Rails.root}/config/api_key_google_book.yml")[Rails.env]
59
+ end
60
+
61
+ def checking_filter_type(type)
62
+ if type.nil?
63
+ puts "How you want to filter your search?(example:1)?"
64
+ puts "1)Free ebooks"
65
+ puts "2)Paid ebboks"
66
+ puts "3)Full text"
67
+ f_type = gets
68
+ else
69
+ type = "1"
70
+ end
71
+ end
72
+
73
+
74
+ def set_filter_type(type)
75
+ case type.to_i
76
+ when 1
77
+ type = "free-ebooks"
78
+ when 2
79
+ type = "paid-ebooks"
80
+ when 3
81
+ type = "full"
82
+ else
83
+ type = "ebooks"
84
+ end
85
+ end
86
+
87
+
88
+ def checking_type(type)
89
+ if type.nil?
90
+ puts "How you want to search?input only list number(ex:1)\n"
91
+ puts "1)By Title\n"
92
+ puts "2)By Author Name\n"
93
+ puts "3)By Publisher\n"
94
+ puts "4)By Subject\n"
95
+ puts "5)By ISBN number:- \n"
96
+ puts "6)Library of Congress Control Number\n"
97
+ puts "7)By Online Computer Library Center number\n"
98
+ puts "8)Want to search Downloadable book"
99
+ puts "9)Want to search downloadable Magazines"
100
+ type = gets
101
+ else
102
+ type = "1"
103
+ end
104
+ end
105
+
106
+ def set_type(type)
107
+ case type.to_i
108
+ when 1
109
+ type = "intitle"
110
+ when 2
111
+ type = "inauthor"
112
+ when 3
113
+ type = "inpublisher"
114
+ when 4
115
+ type = "subject"
116
+ when 5
117
+ type = "isbn"
118
+ when 6
119
+ type = "lccn"
120
+ when 7
121
+ type = "oclc"
122
+ when 8
123
+ type = "download"
124
+ when 9
125
+ type = "magazine"
126
+ else
127
+ type = "inauthor"
128
+ end
129
+ return type
130
+ end
131
+
132
+ def url_formation(api_key = nil,type = nil,search_param = nil, filter = nil)
133
+ main_url = "https://www.googleapis.com/books/v1/volumes"
134
+ if !type.nil?
135
+ url = set_normal_url(api_key, type, search_param, main_url)
136
+ elsif type.nil? && !filter.nil?
137
+ url = main_url+"?q=#{search_param.gsub(/\s+/, "+").strip}&filter=#{filter}&key=#{@api_key}"
138
+ else
139
+ url = main_url+"?q=#{search_param.gsub(/\s+/, "+").strip}&key=#{@api_key}"
140
+ end
141
+ puts "#{url}"
142
+ return URI(url)
143
+ end
144
+
145
+ def set_normal_url(api_key, type, search_param, main_url)
146
+ case type
147
+ when "download"
148
+ url = main_url+"?q=#{search_param.gsub(/\s+/, "+").strip}&download=epub&key=#{api_key}"
149
+ when "magazine"
150
+ url = main_url+"?q=#{search_param.gsub(/\s+/, "+").strip}&printType=magazines&key=#{api_key}"
151
+ else
152
+ url = main_url+"?q=#{search_param.gsub(/\s+/, "").strip}+#{type}:keyes&key=#{api_key}"
153
+ end
154
+ return url
155
+ end
156
+
157
+ def connect_google(key = nil,type = nil,search_param = nil,filter = nil)
158
+ uri = url_formation(key,type,search_param,filter)
159
+ http = Net::HTTP.new(uri.host, uri.port)
160
+ http.use_ssl = true
161
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
162
+ response = http.get(uri.request_uri)
163
+ # response = Net::HTTP.get_response(uri)
164
+ return response.body
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,37 @@
1
+ require_relative 'base.rb'
2
+ require_relative 'book_item.rb'
3
+ class BookInfo
4
+ attr_accessor :items, :titles, :subtitles, :book
5
+ @titles = []
6
+ @subtitles = []
7
+ def initialize(items=[])
8
+ @items = items
9
+ end
10
+
11
+ #return the all titles of those books
12
+ def get_all_titles
13
+ show_all_titles_and_subtitles("title")
14
+ return @titles
15
+ end
16
+
17
+ #return all the subtitles
18
+ def get_all_sub_titles
19
+ show_all_titles_and_subtitles("subtitle")
20
+ return @subtitles
21
+ end
22
+
23
+
24
+ #return the particular book
25
+ def get_book(params = {})
26
+ item = @items.detect{|i| i[params.keys.first] == params.values.first} || @items.detect{|i| i["volumeInfo"][params.keys.first] == params.values.first}
27
+ @book = BookItem.new(item: item)
28
+ end
29
+
30
+ private
31
+ def show_all_titles_and_subtitles(flag)
32
+ @items[:items].each do |item|
33
+ @titles << item["volumeInfo"]["title"]
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,94 @@
1
+ require_relative 'book_info.rb'
2
+ require_relative 'base.rb'
3
+
4
+ class BookItem
5
+ attr_reader :item
6
+
7
+ def initialize(item)
8
+ @item = item
9
+ end
10
+
11
+ def get_title
12
+ @item[:item]["volumeInfo"]["title"]
13
+ end
14
+
15
+ def get_sub_title
16
+ @item[:item]["volumeInfo"]["subtitle"]
17
+ end
18
+
19
+ def authors
20
+ @item[:item]["volumeInfo"]["authors"]
21
+ end
22
+
23
+ def publish_date
24
+ @item[:item]["volumeInfo"]["publishedDate"]
25
+ end
26
+
27
+ def preview_link
28
+ @item[:item]["volumeInfo"]["previewLink"]
29
+ end
30
+
31
+ def info_link
32
+ @item[:item]["volumeInfo"]["infoLink"]
33
+ end
34
+
35
+ def canonical_volume_link
36
+ @item[:item]["volumeInfo"]["canonicalVolumeLink"]
37
+ end
38
+
39
+ def thubnail_image
40
+ @item[:item]["volumeInfo"]["imageLinks"]["thumbnail"]
41
+ end
42
+
43
+ def small_thumbnail_link
44
+ @item[:item]["volumeInfo"]["imageLinks"]["smallThumbnail"]
45
+ end
46
+
47
+ def pagecount
48
+ @item[:item]["volumeInfo"]["pageCount"]
49
+ end
50
+
51
+ def web_reader_link
52
+ @item[:item]["accessInfo"]["webReaderLink"]
53
+ end
54
+
55
+ def buy_link
56
+ if buyable?
57
+ @item[:item]["saleInfo"]["buyLink"]
58
+ else
59
+ "your book is not buyable"
60
+ end
61
+ end
62
+
63
+ def magazine?
64
+ if @item[:item]["volumeInfo"]["printType"].upcase == "MAGAZINE"
65
+ true
66
+ else
67
+ false
68
+ end
69
+ end
70
+
71
+ def downloadable?
72
+ unless @item[:item]["accessInfo"]["pdf"].nil? || @item[:item]["accessInfo"]["pdf"]["downloadLink"].nil?
73
+ true
74
+ else
75
+ false
76
+ end
77
+ end
78
+
79
+ def buyable?
80
+ if @item[:item]["saleInfo"]["isEbook"] != true || @item[:item]["saleInfo"]["buyLink"].nil?
81
+ false
82
+ else
83
+ true
84
+ end
85
+ end
86
+
87
+ def download_link
88
+ if downloadable?
89
+ @item[:item]["accessInfo"]["pdf"]["downloadLink"]
90
+ else
91
+ "your book is not downloadable"
92
+ end
93
+ end
94
+ end
data/spec/base_spec.rb ADDED
@@ -0,0 +1,61 @@
1
+ require_relative '../lib/google_book/base.rb'
2
+ require_relative '../lib/google_book/book_item.rb'
3
+ require_relative 'spec_helper.rb'
4
+
5
+ describe "base" do
6
+ let(:api_key){"AIzaSyD82ugogyUN7bAZ4k2UaLlVCep5NxZVYNM"}
7
+ before(:all) do
8
+ @con_book = GoogleBook::Book.new(:api_key => api_key)
9
+ end
10
+
11
+ describe "google_connect" do
12
+ it "should connect google book" do
13
+ stub_request(:get, "https://www.googleapis.com/books/v1/volumes?q=flowers+inauthor:keyes&key=#{api_key}").to_return(:body => "abc")
14
+ end
15
+ end
16
+
17
+ describe "initialize" do
18
+ it "should connect googlebook api properly" do
19
+ @con_book.should_not be_nil
20
+ @con_book.should be_a_kind_of(Object)
21
+ end
22
+ end
23
+
24
+ describe "search" do
25
+
26
+ it "should return anything type" do
27
+ @con_book.search('flowers',2).should_not be_nil
28
+ @con_book.search('flowers',2).should be_a_kind_of(Array)
29
+ end
30
+
31
+ it "should return all books items is eual to the items of the book object" do
32
+ @con_book.search('Flowers',2).should eq @con_book.items
33
+ end
34
+ it "should return magazines after giving filter" do
35
+ @con_book.search('Flowers',9)
36
+ @con_book.books.first.magazine?.should be_true
37
+ end
38
+ end
39
+
40
+ describe "filters" do
41
+ it "should return results while using diffferent filters" do
42
+ @con_book.filter('Ruby Language', 1).should_not be_nil
43
+ @con_book.books.first.buy_link.should_not be_nil
44
+ end
45
+
46
+ it "should also show downloadable books for free_ebook filter" do
47
+ @con_book.filter('Ruby Language', 1).should_not be_nil
48
+ @con_book.books.first.download_link.should_not be_nil
49
+ end
50
+ end
51
+
52
+ describe "books" do
53
+ before(:all) do
54
+ @con_book.search('Flowers',2)
55
+ end
56
+ it "should create all the books instance" do
57
+ @con_book.books.should be_a_kind_of(Array)
58
+ @con_book.books.first.should be_a_kind_of(Object)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,32 @@
1
+ require_relative '../lib/google_book/base.rb'
2
+ require_relative '../lib/google_book/book_item.rb'
3
+ require_relative 'spec_helper.rb'
4
+
5
+ describe "book_item" do
6
+ let(:api_key){"AIzaSyD82ugogyUN7bAZ4k2UaLlVCep5NxZVYNM"}
7
+ before(:all) do
8
+ @con_book = GoogleBook::Book.new(:api_key => api_key)
9
+ @con_book.search("Flowers",2)
10
+ @book_items = @con_book.books
11
+ end
12
+
13
+ describe "initialize" do
14
+ it "should have same instance" do
15
+ @book_items.first.should be_a_kind_of(Object)
16
+ @book_items.first.item[:item]["id"].should eq(BookItem.new(item: @con_book.search("Flowers",2).first).item[:item]["id"])
17
+ end
18
+ end
19
+
20
+ describe "get_title" do
21
+ it "should return proper title" do
22
+ @book_items.first.get_title.should eq(@book_items.first.item[:item]["volumeInfo"]["title"])
23
+ end
24
+ end
25
+
26
+ describe "get_sub_title" do
27
+ it "should return proper subtitle" do
28
+ @book_items.first.get_sub_title.should eq(@book_items.first.item[:item]["volumeInfo"]["subtitle"])
29
+ end
30
+ end
31
+
32
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_book
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Rajarshi Das
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Integrate the google book api into your app
14
+ email: rdasarminus@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - Gemfile
21
+ - Rakefile
22
+ - spec/base_spec.rb
23
+ - spec/book_item_spec.rb
24
+ - lib/google_book.rb
25
+ - lib/google_book/base.rb
26
+ - lib/google_book/book_info.rb
27
+ - lib/google_book/book_item.rb
28
+ homepage: https://github.com/rajcybage/google_book
29
+ licenses: []
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.0.3
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Google Book API integration
51
+ test_files: []