Using an API to Make a simple CLI App

Bams J. Teague
5 min readJan 12, 2021

--

I can’t begin to tell you how exciting it is, as a student developer, to have created your first application and watch it run. You crafted that! We encounter a lot of frustration as developers, but when things are running smoothly after all our hard work…there’s no feeling like it.

Here, I’m going to share with you, using my personal CLI project as the example, how to get started setting up your own application.

You may be wondering, what exactly IS a CLI? What does that stand for? Well, CLI stands for Command Line Interface — this is a program run in your terminal!

CLI programs are built to accept input to carry out functions. For our purposes, the CLI application we’re building will be taking data from the Star Wars API (Application Programming Interface) to output information on the planets from the far, far away galaxy.

To get started, I used the command (gem) bundle gem <YOUR_PROJECT_NAME> once I was inside the directory where I wanted to save my file. This gem provides you with many of the files you will need to get started on your project!

  • A bin folder that has a console file already created
  • A lib folder which contains another folder starwars_cli(for our purposes)
  • This folder is where you will create the classes you need. We will have a API, CLI, and Planet class for this example.
  • This folder also contains a file starwars_cli.rb which will act as our environment file.
  • We are also provided a Gemfile, Rakefile, README and a .gemspec file.

Here, you can ignore the provided Gemfile and instead require any gems in your folder that is acting like an environment file.

To make sure our environment file and our Class files are run when our program is launched, we’re going to write in a shebang at the top of the file to let the program know to read in ruby, then require_relative our lib/starwars_cli folder that contains our environment and class files, and then instantiate a new instance of our CLI application. This will allow us to launch into our program through the terminal using the command:

ruby bin/executable

Now that you have that set up, let’s take a look at our environment file.

Here in our environment file, as I mentioned before, is where you are going to make sure you require_relative the files you need as well as require any installed gems. Since we have made sure that our executable file has access to this file, we have access to everything we need in our program!

Now, you’ll want to start building out your classes. In our API class, we’ll be creating a class method to collect data from an outside API, which will dictate the information the user receives as they run the CLI.

Here, we’ve set a class method self.get_data which sets a variable response equal to a .get call using rest client to pull data from the Star Wars API. We’ve then set up a variable planet_array to equal the JSON parsed data of the response’s body, specifically the value of the"results" key. Then, we iterate over the resulting array with the .each method and create new planet instances, ready to use.

Moving on from our API class, we’ll create another new file in our lib folder to hold our CLI class. This is the class that will interact with our user and give them access to our API’s data. How will we access the data we’ve just gotten in our API class within this CLI class, you may ask? Here’s how I brought the information into my project:

Here, in our call method to launch our app, we’ve greeted our user and given them some options to navigate our program. We’ve implemented the colorize gem (required in our environment file) to make our program extra interesting! After greeting our user is where we will pull the data from our API class into the CLI class. We call the class method get_data on the API class to give this call method access to the information. Then, we’ call up a menu that looks like this:

We’ve prepped the program to accept input downcased and use if/elsif/else logic to dictate what other methods to run. For instance, we have a planet_list method that prints out a numbered list of planets using our Planet class. If the User wants to leave, we have a goodbye method that will issue the user a exit message and quit the program. We also have an error message that will pop up with anything else is entered called invalid_entry. It’s important to call the menu method within the goodbye method in order to loop back in and give the user another chance to launch into or exit the program.

Our Planet class:

And there you have it! That’s the basic set up of a simple CLI application that pulls data for the user through an API data base!

The results of our efforts are going to look a little something like this…..

--

--

Bams J. Teague
Bams J. Teague

Written by Bams J. Teague

Full stack program engineer and graduate of Flatiron

No responses yet