This week I completed my first CLI project using ruby as tasked by Flatironschool software engineering program. The project requires us to build a CLI that provides access to data from the web, collect the data and save it into an object. We were also required to go at least one level deep. After wandering on the web for ideas and looking for good free API to collect the data from, I decided to build a simple Weather gem because Who doesn’t like a forecast update?
Well at least, that is the first thing I do before getting out, CHECK THE WEATHER!! Working with API for the first time was quite a challenge. Geez… the amount of time I spent on the web to teach myself.
Well, it Paid off!!
App Description
My app allows user to get a weather update by zip code. It provides a 24 hour forecast, detailing it every 3 hours. I used the OpenWeatherMap API to collect my data, which was returned in a JSON format. It was not difficult to work with JSON as I know how to operate on hashes or array with Ruby.
Project Outline / Step
Fetching data from the Web using OpenWeatherMap API
The app prompt the user to enter a zip code. Once a valid zip code is retrieved, the app used HTTParty gem to query the web and JSON parser to parse the response which will then be used to provides user requested data. The easiest way to learn how a gem works is by reading the README. It literally tells you how to use the gem. Well, you can always google everything…, But seriously, go to the source
Collecting Data into Objects
Next I used the skills learned during this first part of the program at Flatironschool to collect data into objects. The response from the API provided 40 data points enclosed in an array. The data points in the main array were either hashes or arrays. Those data points represent weather data every 3 hours for 5 days. I used iteration to collect the details I needed for my app, as well as the built in Ruby class Struct . A struct is useful in terms of producing value objects which store related attributes together.
Now I had an array of Forecast containing the details I need. Next, I decided to display forecast to user in a 24hrs period. So I had to divide my array into 5 arrays, each containing 8 elements (8 weather data) that would be display to user per request. I used the terminal-table gem to display the data.
Ouf! The hard work was done.
CLI Time
All I had to do at this point was to transfer the collected data into my CLI, and used some styling gem like Colorize, TTY or ruby_Figlet to improve the user experience. Upon execution, the app starts with a banner ‘Weather-Forecast’ that was obtained using the ruby_figlet gem. This is a useful and simple ruby gem for font interpretation and printing. You can check the installation and usage of any these useful gems on their github repository, checking the README file. This gem has many font I encourageto checkout for your CLI. I used the simple one shown bellow:
The code above also display how one can use the colorize gem.
Fnally, my last gem.. The one I discover last and I told myself I GOT TO USE THIS ONE! The TTY-PROMPT
We all know the drill of having to throw off a bunch of if/else statements to prevent the user from inputing a wrong entry for a selection menu. This becomes annoying when you have you have a long selection or plan to expand the options.
Well, I thank the creator of TTY-Prompt. This is a life savior. This gem improves you menu building capabilities. It not only refactors your code, but also makes it interactive.
Check the gem README page on github for more gem usage other than the menu selection.
I use the following code for my menu:
-
prompt = TTY::Prompt.new
-
@options = %w(Exit Check-Another-Area Next-Day-Forecast Next-2-Days-Forecast Next-3-Days-Forecast Next-4-Days-Forecast)
-
@selection = prompt.select(“What would you like to do next?”, @options)
After creating an instance of prompt, we use the select method on the prompt and passig it 2 arguments: the question to be asked and the list of options. and our result from the prompt:
Although I am happy with the end result, my project can be further refactored.
My TO DO List:
- Hide my API keys
- Possibly add some emoji and more style
- Revise any hard coding
Thanks for reading, until next time…
Let’s break in!