Pulling Out Data From Fusion Yelp

David Cha
5 min readMay 24, 2020

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.

Yelp Fusion will give you an API key after you sign up, which you can copy and paste onto the Authorization section in Postman. The type we’re working with is a Bearer Token. If you use the API key as a different type, it might not work the way you intended. Don’t worry, I have refreshed my API key numerous times. :)
Here we can see the params available to us provided by Yelp’s documentation. There is a lot lot more if you want to go ahead and do something else. Using Postman, the key values I used were term, location, radius, and open_now

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.

Right below the tabs, we can see that there is another tab that contains response. That response will display the information you requested with the params you have inputted earlier. It contains one object, businesses and inside businesses contains an array of objects. (Remember businesses, we’ll come back to it)
Also if you scroll down on your management page on Yelp Fusion, you will see a section that records your API usage. Do not fret, it will take many attempts to use all 5000 calls. Just beware they will bill you if you do happen to surpass that amount.

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.

These are the key value pairs that I would be using to associate the given information from the database. You are able to retrieve other types of information, however you see fit (you’re the boss of this operation!).

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!

Here, we have Ruby (left) and the terminal (right). In Ruby, the method search is basically the same thing we did in Postman, but this time we’re explicitly typing our request into the Fusion Yelp database. Once the request happens, we have to parse the information so that it can be human readable. A lot is going on here, but it’s a smooth transition! Lines 40 to 43 are meant to separate the instances.

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.

The information we requested from the Fusion Yelp database. We have it as human readable but we still need to separate the instances of all the restaurants we have. This way we can read the information better!

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.

Earlier I mentioned the key value pairs I used to associate the information I was given from the Fusion Yelp database. This right here, I’m making the information my own. :)

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!

This is my frontend, it is displaying all the restaurant’s instances but when I click on one of them, I see the information of a single instance.

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!

--

--

David Cha

Interested in the structure of codes and chasing the Dopamine rush as I start to understand their structures.