DEV Community

Vikram Chatterjee
Vikram Chatterjee

Posted on

Phase One project - Joke App

I just finished first project for Flatiron School. This is a CLI gem that draws data from an API. I chose the Official Joke API which can be found at “https://github.com/15Dkatz/official_joke_api”.

The Official Joke Api is an API containing a list of around 400 jokes, including general jokes, programming jokes, and knock-knock jokes. It has endpoint URLS that allow a user to access a random joke, access a joke by type, or access ten random jokes or 10 jokes by type. In my project I decided to focus on the endpoints that give ten jokes.

I started by setting up an environment that requires “RestClient” to GET information from the end point URLs:

· pry (to look into the code for development purposes), and

· JSON (to parse the string information from the websites into objects).

After setting up my “runjokes” file to welcome the user to my app and starting a CLI instance, I began setting up my services:

· the Cli class

· the Api class, and

· my Model class Jokes

I started with the “Api class” and defined the base-url for the API endpoints. Then I created a method “self.load_ten_jokes” which employed “RestClient” and “JSON” to get a response from the "/random_ten" endpoint and parse the data from within into an object.

I iterated over each item in the data and created objects in the Jokes class, initializing each joke by assigning attribute accessors to the keys in each joke hash (id, type, setup, and punchline), and using “.send” to pass the values into their respective keys.

Then, I passed all 10 of those hashes into a class array “@@all”, which gave me something to represent in the CLI. I made a reader and a clear method for the “@@all” array and moved onto the CLI.

In the CLI, I created a main menu which initially asked the user to enter 1 to get 10 random jokes, and created a method to get user input. Upon getting the user input of 1, I called the Api function to get the jokes, and I iterated over the 10 joke objects in the array, listing them by their order in the array and revealing the setup of each joke.

Then, I called a “sub_menu” method that asked the user to select a joke in the list (1-10) that they'd like to hear the punchline to. I made sure that the input was valid and I assigned a variable 'joke' to the number that the user had selected and printed the punchline to that specific joke, which is one level deep into the list of joke objects which I had provided before.

After revealing a punchline, or telling the user that their input was invalid, I cleared the class array of jokes “@@all” so that 10 new random jokes could be populated into the array.

I later expanded my code to allow the user to get 10 random jokes by entering 1, or to select jokes by “Type” by entering 2. I updated the Api class to access endpoints that would yield up to 10 jokes from any of the three different types i.e. general, knock-knock, or programming.

I correspondingly created three methods in the CLI class which would load a different endpoint by passing in a “Type” argument into the “load_jokes” Api method. I gave the user the option to choose between these three types, and repeated the process of listing each joke and going into a sub-menu so that the user could select the punchline they would like to hear.

In the case of the knock-knock joke type, the API had only 5 jokes, so I had to create a dedicated sub-menu that would only accept inputs "1-5" as valid. After the user hears the punchline or enters an invalid input, the program starts over, and asks the user if they'd like to hear 10 random jokes or select jokes by “Type”.

As a finishing touch, I customized the get input method to better inform the user whether they should be choosing between “1 or 2”, or “3 to 5” to select joke type, and “1 to 5” for knock-knock jokes, or “1 to 10” for lists of ten jokes.

My 'Joke App' provides an example for a gem that fetches data from an API, provides a choice for user input at two levels, and provides a list of information that the user can pick from to get more specific details about any specific item on that list.

Top comments (0)