Hey friends, hope all is well! Today I’m going go explain how to use the Fusion Yelp API key with Ruby! I thought it was difficult to use at first, but it becomes easier with practice. Now before we begin, let’s setup the 2 things we need to utilize the API.
- Postman or Postgres (I used Postman)
- The Fusion Yelp API key that you can get by signing up here
Cool, now that we’re ready let’s begin…
Now before we start to implicate our API key with Ruby, we have to test and see whether or not we can pull in some data through Postman. It was a rookie mistake for me to spend a ridiculous amount of time trying to figure out how to pull data out while I was trying to use Ruby. Postman will go ahead and do most of the work, we just have to give it the params we want to display. It will even show what the url will look like! Of course you would have to input an Authorization key (API key) that is provided by Yelp.
Once we have the necessary params and have input the correct authorization type (bearer token), we can go ahead and click send and see what kind of response we get from our reliable Yelp Fusion database.
Pretty amazing right?! We were able to get the information we wanted by utilizing a great tool called Postman. It gave us a way to dissect the Fusion Yelp database without much trouble compared to using Ruby and constantly putting rails c, rails db:reset into our terminal (less confusion this way, as well as less code to write). With that out of the way, we can start requesting and displaying our information through Ruby on Rails (forgot to mention I’m utilizing rails as a backend). In Ruby, I also downloaded “‘yelp-fusion’, require: ‘yelp/fusion’” so that I can use some built-in methods to pull the data into Ruby. It is a gem file; you can add it onto your gem file or explicitly type onto the terminal by bundle install.
Following best practice, we should start off by creating your model that will be containing the information from the database. In my case, the information I’m retrieving is going to contain data about restaurants around a location.
With this setup, we can now try to create some real data with Ruby! Start off by going into our seeds.rb file and start writing some methods! These methods will allow us to start pulling the information just like we did with Postman. It will also allow us to make the information ours to use!
You must be wondering what the method self.search means. Self refers to the model Restaurant we created earlier. In Ruby it is a reserved key that refers to an object. Search is the name of the method I’m using because this method will be searching for restaurants (naming convention). When we invoke this method by typing rails db:seed into our terminal, Ruby makes a request to the Yelp Fusion server. While the request is on-going, the computer has the ability to parse the response in order to make it human readable.
We’ve come a far way and we’re so close to having this information become ours. Our next step is to separate the instances so that we can call on an individual restaurant. In order to do that, we are still in our seeds file and we’re going to use dot notation (.each) to create our instances of restaurants. Remember earlier, I mentioned something about “businesses.” It’s very important! Our parsed response contains an object called businesses. To get any information, we must call on the method self.search and grab the object “businesses” with .each. As we’re calling onto each instance of businesses by calling it business, we will then use another method to create a single instance. In my models file, restaurant, I have a method called self.create_restaurant_data(business). This method is being called in our seeds.rb file. Each key value pair is going to be associated with the business’s attributes.
After completing these steps, you should be able to play around with your new Ruby data that was transferred over from Yelp! The Fusion Yelp database is versatile, so you are not limited to just restaurants, you can search for anything!
You can do whatever you want with the information you created with Ruby, you’re the boss! In my case, I used Mapbox with React to display my restaurants. I hope this will be helpful to aspiring coders and thank you for taking the time to read and learn. Have an awesome day!