My first app ever happened to be a CLI program that displays live weather for a certain city. Pretty useful if you ask me. I built it using Ruby along with a couple of gems(Json, HTTParty, and Pry). For the data I used an API that when sent a GET request would return a 5 day forecast for a specific city. Aside from this being my first app, this would also be my first time using an API so I was a bit intimidated stepping into this unknown realm.
First time checking the data returned by the URL endpoint caused major confusion. It was in a format I had never seen before and had no idea how to parse through it. Also, how would I validate user input? So that invalid commands didn’t completely break my code. Both of these were topics that I did not understand very well at very well in the beginning of my project but were necessary for completion. This post will be discussing how I overcame these minor obstacles that at the time felt so big. With the purpose to help other beginner developers tackle their first project using API and spend less time debugging.
Fetching the Weather
API’s are a way in which big companies offer their data, or functionality, to the public so that it can be used by other developers in their own applications. A good metaphor to understand API’s is to think of them as the waiter in a restaurant that takes the request for the food, goes to the kitchen, and returns with the food requested. Where your app is the customer, the kitchen is the API endpoint URL where the information is coming from, and the food is the data.
To set up an API request from an application there are a few steps we must take:
- The first step is to convert the endpoint URL(the location) to a URI(the way to categorize that address ex. ‘http:’ or ‘mailto:’) Using Ruby’s built in URI module.
- Second step is to get the data from the endpoint. This is where the HTTParty gem comes in handy because it allows us to call the #get on our URI. Which will return a headache.
3. Next we need to convert this data into something we can actually use. Thats when json gem comes to save the day. We use the #parse method from JSON module which parses through that string and returns a good old Ruby hash that we are all familiar with.
In Ruby these steps look like this:
uri = URI(url)#2nd step
response = Net::HTTP.get(uri)#3rd step
Validating Search Input
After making the data ‘workable’, I began my CLI class which will be responsible for outputting the user receiving and validating commands.
user_input = gets.stripif user_input.to_i.between?(1, Weather.all.size) forecast_display(Weather.all[user_input.to_i-1]) sleep(5)else puts "\n\nWait for Prompt(=>)" puts "That is not a valid command. Try again bucko\n\n" sleep(3) display_optionsend
The above did not require much time. However, validating the users search input required a bit more thinking. My CLI class will need a method that checks that the city entered was not already in the list. Then check the API response and check if the city actually existed. Whenever an incomplete city name was entered, the API would return a list of cities that matched the search input. For example, entering ‘san’ in the search would return the following:
Which will therefore add the incorrect city to the list(because I programmed it to return the first city). To prevent this from happening I made a helper class method. This method would check if the complete city name was entered or if an incomplete city was entered before making a new API request for the new city. The #downcase so that capitalizations wouldn’t be an issue.
@@all.any? do |weather|
weather.city.downcase == city.downcase ||
For non-existent cities the API would return an empty array. A simple #empty? and that would take care of any fake cities the user entered.
Programming languages alone allow for the creation of many programs but the functionality of those programs will be very limited. Incorporating API’s into our programs allows for many more possibilities. That huge nested hash may be intimidating at first but with the right tools we can make it something pleasurable and easy to work with. It is essential that every developer learns to work with all kinds of API’s, both data and functionality, and the earlier the better. As well as learning how to validate user input like passwords, email addresses… It may sound fairly simple and easy now but it took me a lot of trial and error to fix these little bugs. Ultimately, I enjoyed the process of working with API’s. I learned lots about API’s and programming in general while working on this project. I hope you did too.