DEV Community


Posted on

A Ruby CLI Application Showing the Distribution of Languages Across Countries

My goal was to build a CLI (Command Line Interface) application that utilized external data from a webpage, prompted a user to make choices, and then provided detailed information back to the user based on the choices which the user made.

I chose to build a Ruby CLI application that would allow a user to see information about all of the regions, countries, and national languages of the world. First I found an API (application programming interface), from the website

This API provided a very detailed list of all the countries in the world, the major languages used in those countries, the regions of the world those countries are located in, and some other important geographical information about those countries.

With this API, which contained detailed geographical information in JSON (JavaScript Object Notation) format, I was able to obtain the external data that my application could utilize to provide geographical information back to the user.

In order to parse the JSON formatted data and import it into my application, I used the following lines of code:

URL = ""

    def get_country_data
        uri = URI.parse(URL)
        response = Net::HTTP.get_response(uri)

    def countries
        countries = JSON.parse(self.get_country_data)
Enter fullscreen mode Exit fullscreen mode

With the relevant geographical data for each country now accessible to my program in the form of nested hashes, I was able to use that data to create Ruby objects that my application could manipulate in various ways.

I chose to focus on data about the names of countries, regions, and languages out of the data that had been made available to my application from the restcountries website.

Consequently, in my 'lib' folder, I created Country, Region, and Language classes, as well as another class to import the raw data, and crafted methods for each class that would allow me to display the geographical data in specific ways back to the user.

I also created a CLI class, where I stated the various options available to the user to obtain different kinds of information about countries and languages, and provided prompts for specific information to the user about what information they were looking for. In my CLI class I also defined additional methods to provide responses to information that the user might enter.

Finally, I created an executable file in the bin directory of the application to run the completed program, as well as an environment.rb file in the 'config' folder and a Gemfile and README file.

With this, my application was complete and I had a working Ruby CLI application that utilized external data.

Discussion (0)