My first project at Flatiron School
With 2020 finally behind us, I think it is time to start looking on to bigger brighter things. That is exactly how 2021 started for me and my first project at Flatiron School. My first project is a command-line interface using and parsing an API to get usable data in Ruby. I chose a topic I hold dear to my heart, which is local breweries. Every since 2016 when they became popular in NJ I have gone to loads of breweries and even some opening day celebrations for them. It brings my family closer together and its also a great way to support a local businesses. It was only fitting I use Open Brewery as my API of choice.
The API I chose looks like this:
Yeah, I know, intimidating… but let me help you look at this with some rose colored glasses. If you look at the first line we see this ‘[{“id”…’ this shows us right off the bat that when parsed, we already have code that Ruby can work with. The API is in the form of an array of hashes and our good friends RESTClient (Representational State Transfer) and JSON(Java-Script Object Notation) are here to help!
The Open Brewery API is formatted as string and we are able to use a JSON gem to help turn it into objects for us. Below is my API class and it will show us exactly how I parsed this data to make it in a format that our Ruby language can work with:
class APIdef self.get_dataresponse = RestClient.get('https://api.openbrewerydb.org/breweries?by_state=new_jersey&per_page=50')#binding.prybreweries_array = JSON.parse(response)breweries_array.each do |brewery|Brewery.new(brewery["name"], brewery["city"], brewery["street"])endendend
After this code runs we now have an array of objects that look like this:
Ah, much better! I don’t know about you, but reading this makes my eyes hurt much less! So now that we have this array of objects we can iterate over the objects and grab any and all information we would like. My project involves choosing a brewery based on the town and then displaying the address of the brewery. So my variables of interest were: name, street, and city.
The following code shows us how to iterate over the object to select the city and set that to a variable we can use.
def sorted_citycities = Brewery.all.uniq{|brewery| brewery.city}sorted = cities.sort_by{|brewery| brewery.city}end
The above code takes the array from our Brewery class and searches all the objects within it to find all the cities and sets it equal to the ‘cities’ variable. When doing this though I saw an issue of some towns having more than one brewery. So to fix this we need to use the ‘uniq’ function and only take unique city names. The ‘sorted’ variable lists the new array of cities in alphabetical order.
This worked beautifully and did exactly what I wanted it to do… until I realized I was not able to return the all the breweries from the town with that method. I needed another step… I needed to iterate over the Brewery class again and make it grab all breweries that are in the town that was chosen. The code looks like this
def display_breweriesBrewery.town_breweries(@selected_city.city).each.with_index(1) do |brewery, i|puts "#{i}. #{brewery.name}".cyanend
The code above uses an instance variable of @selected_city, that was set in a previous method, to only grab the city that was chosen from the list and allows us to grab the Brewery object that is attached to that city and then iterate over that to grab the name of the brewery. This was a big issue I needed some help with from a classmate. I struggled this trying to figure out how to list all the breweries from a town for nearly two days. A fresh pair of eyes is always a nice way to improve your code and work through problems.
Touching on this topic, I actually spent a good chunk of time helping others with their projects. It is in my nature to be helpful, but it wasn’t completely selfless. Seeing other people’s errors and helping them work through them really helps me solidify my knowledge and helps me be able to talk about code with a little more confidence. One error I had to help someone with I actually had in my code without even knowing so I was grateful for that.
In the end I was able to make a CLI I am very proud of. I chose a topic that I really enjoy and I am so proud of being able to build a program from scratch. Two months ago I never could have imagined doing that.