Phase 1 is coming to a close and we were tasked with our CLI final project. For our final project, we were entrusted with creating a CLI project from scratch by using an API or scraping.
When brainstorming what I wanted to create for my project I knew I wanted to do something anime-related. I wanted to create a CLI application that could pull along the lines by character or by anime title. I spoke to my cohort lead and she recommended that I started with an API rather than scraping.
Finding my API
Finding the perfect API was challenging. Many APIs were close to what I wanted but not quite. Some APIs were either too complicated or lacking in providing the kind of information that I needed. At first, I thought I found what I needed. I found the Jikan API, which was an open-source PHP & REST API for the “most active online anime + manga community and database.” It seemed like a great fit but when conducting further research about this API it seemed better suited for different languages like Javascript. This API was also too intimidating for me because there were so many features such as searching by anime, manga, season, user, club, history, etc. I decided against this API for this project but maybe I’ll tackle it for fun later.
Finally, I found the perfect API for me, the Studio Ghibli API. It was still anime-related by cataloged the people, places, and things found in the worlds of Ghibli but also it allowed for the kind of project I wanted to make. I wanted to create a project where I provided the user with different lists and they can pick what they want to learn more about. This API fit exactly what I wanted to do because it had many endpoints and huge arrays full of details about Studio Ghibli.
After finding the API I wanted to use it was time to start coding! Here are a couple of things I would like to talk about that I had to implement to reach my final project:
- Pushing Work to GitHub literally saved my life
- The holy grail that is Keyword Arguments and Mass Assignment
Pushing Work to GitHub literally saved my life
When I began my project, my cohort lead showed me how to upload all of my work to GitHub. She talked about how this not only showed the progress of the many steps of building my CLI but it would come in handy if my code ever broke. This problem did happen to me and I was racking my brain trying to get my code to what it was before. I must have deleted something important by accident because no matter what I was doing my code refused to work. Luckily I remembered that I had my previous version on GitHub and I retraced my steps from there. After grabbing my old code from GitHub my code worked again! Honestly, I am still not 100% sure what went wrong but I’m really glad that I was able to get my project back up and running. It would have been a disaster and a waste of a week if I wasn’t able to restore my project.
If you take anything from this blog is that you save all your hard work to GitHub and that you push frequently.
The holy grail that is Mass Assignment and Metaprogramming
In my project I wanted you to be able to search by either film or by a specific character. These different classes had different variables that it needed to pull from its array. From my Film class, I only wanted to pull title, description, release_date, original_title, and director. For my Person class, I only wanted to pull a name, gender, age, eye color, and hair color. I could have hard-coded each class but that would become very tedious. After speaking to my cohort lead about it she suggested metaprogramming and mass assignment.
Hard-coding is very tedious and very prone to breaking. At first my code looked like:
@@all = []
attr_accessor :name, :gender, :age, :eye_color, :hair_color
def initialize
@name = name
@gender = gender
@age = age
@eye_color = eye_color
@hair_color = hair_color
end
@@all << self
End
But through the key/value pair, made my code much more flexible.
def initialize(person_hash)
person_hash.each do |key, value|
self.send("#{key}=", value) if self.respond_to?("#{key}=")
end
save
end
By doing this, I did not have to write out every piece of information I wanted to pull from my API.
Thank you so much for taking your time for reading my blog, I hope you learned from my mistakes or found a better way of coding. If you want to see the final project please check out:
https://github.com/sumeikom/CLI_Final_Project
Thank you so much!
Top comments (0)