How I used an API to build a CLI app to compare conditions at Ski-Areas
Over the last two weeks, I have been hard at work building my first Command Line Interface (CLI) using Ruby! I enjoyed building my first CLI app, which I call “Freshies”, in reference to the coveted fresh snow sought after by skiers and snowboards around the globe. “Freshies” is used to check conditions at ten of the most popular Ski-Areas in Colorado.
I wanted to build an app with a bit of humor behind it so that when I got stuck in the weeds I could sit back and laugh about the whole thing. This proved helpful more than once.
My CLI app uses data generated from an Application Programming Interface(API) called One Call API, which is one of many APIs available to use for free at openweathermap.org. The One Call API takes in a few parameters, including the latitude and longitude of a specific location to generate a weather forecast for the specified location. The API also takes in a parameter to change which unit to display the data in (Imperial or Metric). Finally, the API requires a specific user ID, generated by openweathermap.org when you sign up, called an API Key. This is used so the server knows who is making the request.
In order to call the API, (enter the desired parameters, and send a “get-request” to their servers), a specific web address must be created in this format known as a URI, or Universal Resource Identifier:
Building my API class to generate all of the data for my app was one of the toughest parts of this build. Here’s how I did it.
I first define the initial URL for my custom URI as a constant called “INITIAL”. Then, I define all the locations for the Ski-Areas in a hash, where each key is a Ski-Area and its value is an array containing the latitude and longitude for that location and set it equal to the constant “LOCATIONS”.
When a new instance of my API class is instantiated, it is initialized by passing each of the defined locations, along with its corresponding latitude and longitude, through the instance method #api_call_for. The latitude and longitude are immediately passed into another method which I built called #uri_for, which formats the URI using the “INITIAL” url, and a hash of URI parameters, which I defined as a local variable called “parameters”.
The kicker is this line: uri.query = URI.encode_www_form(parameters). This calls on the query method of the URI module which is built into Ruby, to take in my parameters and spit out a completed URI in the correct format to be received by the openweathermap.org servers called “encoded form”.
Since the return value of my #uri_for method is a custom-built URI, my #api_call_for method can now make a call to the API using another built-in Ruby class, Net::HTTP, and one of its methods called #get_response, which takes in our custom URI as an argument and set it equal to a local variable called “response”.
I then use yet another built-in Ruby module called JSON and one of its methods called #parse, which takes the response and parses it into an organized set of nested hashes. This includes all the current weather data, as well as the seven-day forecast data.
Now that we have our data sorted into a readable format, I use the meta-programming technique called mass-assignment to create and read individual objects for things like temperature, humidity, conditions, wind speed for every day of the forecast.
Phew!! That’s a lot of information to process! But thanks to the miracle of modern computing, my “Freshies” app does all of this ten times in just a few seconds, as soon as the “Freshies” app is opened. This allows the user to view the current conditions and seven-day forecast for any of the ten ski-resorts at the click of a button, and help them decide which ski-area has the best snow!
Overall, this was a fun project to build, I learned a ton about the process of creating a CLI app and how to use an API. I also learned that I'm really starting to enjoy the process of coding.