Flatiron's first project tasked me with creating a CLI app in Ruby. The app had to fetch data from an outside source, use proper object orientation, and let the user interact with the command line to retrieve specific data. I love space. So I chose to use the Open Solar System API as my data source, and I built my application to display different kinds of data about all the planets and moons in our solar system based on what the user wants to see.
There were two major challenges I had to overcome when building this project. The first was using the API and retrieving the data I desired. The second was organizing and instantiating my objects. Once I had these two steps done, the next steps were a little more straightforward - getting user input and displaying object attributes in a user friendly form.
My API class was designed to initialize with the API url I was using. I then created methods using the gems 'httparty' and 'json' to parse through the json objects I was getting to return an array of hashes (and some with nested hashes). After that, I parsed through each hash, each containing keys and values pertaining to a celestial object, and instantiated new planet and moon objects. I had to determine whether the hash was a planet or a moon, then create a new planet or moon instance, passing through the hash of data for that specific object.
API retrieval/creating new objects in action:
I decided to make two separate, but related classes. One for planets and one for moons. The API class would automatically pass in each planet and moon hash to its corresponding class. Each class uses meta-programming to initialize an object and create attribute accessors for each key in the hash, and set those attributes equal to the values. This meant I didn't have to manually set every attribute for the new objects. I knew that each planet object should have many moons. That meant that in the moon class, I had to iterate through all the planet objects to find which planet that specific moon instance belongs to. In doing this, I set up a has-many relationship where a planet has-many moons, and each moon knows about the planet instance it belongs to.
Meta-programming/Mass Assignment in action:
That was the heavy lifting. The rest was straightforward - displaying information, getting a user input, displaying more information, etc.. In the end, a user could enter any planet they wanted to know more about, and then decide to get a list of its moons, or get its mass, density, and many other details. When a user decided to list a planet's moons, they would see every moon that a planet had in its orbit, along with an option to enter a moon they wanted to know more about. If so, the user could find out details about any moon.
After completing this project, I feel very confident with accessing data from an API and object orientated programming and design. I am currently learning how to turn my app into a Ruby gem so anyone can use it easily.
If you want to check out the project on github, see this link: https://github.com/nicklevenson/solar-system-cli
Top comments (0)