From 5c2466832c0d2ea305bdc9d6f712bd88840a13b2 Mon Sep 17 00:00:00 2001 From: Jon Allured Date: Sun, 22 Dec 2024 12:53:20 -0700 Subject: [PATCH] Improve enhancer sad path --- app/models/book/enhancer.rb | 7 +++++-- spec/models/book/enhancer_spec.rb | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/models/book/enhancer.rb b/app/models/book/enhancer.rb index c387167..f270ab1 100644 --- a/app/models/book/enhancer.rb +++ b/app/models/book/enhancer.rb @@ -5,11 +5,14 @@ def update_from_api api_data = OpenLibraryApi.get_book(book.isbn) return unless api_data + isbns = api_data["isbn_13"] || [] + attrs = { - isbn: api_data["isbn_13"].first, + isbn: isbns.first, pages: api_data["number_of_pages"], title: api_data["title"] - } + }.compact + return unless attrs.any? book.update!(attrs) end diff --git a/spec/models/book/enhancer_spec.rb b/spec/models/book/enhancer_spec.rb index e8d5006..884d8df 100644 --- a/spec/models/book/enhancer_spec.rb +++ b/spec/models/book/enhancer_spec.rb @@ -25,6 +25,20 @@ end end + context "with empty api data" do + let(:api_data) { {} } + let(:isbn) { "123456789" } + + it "does nothing" do + expect(OpenLibraryApi).to receive(:get_book).and_return(api_data) + book.enhancer.update_from_api + + expect(book.isbn).to eq "123456789" + expect(book.pages).to eq nil + expect(book.title).to eq nil + end + end + context "with a book that has none for isbn" do let(:isbn) { "none" }