CLI - My First Own Project

Posted by Gina on October 20, 2018

Welcome to the newest episode of the GinaStruggles. First off, I will now apologize for any words or terms that I misuse. Using the right terminology and explaining myself correctly has been one of my biggest issues.

Now, on to this week’s task. Getting an idea of what to do came quickly. My CLI is there to show the user the newest book releases via a genre. Upon starting the interface the user can either input a genre or input ‘list’ to get a list of the genres that are covered in this CLI - which are easily expandable or interchangeable by the way. By inputting a genre, the user will get a list of the 20 newest books sorted by best sellers. The title is displayed and upon inputting the number the title is connected to, it will display more information such as the author and its price with the book format. Now I just had to get all that to magically work. When you look at your text editor and it is basically just an empty slate it is so rough. Its the same as you having to clean your room but it is such a big mess that you just have no idea where to begin. Even though it is not even remotely similar at all, it certainly feels the same. The helplessness is real. So after realizing that this is not even close to anything I have done before and my eyelid starting to twitch crazily, I took a deep breath and just started. I created all the files I needed. I required the environment file in the executable file, required every other file in the environment file. Installed all the gems that I needed and started to build my cli file. I began actual coding with the interface, put lots and lots of notes in and figured out from there what things needed to happen in the other files to get the output that I wanted. Once I had shelled out the interface, I started scraping. Honestly, the scraping part was one of my least issues. It was so straight forward. There was one little hickup just with how the website set up the objects but that got resolved fairly quickly. I created two class methods in the scraper file, one to scrape the genres and on to scrape all the books. Originally I planned on just assigning a name to the genre and having that in the Genre.all array. I did the same for the books. Unfortunately, that way, I ended up with two arrays that were completely seperated from each other and were not able to interact with each other. I was very stuck on the plan I had in mind, mainly somehow wanting to create a hash that would have the genre as its key and the matching books as its value. There was no alternative for me so I was almost desperately trying to make that happen. Luckily my 1:1 with Enoch was coming up and he kicked me into gear. With a little - ok maybe a lot - of help from him, I finally was able to see an alternative - and may I say, way better - way. In the back of my mind I knew that it was a possibility but again, I just wanted to do it my way. We ended up adding a url to our genre instances. In the books file I added an #add_book method and used that in my scraper method. I iterated through the Genre.all array and used the #add_book method to create many books in association with its genre. With these few methods I was able to get my cli to work the way I wanted it to. So back to the cli file. I created a while loop with different inputs in an if statement within that loop. If the input is ‘list’, it will iterate through the Genre.all array and put out a genre’s name. Alternatively, if the input matches any of the genres in its all array it will iterate through the many books and output the book title with its matching index number. Once that was done I created a new method called #book_info and called it right after the book title output. That method creates a new while loop for which the user input would be a number from 1 through 20. Instead of just returning the book title this one puts out the title, author and price. And that was it. I was done.

This challenge has been so much fun, got me to write code that I have been practicing writing this whole last month and it taught me to broaden my horizon and look at alternative ways to get to the goal. It definitely was a struggle but it was a struggle worth taking!