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 restcountries.com:
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.
In order to parse the JSON formatted data and import it into my application, I used the following lines of code:
URL = "https://restcountries.com/v3/all" def get_country_data uri = URI.parse(URL) response = Net::HTTP.get_response(uri) response.body end def countries countries = JSON.parse(self.get_country_data) end
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.