Library Of Mo

Muhammad Ullah
6 min readApr 18, 2021

Well…I have finally finished my first CLI project for Flatiron School. After two failed attempts of creating a superhero and comic book project, I had decided to create a library. One of my goals is to have a full library in my home because it’s not just a room full of books and information, it is a room full of different lives you can live and experience. Since that is a goal that will take some time to achieve, I will settle for a virtual library.

The API

My project is extracting information from the New York Times Best Sellers Categories List API which gives a list of categories in an array of hashes and in each has there is a book array which contains a hash of the top 5 best seller books that falls under that category.

The Code

My code is fairly simple since this is only my first project after all. It goes 2 levels deep which brings you to a list of categories and the user selects their favorite category and it puts out 5 books with the author, publisher, description(if it was in the API), highest rank reached, and how many weeks it was on the list.

In order for my code to work it first needed the ability to GET the user’s input, so I defined a method user_input and for a solid 2 minutes, I was trying to rembember what is that we use to GET the user’s input and it poped into to my head to use gets with a .strip to trim any leading and trailing white lines.

The code starts off with a start method that asks the user to enter their name and then following that would be to invoke the class method API.get_data which takes the information from the API and instantiates new instances. Once the user puts in their name and the class method, it then calls on the greetings method which greets the user and instruction to see a the categories list or to exit using a puts statement which the calls on the menu method.

Menu method

The menu, although it is nothing new or extravagant, it still intrigued me. Using ‘if’ and ‘else’ statements and depending on what the user inputs will decide what method it will call on next. If the user types in ‘yes’ it will call on the category_list method which will print out all of the categories. Or if the user types ‘exit’, it will then have a goodby message and exits out. And if they type in anything else it will call on the invalid method which is a puts statment to “enter a valid entry” then calls on the menu method, so the user has to pick one of those two before moving on.

The category_list method invokes the class method Library.all(contains a class variable thats is set equal to an array of all the keys and values that I needed to initialize) and we need to iterate through the array using .each.with_index to retrieve the the name of the category and its index to be put ot a puts statements containg those objects which then gives a list of numbered categories, and then calls on the select_category method. This is where the user enters the category of their choosing and once they hit enter the class method Library.find_by_category gets invoked as the chosen category becomes the argument to check where the category that was entered exists. If what they enters was ‘exit’, it invokes the leaving_library method, and if what was inputted is not ‘exit’ or any of the categories, then it invokes the invalid method then the select_category method, so they can enter a valid entry. But if the user inputs a category and it checks out then it invokes the books_list(cat_name) method with the chosen category as the argument.

The books_list method was a bit confusing for me at first because the information I needed was in an array of hashes that was inside an array of hashes, but luckily with the help of pry and my genius of a cohort leader pointed out that when we passed the chosen category as an argument we were then inside inside of the hash and all I needed to do was iterate through the array and hashes the same way I was able to do with the categories by using .each. This method then puts out the list of book that falls under that category with the author, publisher, description(if given), highest rank reached, and how many weeks it was on the list.

The Most Challenging Part

As soon as I thought I was done, I ran into an annoying problem. When the books_list(cat_name) method got invoked to put out the information of the books, the titles of the books came out all uppercased because that is how it is in the API. Now the code worked perfectly don’t get me wrong, and I thought I could just leave it alone, so I decided to save and turn off my laptop and go to sleep, but I could not sleep. The though of the title being all uppercased was gnawing at my brain, and this is my code no matter how simple it is, it is still my code and I must take care of it. I decided to strap on my boots and get back to work. I kept thinking to myself, because that is what I do best, how can i take the title of the books and capitalized them. At first I thought I could downcase the the titles then call on the string class method ‘capitalize’ on the titles, but that would only capitailize the first word, and some of the books have titles more than two words. I was stumped, but after taking a walk, I realized I had done something similar in previous labs. I would have to split the titles into multiple strings, then use .map to itterate each string in the array to be capitalized then finally using .join to for back into one string with the whole title being capitalized. Everything I just mentioned is being the done in a method called capitalize_title(title) which gets invoked in the books_list with the title of the book being passed as the argument.

It’s not much, but it’s honest work.

Lessons Learned

Through out this project I’ve learned a great deal about coding, and Iwould like share it in my ending

  1. The importance of design.
  2. The importance of classes having a single responsibility and their methods being alligned with that responsibility.
  3. ‘PRY’ is a powerful tool
  4. A life lesson also is to take your time and take one step after the other. Taking you time each step of the way will relieve you of a lot of headaches

Well…This is the end of my blog talking about my first CLI project that I’m damn proud not because it is perfect which it is not, but because it is something I had created. Hope you enjoyed reading as much as i enjoyed writing this… and the code.

--

--