booksr 0.1.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 +7 -0
- data/lib/booksr/api_handler.rb +38 -0
- data/lib/booksr/book.rb +17 -0
- data/lib/booksr/parser.rb +33 -0
- data/lib/booksr.rb +31 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b2cdb3639defb32deae580bd104db190ea815ada
|
4
|
+
data.tar.gz: a60ceb1c3269a6edc0495f62f250b7ba1f3da4bf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 99d4b18b59e352efbd922e68697b9e260f1e5f4a86d6322dd5b550a5c384474b4089ec131aa42f647f22d0302bb50a145b8c3bebac184425cf912b3e2a7eac3e
|
7
|
+
data.tar.gz: a616d81e16d849087558a9794b8adc39ee96d01df726c7ccad37076ce4d8ae232f8f7220b494e27e6de06fa2a21e05c12f429b2c9b70941fba05a1f4f6478c0a
|
@@ -0,0 +1,38 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
class ApiHandler
|
4
|
+
GOOGLE_MAX_RESULTS = 40 # const
|
5
|
+
|
6
|
+
def search_by_google(query_string, query_type)
|
7
|
+
responses = Array.new
|
8
|
+
parser = Parser.new
|
9
|
+
|
10
|
+
# https://www.googleapis.com/books/v1/volumes?q=field:qeury_string&startIndex=#{start_index}&maxResults=#{GOOGLE_MAX_RESULTS}
|
11
|
+
query_string = URI::escape(query_string)
|
12
|
+
api_uri = "https://www.googleapis.com/books/v1/volumes"
|
13
|
+
query = "?q="
|
14
|
+
|
15
|
+
if query_type == :title
|
16
|
+
query += "intitle:"
|
17
|
+
elsif query_type == :author
|
18
|
+
query += "inauthor:"
|
19
|
+
elsif query_type == :isbn
|
20
|
+
query += "isbn:"
|
21
|
+
end
|
22
|
+
|
23
|
+
query += query_string
|
24
|
+
|
25
|
+
start_index = 0
|
26
|
+
begin
|
27
|
+
subquery = "&startIndex=#{start_index}&maxResults=#{GOOGLE_MAX_RESULTS}"
|
28
|
+
|
29
|
+
response = RestClient.get api_uri + query + subquery
|
30
|
+
responses.push(response)
|
31
|
+
|
32
|
+
data = parser.parse_json(response)
|
33
|
+
start_index += GOOGLE_MAX_RESULTS
|
34
|
+
end while data.size == GOOGLE_MAX_RESULTS
|
35
|
+
|
36
|
+
return responses
|
37
|
+
end
|
38
|
+
end
|
data/lib/booksr/book.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
class Book
|
2
|
+
attr_reader :title, :subtitle, :authors, :publisher, :published_date, :description
|
3
|
+
attr_reader :isbn10, :isbn13, :page_count, :lang
|
4
|
+
|
5
|
+
def initialize(info)
|
6
|
+
@title = info[:title]
|
7
|
+
@subtitle = info[:subtitle]
|
8
|
+
@authors = info[:authors]
|
9
|
+
@published_date = info[:published_date]
|
10
|
+
@publisher = info[:publisher]
|
11
|
+
@description = info[:description]
|
12
|
+
@page_count = info[:page_count]
|
13
|
+
@lang = info[:lang]
|
14
|
+
@isbn13 = info[:isbn13]
|
15
|
+
@isbn10 = info[:isbn10]
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class Parser
|
2
|
+
def parse_json(json)
|
3
|
+
data = JSON.parse(json)
|
4
|
+
volumes = data["items"]
|
5
|
+
return volumes
|
6
|
+
end
|
7
|
+
|
8
|
+
def parse_info_from_google(volume_info)
|
9
|
+
info = Hash.new
|
10
|
+
|
11
|
+
info[:title] = volume_info["title"]
|
12
|
+
info[:subtitle] = volume_info["subtitle"]
|
13
|
+
info[:authors] = volume_info["authors"]
|
14
|
+
info[:published_date] = volume_info["publishedDate"]
|
15
|
+
info[:publisher] = volume_info["publisher"]
|
16
|
+
info[:description] = volume_info["description"]
|
17
|
+
info[:page_count] = volume_info["pageCount"]
|
18
|
+
info[:lang] = volume_info["language"]
|
19
|
+
|
20
|
+
isbns = volume_info["industryIdentifiers"]
|
21
|
+
if !isbns.nil?
|
22
|
+
isbns.each do |isbn|
|
23
|
+
if isbn["type"] == "ISBN_13"
|
24
|
+
info[:isbn13] = isbn["identifier"]
|
25
|
+
elsif isbn["type"] == "ISBN_10"
|
26
|
+
info[:isbn10] = isbn["identifier"]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
return info
|
32
|
+
end
|
33
|
+
end
|
data/lib/booksr.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'rest-client'
|
3
|
+
|
4
|
+
require 'booksr/api_handler'
|
5
|
+
require 'booksr/parser'
|
6
|
+
require 'booksr/book'
|
7
|
+
|
8
|
+
class Booksr
|
9
|
+
def self.search(query_string, query_type = :keyword)
|
10
|
+
accepted_type = [:title, :author, :isbn, :keyword]
|
11
|
+
if !accepted_type.include?(query_type)
|
12
|
+
return nil
|
13
|
+
end
|
14
|
+
|
15
|
+
api = ApiHandler.new
|
16
|
+
parser = Parser.new
|
17
|
+
books = Array.new
|
18
|
+
|
19
|
+
responses = api.search_by_google(query_string, query_type)
|
20
|
+
responses.each do |response|
|
21
|
+
volumes = parser.parse_json(response)
|
22
|
+
|
23
|
+
volumes.each do |volume|
|
24
|
+
volume_info = volume["volumeInfo"]
|
25
|
+
books.push(Book.new(parser.parse_info_from_google(volume_info)))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
return books
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: booksr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- cjwind
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rest-client
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: test-unit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Search book with title, author, isbn or keyword by Google Book API.
|
56
|
+
email: cwentsai@gmail.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- lib/booksr.rb
|
62
|
+
- lib/booksr/api_handler.rb
|
63
|
+
- lib/booksr/book.rb
|
64
|
+
- lib/booksr/parser.rb
|
65
|
+
homepage: https://github.com/cjwind/booksr
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
metadata: {}
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 2.4.5
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: A simple book searcher
|
89
|
+
test_files: []
|