How To Pull Book Covers From the Open Library API Into Your Rails App

My goal was to display covers via Open Library rather than forcing users to upload a cover from their desktop. The API makes it easy to pull covers by ISBN, but I don’t store that and don’t want to force users to look it up, so we decided to let users put in a title, which would then be run in a search on Open Library. 

Ideally, Open Library would return 8-10 book covers and the user would select the correct one, but this would require a bit of Javascript for the interaction. Instead, to get the API up and running, we just had Coverlist use the first cover in the search results (if there are no results, it will pull nothing and the cover will be blank). 

Yes, this is a temporary solution.

We also used HTTParty to parse the API.

– Add HTTParty to Gemfile

– Add a few lines of code to Create action in controller – make a variable for search results, then parse, then assign new variable  for cover_url that refers to the Open Library book ID url where the cover is stored (they prefer you link back to them rather than downloading and saving their covers). 

– Run a migration – had to add cover_url to books (ended up doing it manually)

– Change views to show cover_url instead of cover

Here is the full experience in Google+ Hangout

After we finished this lesson, the solution worked locally but not on heroku. 

I forgot to do heroku run rake db:migrate and heroku restart. Then it worked.

Then I needed to do an If/Else statement so users could override the Open Library cover with their own cover, in case the app pulled the wrong one. If there is a manually uploaded cover, that is shown – if not, the cover_url cover is shown. 

Right now it only interacts with the API on create, not update (although this could be easy to change) so if you put in Harry Potter for title, it might be a toss-up which book of the series you get. Thus, a few improvements should be made to this API integration, but the exciting part is that Coverlist is using its first API. And it happened in one hour. Much easier than I expected. Bigger things to come!