Can You API an Entire League?
After the insanity of the first week of November, with the presidential election putting the ENTIRE COUNTRY on edge, it would have been nice to have had a week to just decompress from everything happening. Unfortunately, the second week is project week, so it’s time to keep pushing! One of the more exciting pieces of news (that wasn’t related to politics) from early November was the news that the NBA had voted on a start date for their next season! December 22nd! I’m a massive basketball fan, so this news couldn’t have come at a better time. I found an API that listed all 30 NBA teams with some background information on each of them, so I decided to get to work.
My program presents you with your option of NBA team names or NBA team cities and gives you information on the name or city (whichever you decided not to search by), the division they play in, and the conference they play in with one easy-to-read sentence. The program then gives you the option to look at the team’s “rivals”, which in this case are all of the teams in their division, which they play 4 times a year instead of the normal 2 for any other team.
How to Set an API Class
To start my project, I had to set up an API class in order to allow my project to interact with my selected API. An API is a way that a company can make their data accessible for other programmers to use. The comparison I’ve seen used frequently is that an API is something like a menu at a restaurant. You have a list of dishes, some information about that dish, and you have somebody else do the work to get you that dish.
This is the initialize method in my API class. When an API is initialized, it’s a gigantic wall of information that’s incredibly difficult to read, so we need to go through some steps to make it a little more readable. First, the URI method prepares the URL to cooperate with the Ruby Gems that we have installed to help with the project.
After the URI is set, Net::HTTP allows our program to get information from the URL, and then parses through it with JSON, which turns all of the API’s information into a hash, taking our gigantic wall of unreadable content:
And turns it into this!
Much better!
Finding My Rivals
The part of my CLI that gave me my biggest headache was at the very end of my program. I wanted my program to take in the team a user wanted to look at, find the division that team plays in, and return the other teams in that division. Here’s the code that ended up working for me:
The user is given the option to run this code after the final output presenting a team’s full information is given. That instance (team_instance) is taken in as an argument for final_team_rivals. I then decided to iterate over Teams.all to look at every team hash given from the API, using .each to check every hash and return unmodified elements. Inside the block, I check if the specific instance of team’s name is equal to the team name used as an argument, and using “next” I’m able to skip over putting out that name at the end, since a team can’t be their own rival (you could make the argument that when a team is bad they’re their own rival, but that’s not important right now). After I successfully removed the user’s chosen team from the result, the block will puts out every team name for each instance where the iteration’s division name is equal to the argument team’s division. Successfully printing out a list of the four other teams listed in a division.
Conclusion
Since this was my first experience with really building out a full program from scratch, without an environment given to me through tests, it started out as an incredibly daunting task. APIs can be scary to confront, the wall of text that they give you at first glance can seem pretty menacing. After getting the ball rolling, this was a very fun challenge. Even with the problem solving and critical thinking skills the previous 3 weeks had given me in regards to coding, it’s fun to come out of this project with those skills developed even further. Even more than before, I’m looking forward to the next challenge!