Class: ApiHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/booksr/api_handler.rb

Constant Summary collapse

GOOGLE_MAX_RESULTS =

const

40

Instance Method Summary collapse

Instance Method Details

#search_by_google(query_string, query_type) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/booksr/api_handler.rb', line 6

def search_by_google(query_string, query_type)
	responses = Array.new
	parser = Parser.new

	# https://www.googleapis.com/books/v1/volumes?q=field:qeury_string&startIndex=#{start_index}&maxResults=#{GOOGLE_MAX_RESULTS}
	query_string = URI::escape(query_string)
	api_uri = "https://www.googleapis.com/books/v1/volumes"
	query = "?q="

	if query_type == :title
		query += "intitle:"
	elsif query_type == :author
		query += "inauthor:"
	elsif query_type == :isbn
		query += "isbn:"
	end

	query += query_string

	start_index = 0
	begin
		subquery = "&startIndex=#{start_index}&maxResults=#{GOOGLE_MAX_RESULTS}"

		response = RestClient.get api_uri + query + subquery
		responses.push(response)
		
		data = parser.parse_json(response)
		start_index += GOOGLE_MAX_RESULTS
	end while data.size == GOOGLE_MAX_RESULTS

	return responses
end