Skip to content

Commit

Permalink
Improve enhancer sad path
Browse files Browse the repository at this point in the history
  • Loading branch information
jonallured committed Dec 22, 2024
1 parent 9517425 commit 5c24668
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
7 changes: 5 additions & 2 deletions app/models/book/enhancer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions spec/models/book/enhancer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand Down

0 comments on commit 5c24668

Please sign in to comment.