My first coding project!
So far at flatiron we have learned about procedural and object oriented ruby. It was difficult to learn about the concepts at first, but one day it just clicked for me halfway through reading a lesson. I still have a lot to learn, but I am excited to say that I created my first project and it works!
Our first project was to create a CLI gem that had to fulfill some requirements: It must iterate an array and store the information as objects with attributes; It had to have control flow and conditions; and it had to reach one level into a hash of data from either scraping a website or using an API to pull data.
During a study group one of the instructors decided to make a CLI gem from scratch using the Star Wars API to get his information that he was going to use. That API is what gave me the idea to create my project.
Behold! The Star Wars Starships API!
In all seriousness, it was a fun project and learned some awesome techniques for retrieving data from an API especially when it spanned over multiple pages.
My project begins at the concept: I thought it would be fun to make a CLI project that could access starship data from the Star Wars API, which took data recorded from the first seven movies(as of this blog post), and make it easily display for someone who was interested in knowing more about the starships from the Star Wars universe.
So I set off to conceptualize the process of pulling information from the API and then displaying it successfully for the user and creating a control flow for the program so that the user could make a choice, access the menu, or exit the program at any time during the usage of my CLI project.
I initially got stuck when it came to iterating over the hash of data provided by the Star Wars API (https://swapi.dev/). I was first able to successfully iterate over only the first page of data that I wanted pulled. Being stuck and thinking of anyway I could possibly think off with my newfound knowledge of ruby, I went back to a study group recording of an instructor working with the Star Wars API where I learned of a beautiful trick to be able to iterate all of the pages I needed. Within the hash was a key “next” that referred the next page of data, which pointed to the value of said webpage. Using that hash key, I learned I could create a while loop that would not break unless that “next” hash key would no longer point to a value of a webpage. So, using that technique I was able to pull in all my data points and create an iteration to create objects of each starship and collect and designate attributes to each starship object. Once the “next” hash key no longer had a value, the loop would break out and the program would continue to work as intended. After testing out using the ‘pry’ gem, I was successfully pulling the data from the API and converting it to objects.
The next part of the project was fairly simple object orientation, which was just creating a class to create objects with a few attributes in the form of the starship data taken from the API. Those attributes would be the specific data I wanted to be shown to the user when they requested data on a particular starship. After testing it and seeing that my project was coming along great, I started the most amusing part of the project, which was the control flow and setting conditions for my the inputs I wanted to allow for the user in my program. The control flow is pretty simple for the project requirements, however I can imagine later down the road if I were to work on a much larger application control flow can make or break an app. When building the control flow of my project I created several methods to either run when a user input something, or when the program loads up, or when to ask for an input from a user. Once I got the control flow down, I got caught up in the condition statements for my the inputs I was allowing. I wanted the program to spit back an error to the user if they made an incorrect input. Once a user made an input, I had a Boolean method that would check to see if the input was valid. That method determined whether the input was between the first index of the array from the starship class variable to the last index(the array length). However my method to get the initial method was causing the app to not recognize the string ‘exit’ or ‘menu’. I had combined two steps of converting the users input to an integer and subtracting one from the input before checking to see if it was valid. After trying multiple different outputs I had to take advantage of using our instructor’s office hours to ask him for some help.
He solved my problem so quickly. I am thankful and also motivated to learn more and practice coding to be able to solve and issue that was ultimately so simple. Learning to sidestep an issue with such a simple solution is a key to learning how to think like a software developer. I corrected my issue by creating an additional variable in my conditional statements method. This allowed me to pass in the initial string and set that if it was equal to the string ‘exit’ it would exit the program; or if the input was equal to the string ‘menu’, it would take the user back to the menu list of objects that were stored. Otherwise, the input would get converted to an integer and used as the index of the starship object that was being retrieved via the user’s input. Here is a picture of my solution:
After solving that issue, my project was complete. It could pass the requirements of the project and I submitted it for review.
Overall, this was a fun project. I learned how to create a project from scratch, how to use the git gem and GitHub, and more importantly how to create multiple files and have the run together in one app using ‘require’ or ‘require-relative’ to get all of my separate class files to work coherently. This is one of many projects that I will be posting about as I continue through the software engineering boot camp through the Flatiron School. I am excited to learn more and further my coding skills to build up great projects and accel my learning and thinking processes. I am proud of myself for being able to stick through this first phase of the boot camp and overcome challenges in the project to be able to continue on. I look forward to the rest of the boot camp and what I will learn. As always, stay tuned for more of my journey!
The link to my project is listed here if anyone is interested in taking peek:
GitHub Repository: https://github.com/wizbeck21/CLI_swstarships
Demo Video (How it works): https://youtu.be/fzZqsmDkSOg
The API I used for my project, Star Wars API: https://swapi.dev/