Class: Google::Books::API
- Inherits:
-
Object
- Object
- Google::Books::API
- Defined in:
- lib/bookle/google_books_api.rb
Constant Summary collapse
- GOOGLE_OPTIONAL_PARAMETERS =
{ 'start_index_at' => 'startIndex', # starts at 0 (default) 'maximum_results' => 'maxResults' # [0..40], default=10 }
- GOOGLE_QUERY_KEYWORDS =
Prefixed some of the keywords with ‘in_’ to convey the Google meaning that the value is found in the content. Google differentiates between in content and is content.
{ 'title' => 'intitle', 'author' => 'inauthor', 'publisher' => 'inpublisher', 'subject' => 'subject', # *in* subject 'isbn' => 'isbn', # ISBN 'lccn' => 'lccn', # Library of Congress Control Number 'oclc' => 'oclc' # Online Computer Library Center number }
Instance Attribute Summary collapse
-
#raw_response ⇒ Object
readonly
Returns the value of attribute raw_response.
-
#total_volumes ⇒ Object
readonly
Returns the value of attribute total_volumes.
-
#volume ⇒ Object
readonly
Returns the value of attribute volume.
-
#volumes ⇒ Object
readonly
Returns the value of attribute volumes.
Instance Method Summary collapse
-
#clear_search_options ⇒ Object
Clears the search criteria.
-
#initialize(google_books_api_key) ⇒ Google::Books::API
constructor
Instantiates the API for a given Google key.
-
#search ⇒ Array
– An array of volumes (or what the Google API calls “items”).
-
#search_accessors ⇒ Array
Returns a list of accessors that can be used to set the search criteria.
-
#update_cacert_file ⇒ String
– Either a successful message or an error explanation.
-
#uri_string ⇒ Object
Builds and also lets users query the URI string used to interact with the Google API.
Constructor Details
#initialize(google_books_api_key) ⇒ Google::Books::API
Instantiates the API for a given Google key.
38 39 40 41 42 |
# File 'lib/bookle/google_books_api.rb', line 38 def initialize(google_books_api_key) @google_books_api_key = google_books_api_key @total_volumes = 0 @volumes = [] end |
Instance Attribute Details
#raw_response ⇒ Object (readonly)
Returns the value of attribute raw_response.
30 31 32 |
# File 'lib/bookle/google_books_api.rb', line 30 def raw_response @raw_response end |
#total_volumes ⇒ Object (readonly)
Returns the value of attribute total_volumes.
30 31 32 |
# File 'lib/bookle/google_books_api.rb', line 30 def total_volumes @total_volumes end |
#volume ⇒ Object (readonly)
Returns the value of attribute volume.
30 31 32 |
# File 'lib/bookle/google_books_api.rb', line 30 def volume @volume end |
#volumes ⇒ Object (readonly)
Returns the value of attribute volumes.
30 31 32 |
# File 'lib/bookle/google_books_api.rb', line 30 def volumes @volumes end |
Instance Method Details
#clear_search_options ⇒ Object
Clears the search criteria.
52 53 54 55 56 57 58 |
# File 'lib/bookle/google_books_api.rb', line 52 def search_accessors.each do |method_name| __send__ "#{method_name}=".to_sym, nil end nil end |
#search ⇒ Array
Returns – An array of volumes (or what the Google API calls “items”).
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/bookle/google_books_api.rb', line 68 def search google_response = nil uri = URI(uri_string) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme == 'https' http.ca_file = cacert_path http.start {http.request_get("#{uri.path}?#{uri.query}") {|response| @raw_response = response.body}} items = Google::Books::Items.new(@raw_response) @total_volumes = items.total_items || 0 @volumes = items.items || [] @volume = volumes.first items end |
#search_accessors ⇒ Array
Returns a list of accessors that can be used to set the search criteria.
47 48 49 |
# File 'lib/bookle/google_books_api.rb', line 47 def search_accessors GOOGLE_QUERY_KEYWORDS.keys + GOOGLE_OPTIONAL_PARAMETERS.keys end |
#update_cacert_file ⇒ String
Returns – Either a successful message or an error explanation.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/bookle/google_books_api.rb', line 90 def update_cacert_file # cacert = certification authority certificates file_path = cacert_path Net::HTTP.start("curl.haxx.se") do |http| resp = http.get("/ca/cacert.pem") if resp.code == "200" File.delete("#{file_path}_old") if File.exists?("#{file_path}_old") File.rename(file_path, "#{file_path}_old") File.open(file_path, "w") {|file| file.write(resp.body)} puts "\n\nAn updated version of the file with the certfication authority certificates has been installed to" puts "#{cacert_path}\n\n" else puts "\n\nUnable to download a new version of the certification authority certificates.\n" end end rescue => e puts "\n\nErrors were encountered while updating the certification authority certificates file.\n" unless File.exists?(file_path) if File.exists?("#{file_path}_old") File.rename("#{file_path}_old", file_path) else puts "\nThe certification authority certificates file could not be found nor restored.\n" end end puts "\nError information:\n" puts e. end |
#uri_string ⇒ Object
Builds and also lets users query the URI string used to interact with the Google API.
61 62 63 |
# File 'lib/bookle/google_books_api.rb', line 61 def uri_string "https://www.googleapis.com/books/v1/volumes?key=#{@google_books_api_key}#{build_optional_parameters}&q=#{}" end |