diff --git a/6-task-queueing/app/jobs/lookup_book_details_job.rb b/6-task-queueing/app/jobs/lookup_book_details_job.rb index eaf03541..b897c1e9 100644 --- a/6-task-queueing/app/jobs/lookup_book_details_job.rb +++ b/6-task-queueing/app/jobs/lookup_book_details_job.rb @@ -20,17 +20,26 @@ class LookupBookDetailsJob < ActiveJob::Base queue_as :default def perform book - Rails.logger.info "Lookup details for book #{book.id} #{book.title.inspect}" + Rails.logger.info "[BookService] Lookup details for book" + + "#{book.id} #{book.title.inspect}" # Create Book API Client book_service = BooksAPI::BooksService.new - book_service.authorization = nil # Books API does not require authentication + # Books API does not require authentication + book_service.authorization = nil # Lookup a list of relevant books based on the provided book title. book_service.list_volumes book.title, order_by: "relevance" do |results, error| + # Error ocurred soft-failure if error - Rails.logger.error "[BookService] " + error - raise "BookService list_volumes ERROR!" + Rails.logger.error "[BookService] #{error.inspect}" + break + end + + # Book was not found + if results.total_items.zero? + Rails.logger.info "[BookService] #{book.title} was not found." + break end # List of relevant books @@ -57,14 +66,15 @@ def perform book publication_date = Date.parse publication_date book.author = info.authors.join(", ") unless book.author.present? - book.published_on = publication_date unless book.published_on.present? - book.description = info.description unless book.description.present? - book.image_url = images.try(:thumbnail) unless book.image_url.present? + book.published_on = publication_date unless book.published_on.present? + book.description = info.description unless book.description.present? + book.image_url = images.try(:thumbnail) unless book.image_url. + present? book.save end # [END update_book] - Rails.logger.info "(#{book.id}) Complete" + Rails.logger.info "[BookService] (#{book.id}) Complete" end end end diff --git a/6-task-queueing/spec/models/book_spec.rb b/6-task-queueing/spec/models/book_spec.rb index 61420d2d..a0da1335 100644 --- a/6-task-queueing/spec/models/book_spec.rb +++ b/6-task-queueing/spec/models/book_spec.rb @@ -58,39 +58,15 @@ def run_enqueued_job! job expect(job[:job]).to eq LookupBookDetailsJob expect(job[:args]).to eq [{ "_aj_globalid" => book.to_global_id.to_s }] - # Mock Books API RPC method - book_service = double - - # Mock response from call to Books API - book_response = double( - self_link: "https://link/to/book", - volume_info: double( - title: "A Tale of Two Cities", - authors: ["Charles Dickens"], - published_date: "1859", - description: "A Tale of Two Cities is a novel by Charles Dickens.", - image_links: double(thumbnail: "https://path/to/cover/image.png") - ) - ) - - allow(book_service).to receive(:authorization=) - expect(book_service).to receive(:list_volumes).with( - "A Tale of Two Cities", order_by: "relevance" - ).and_yield(double(items: [book_response]), nil) - - allow(Google::Apis::BooksV1::BooksService).to receive(:new). - and_return book_service - run_enqueued_jobs! expect(enqueued_jobs).to be_empty - book.reload + book = Book.find book.id expect(book.title).to eq "A Tale of Two Cities" expect(book.author).to eq "Charles Dickens" - expect(book.published_on.to_date).to eq Date.parse("1859-01-01") - expect(book.description).to eq "A Tale of Two Cities is a novel by Charles Dickens." - expect(book.image_url).to eq "https://path/to/cover/image.png" + expect(book.description).to include "Charles Dickens' classic novel" + expect(book.image_url).to eq "http://books.google.com/books/content?id=5EIPAAAAQAAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api" end it "book details are only looked up when fields are blank" diff --git a/7-compute-engine/app/jobs/lookup_book_details_job.rb b/7-compute-engine/app/jobs/lookup_book_details_job.rb index 617c7f66..82a54027 100644 --- a/7-compute-engine/app/jobs/lookup_book_details_job.rb +++ b/7-compute-engine/app/jobs/lookup_book_details_job.rb @@ -19,17 +19,26 @@ class LookupBookDetailsJob < ActiveJob::Base queue_as :default def perform book - Rails.logger.info "Lookup details for book #{book.id} #{book.title.inspect}" + Rails.logger.info "[BookService] Lookup details for book" + + "#{book.id} #{book.title.inspect}" # Create Book API Client book_service = BooksAPI::BooksService.new - book_service.authorization = nil # Books API does not require authentication + # Books API does not require authentication + book_service.authorization = nil # Lookup a list of relevant books based on the provided book title. book_service.list_volumes book.title, order_by: "relevance" do |results, error| + # Error ocurred soft-failure if error - Rails.logger.error "[BookService] " + error - raise "BookService list_volumes ERROR!" + Rails.logger.error "[BookService] #{error.inspect}" + break + end + + # Book was not found + if results.total_items.zero? + Rails.logger.info "[BookService] #{book.title} was not found." + break end # List of relevant books @@ -53,13 +62,14 @@ def perform book publication_date = Date.parse publication_date book.author = info.authors.join(", ") unless book.author.present? - book.published_on = publication_date unless book.published_on.present? - book.description = info.description unless book.description.present? - book.image_url = images.try(:thumbnail) unless book.image_url.present? + book.published_on = publication_date unless book.published_on.present? + book.description = info.description unless book.description.present? + book.image_url = images.try(:thumbnail) unless book.image_url. + present? book.save end - Rails.logger.info "(#{book.id}) Complete" + Rails.logger.info "[BookService] (#{book.id}) Complete" end end end