DEV Community

Aiden Marques
Aiden Marques

Posted on

My first CLI project

When the time came for me to finally start my CLI project, it was a bit daunting. Working on the labs for the Flatiron was difficult but having to create your own project from scratch, that's when your skills are finally put to the test.

My First Task

What API do I use?

I started with a grand idea. I wanted to create a CLI that provided medical information for transgender individuals.

I was very excited for the idea until I could not find any API that would give me the information I was looking for

After failing at my first idea, I decided to do some research first on the APIs I had available to me. So I decided to look through stockmarket API's. I liked the subject and a good number of API's appeared when I did my research. The issue I kept running into is all the API's I wanted to use had some sort of key. I played with the idea of using one of them even if the API key was necessary.

As I continued searching for API's I could not find a free Stock API that gave me the exact information I needed. There are a lot of API's out there but most of the information I wanted to take use would have forced me to pay monthly for the API use. So that's when I decided it was time for plan C.

I stumbled upon a github that listed many public Api's https://github.com/public-apis/public-apis#food--drink. That is where I found the API I use in my CLI program, openbrewerydb. This API returns a list of breweries.

[
  ...
  {
    id: 299,
    name: "Almanac Beer Company",
    brewery_type: "micro",
    street: "651B W Tower Ave",
    address_2: null,
    address_3: null,
    city: "Alameda",
    state: "California",
    county_province: null,
    postal_code: "94501-5047",
    country: "United States",
    longitude: "-122.306283180899",
    latitude: "37.7834497667258",
    phone: "4159326531",
    website_url: "http://almanacbeer.com",
    updated_at: "2018-08-23T23:24:11.758Z",
    created_at: "2018-08-23T23:24:11.758Z"
  },
]

Enter fullscreen mode Exit fullscreen mode

This was the perfect API, it had a lot of good information that when my CLI called it I could pull for the user. I was able to show different breweries, and different facts about it. Making this CLI interesting and informative for users.

Top Features

I love the functionality of my CLI and will definitely use it myself to find some great breweries. While building out this CLI two features became my favorite part of this program. Below, I talk a little bit to the features and how they elevate my CLI from just a basic Command-line Interface program.

TTY-Prompt

TTY-Prompt is an awesome feature that allows users to select an option using their arrow keys to move up and down the choices and are able to select a choice by pressing Enter.

Welcome User.

Do you want to see all Breweries? (Press ↑/↓ arrow to move and Enter to select)
‣ Yes
  BrewPub
  No

Enter fullscreen mode Exit fullscreen mode

I was able to create this feature using the tty-prompt gem and then updating my menu method so that it reflected that gem.

My menu method is broken down into two parts.

Part 1: Creates the functionality of being able to use your keys and enter to select your choice. This works by creating a new prompt, and then iterating through the different choices I created.

def menu

            prompt = TTY::Prompt.new
            choices = prompt.select("Do you want to see all Breweries?") do |menu|
                menu.choice 'Yes'
                menu.choice 'BrewPub'
                menu.choice 'No'
            end 
Enter fullscreen mode Exit fullscreen mode

Part 2: Gives a path for the choices created. The user is able to choose between 3 choices. Yes, No and BrewPub.

When the user chooses yes, the program prints "Good choice" and it displays the name of all the breweries available and gives the user the opportunity to choose the brewery they want to see more information on.

When the user chooses brewpubs, the program displays breweries that are restaurants. It does not display any other kind of brewery.

When the user chooses no, the program prints "Thank You. Hope to see you soon!" and exits the program.

            if choices == "Yes" 
                puts "Good Choice!"
                display_breweries
                user_choice
                menu

            elsif choices == "BrewPub" 
                display_brewpub_breweries
                user_input_type
                menu

            elsif choices == "No" 
                puts "Thank You. Hope to see you soon!"  
            end            
        end
Enter fullscreen mode Exit fullscreen mode

The TTY-Prompt gem fixed the problem if a user incorrectly typed a choice. I did not want to force the user to have to keep typing until they fully copied what was on the screen, so this gem really helped mitigate that issue by no longer requiring users to input their choice and instead just moving through the choices with their keys.

system "clear"

The other feature that I believe makes my program easier to use is the system clear feature. This feature clears my terminal as soon as I run the CLI program.

#!/usr/bin/env ruby

require_relative '../config/environment'

system "clear"
CLI.new.start
Enter fullscreen mode Exit fullscreen mode

I added the system "clear" line of code to my run file so that as soon as my file was run it clears the terminal for the user. Only displaying my CLI program.

It was an awesome experience putting this project together. I really enjoyed having to tinker with the code so that I could add more functionality and flair to my program. I hope this post helps you add some cool features to your programs as well.

If you would like to check out the code for my project you are welcome to do so. I hope you will try it out and enjoy it.

Top comments (0)