DEV Community

Michael Thornton
Michael Thornton

Posted on

My First Program: Command Line with API Program

I was tasked with creating my first program recently. "I'm not sure if I'm ready for this", was my initial thought. But, with the help of my instructors I inched my way into it.

The first task was to just get the thing setup. I needed to create files that had previously been curated for me while doing labs. This was interesting as I was beginning to learn how my labs were made. After looking up a ton of API's that were either not enough information or you had to pay for a key to use I landed on an API that would return meals by categories or regions. This sounded easy enough.

Now I begin to code. I wanted my program to be easy to use and understand so I wrote down some categories and regions and went from there. Getting the information from the API was easy, getting that info to return values in my program was a different story. I started with just making a random meal option begin to work. First, I had to figure out how the API was presenting its information to me. After a few Pry sessions in my code I realized that the API was setup in a hash format. But, this is the tricky part. This hash was pointing to a value of an array with nested hashes in it. That's a ton of info but I only needed to get the values from a few of them. To do so I used the .each method to go over the API's array and return only the values associated the the information that I wanted.

Alt Text

One part of the things I wanted to display was a website that contained the ingredients and measurements for the meal. Unfortunately, in this day and age there actually isn't a website for every meal, at least not in this API. This to me was one of the biggest hurdles as I assumed that each meal would have this info. But the error "Bad Argument URI is neither a URI object or string" was telling me otherwise. So I went into the API and found out that some of the meals were pointing to a value of null or "", an empty string. I would actually use this to my advantage later.

Next, was to present this information somehow. I made another class that would be responsible for this and stored the info that the API was giving me here. Setup a couple of methods that would return the information for the meals that was selected from my main menu. The methods would clear out any previous meals and only get the ones that were created by the selection. This is where I made use of the blank spaces and created a valid method. If the object contained a blank space it would return a set message and if not would be left alone.

Alt Text

Alt Text

To get the input from the user I made another class that would be responsible to present all the menus take the selections and return the menus. At this point I have three classes all talking to each other, one to get selection information pass it to the meal presenter class and tell the meal class to grab the info from the API fetcher class and create the output. And, not only this but it would return an error menu if the input wasn't a real selection. This all sounds like a walk in the park but all of this took around four days of debugging to just get the info I wanted and present it the way I wanted.

Alt Text

Alt Text

Alt Text

Top comments (0)