class GoogleBook::Book

Attributes

api_key[RW]
books[RW]
items[RW]
total_count[RW]

Public Class Methods

new(api_key) click to toggle source
# File lib/google_book/base.rb, line 14
def initialize(api_key)
  unless api_key.nil?
    @api_key = api_key[:api_key]
  else
    raise "Api key can not be nil. Please given your api_key"
  end
end

Public Instance Methods

book_info() click to toggle source

get all the books information

# File lib/google_book/base.rb, line 45
def book_info
      BookInfo.new(items: @items)
end
checking_filter_type(type) click to toggle source
# File lib/google_book/base.rb, line 58
def checking_filter_type(type)
  if type.nil?
    puts "How you want to filter your search?(example:1)?"
    puts "1)Free ebooks"
    puts "2)Paid ebboks"
    puts "3)Full text"
    f_type = gets
  else
    type = "1"
  end
end
checking_type(type) click to toggle source
# File lib/google_book/base.rb, line 85
def checking_type(type)
  if type.nil?
    puts "How you want to search?input only list number(ex:1)\n"
    puts "1)By Title\n"
    puts "2)By Author Name\n"
    puts "3)By Publisher\n"
    puts "4)By Subject\n"
    puts "5)By ISBN number:- \n"
    puts "6)Library of Congress Control Number\n"
    puts "7)By Online Computer Library Center number\n"
    puts "8)Want to search Downloadable book"
    puts "9)Want to search downloadable Magazines"
    type = gets
  else
    type = "1"
  end
end
connect_google(key = nil,type = nil,search_param = nil,filter = nil) click to toggle source
# File lib/google_book/base.rb, line 155
def connect_google(key = nil,type = nil,search_param = nil,filter = nil)
  uri = url_formation(key,type,search_param,filter)
  uri=URI::Uri.new(uri)
  #      response = Net::HTTP.get_response(uri)
  return uri.response.body
end
filter(query, type = nil) click to toggle source

Google books filtration

# File lib/google_book/base.rb, line 36
def filter(query, type = nil)
  checking_filter_type(type)
  filter_type = set_filter_type(type) unless type.nil?
  json = JSON.parse(connect_google(@api_key,nil,query,filter_type))
  @total_count = json["totalItems"]
  @items = json["items"]
end
set_filter_type(type) click to toggle source
# File lib/google_book/base.rb, line 71
def set_filter_type(type)
  case type.to_i
  when 1
    type = "free-ebooks"
  when 2
    type = "paid-ebooks"
  when 3
    type = "full"
  else
    type = "ebooks"
  end
end
set_normal_url(api_key, type, search_param, main_url) click to toggle source
# File lib/google_book/base.rb, line 143
def set_normal_url(api_key, type, search_param, main_url)
  case type
  when "download"
    url = main_url+"?q=#{search_param.gsub(/\s+/, "+").strip}&download=epub"
  when "magazine"
    url = main_url+"?q=#{search_param.gsub(/\s+/, "+").strip}&printType=magazines"
  else
    url = main_url+"?q=#{search_param.gsub(/\s+/, "").strip}+#{type}"
  end
  return url + "&maxResults=40"
end
set_type(type) click to toggle source
# File lib/google_book/base.rb, line 103
def set_type(type)
  case type.to_i
  when 1
    type = nil
  when 2
    type = "inauthor"
  when 3
    type = "inpublisher"
  when 4
    type = "subject"
  when 5
    type = "isbn"
  when 6
    type = "lccn"
  when 7
    type = "oclc"
  when 8
    type = "download"
  when 9
    type = "magazine"
  else
    type =  "inauthor"
  end
  return type
end
url_formation(api_key = nil,type = nil,search_param = nil, filter = nil) click to toggle source
# File lib/google_book/base.rb, line 129
def url_formation(api_key = nil,type = nil,search_param = nil, filter = nil)
  main_url = "https://www.googleapis.com/books/v1/volumes"
  if !type.nil?
    url = set_normal_url(api_key, type, search_param, main_url)
  elsif type.nil? && !filter.nil?
    url = main_url+"?q=#{search_param.gsub(/\s+/, "+").strip}&filter=#{filter}"
  else
    url = main_url+"?q=#{search_param.gsub(/\s+/, "+").strip}"
  end
  puts "#{url}"
  url = url + "&maxResults=40"
  return URI(url)
end