How to create a CLI using Ruby to get information from an API.

This is my tutorial for creating a command line interface with Ruby to access an API to retrieve specific data. It assumes that you are using Ruby to write your application and already have a GitHub account.

The specific application we will be building here uses the Apex Legends API to pull statistics on a player. We’ll be using object oriented programming to get the job done.

Lets Get Started.

Head over to your GitHub account and log in. make your way over to your repositories and hit the “New” button. Give your repository a name ,(something cool like “MegaAwesomeProject”) along with a description. Public or private is up to you. For this tutorial, were going to add a README file and Choose a license. (We’ll be using the MIT license here.)

Once you’ve got that all sorted out, we’ll be grabbing our repository’s SSH link to get our new file loaded into our text editor. I’ll be using VScode. I’ll also be using ubuntu to create my file. Inside of ubuntu, use cd (change directory) to navigate where you’ll be cloning your repository. once you’ve made it there type the following

git clone git@github.com:masterpkpk/MegaAwesomeProject.git

your Github link will obviously be different since you’ll be using your account.

Now that we are in the right folder, let’s open this puppy up. Type code . and hit enter to launch VScode.

Heck yeah. We made a new repository with a README and license, git cloned it to our folder, changed directories to access our project, then opened that project in VScode. That’s pretty awesome. I think it’s time to get down to business. (We call that business time.)

The File Structure.

Alright, alright alright. This is where we get to start having some fun. This project is going to need a few different folders, so we will go ahead and create those now

Go ahead and setup your folders/files to look like this. Once you do, we can start adding code and get this show on the road. Let’s just start with our environment folder and get all of our requirements going. In this project we use native gems so we wont have to use bundle.

Here we call on ‘require’ for our gems, and ‘require_relative’ for our local files. ‘net/http’, ‘open-url’, and ‘json’ are the gems that will make our API possible to use. Let’s take a look at the code that lets us do this.

The Code

Here we define our Url class along with our url_creator and parse methods. with this specific API, we need to include a platform and a username to access the information we’re looking for. To do this, I created a method that will take in two arguments and interpolate them into our API link. We then create a variable called ‘uri’ and parse the newly made link using URI.parse(). We take this variable and create a ‘response’ variable by passing it into NET::HTTP.get_response(uri). We can now access our players information via response.body! Hoorah! In order to use that data in our program in a way that allows us to access it, we need to parse it with JSON. our parse method will take our response.body and parse it using JSON.parse()

Next we need a class object that we can store all of the information we want inside of. We can call this the Player. Inside of our player class, we have attr_accessors for the Player’s name, platform, rank, kills, and legend. In our initialize method, we will create a new player object that has our five attributes. The data passed into our initialize method will be saved in our instance variables. (Created by place an @ sign in front of them) We then have our self.all method that allows us to call on the @@all class variable. This lets us see all of the Player objects we’ve created so far.

Our next class is where the meat and gravy happens. The CLI class will be where everything comes together to create the user experience. Here we see our start method. We will call this method when the program runs to get everything started. It contains methods we will be seeing here shortly. First it will greet the user and obtain their information. if the information is valid, it will continue to the main menu, If not, it will return back to the get_user_data method. Lets look at what these methods are doing.

There’s a lot going on here. We have a get_user_data method that calls our gets_platform and gets_username methods and set’s them to their own variable. The get_user_data method then takes those variables and uses them to create a new Url object which will create the correct API link. After the API is sorted out we pass that into our parse method, and then finally we can use our parsed data to create a new Player object. (Make sure you open your API in your browser to see where in the JSON hash your desired data will be located.) Once all of that goes down, the program will pass you onto the main_menu method which will print the menu and run our menu_options.

From here we enter a slew of If/else statements that will guide the user to the desired info. Using gets.chomp and setting the user’s input to a variable, we can easily navigate the menu. Once the user is done, they can enter 4 to exit.

The last thing to do is set up our run file. The first line is our shebang line. It will tell our program loader to run this file in a ruby environment. require_relative our environment folder and create a new CLI object and call the start method. Run it with ruby bin/run and you’re finished!

Just a dude that likes to code