Integrating External API Ruby on Rails

Integrating Zomato API step by step process.

Image of an API

When I was a Software Engineering Bootcamp Student, I had a hard time incorporating external APIS to my Rails App. It required me to read the documentation, get the API key, and what not! Oh well, that's every engineer’s life. Without wasting time lets get started:

Objective: Send a request to fetch all the cuisines associated with the specific city.

  • Request: receives a city name string, and API key
  • Response: returns city info, and a list of cuisines in that city

Things we need:

  • create a rails API app $ rails new my_api --api
  • Zomato API key generate an API key from here and save it in a safe place!

Gems we need:

gem ‘rest-client’, ‘~> 2.1’ 
gem ‘dotenv

Set-up: Assuming, that we have generated an API key from Zomato, and have bundle installed our rails app to install all the dependencies,

  • Create a .env file. This .env file must be created in the root folder, not inside any other folders, for example, our Gemfile is located in the root folder.
  • Save the API key which we have generated from the Zomato API inside the .env file.
  • .env file should look like this: API_KEY=abc22223333575757someapikey
  • Hide the API key which is also known as the secret key, by typing.env inside the .gitignore file. Do Not Push anything to GitHub until you do this step.

Awesome, Now we are going to create a Cuisines Controller. Where we are going to send an HTTP GET request to the /cuisines end-point. Your controller should like this:

require 'dotenv'
require 'json'
Dotenv.load
class CuisinesController < ApplicationController def cuisines
endend ## end of class

After we have created our Cuisines Controller and wrote a cuisines method inside, we are ready to make calls to the endpoints!

  • Line 8: In order to get cuisines associated with a particular city, we need to send our first HTTP GET request to the cities endpoint using first RestClient, to get all the cities which are saved in a variable called @response.
@response = RestClient.get "https://developers.zomato.com/api/v2.1/cities?q=#{params["city"]}",    {content_type: :json, accept: :json, "user-key": ENV["API_KEY"]}// ENV["API_KEY"] => loading your secret key from the .env file, that is why we have require "dotenv" 
// Read about Query Params
  • Line 11: From there we are indexing into the first city and saving the first city to @city_info. Note we are doing JSON.parse to our response so that we can convert string to a JSON object.
  • Line13–15: If the @city_info we are getting is true, meaning it exists, then we send another HTTP GET request to the cuisines endpoint, sending the city_id along with it in the query params.
  • Line 17: After we have received the cuisines, we will add a key called cuisines whose value will be all the cuisines to our city_info object and render it on Line 19
  • Line 21: Handling error so that if the name of the city we entered does not exist it should render a message which will indicate the error.

Fire up your server and see the results:

  • on your terminal, type rails s
  • Using Postman: send a GET request to http://localhost:3000/cuisines
  • Where the Query Params will be:
  • Key: city
  • Value: cityname (eg. Houston)

If not using Postman, simply type : http://localhost:3000/cuisinescity=houston in your web browser.

  • In place of Houston, you may type any city name.

Here is an example of the response I am getting:

"id": 277,
"name": "Houston, TX",
"country_id": 216,
"country_name": "United States",
"country_flag_url": "https://b.zmtcdn.com/images/countries/flags/country_216.png",
"should_experiment_with": 0,
"has_go_out_tab": 0,
"discovery_enabled": 1,
"has_new_ad_format": 0,
"is_state": 0,
"state_id": 111,
"state_name": "Texas",
"state_code": "TX",
"cuisines": [
{
"cuisine": {
"cuisine_id": 1035,
"cuisine_name": "Afghan"
}
},
{
"cuisine": {
"cuisine_id": 152,
"cuisine_name": "African"
}
},
{
"cuisine": {
"cuisine_id": 1,
"cuisine_name": "American"
}

Awesome Great work folks!

Here are some links, you might need to check out:

Coach @FlatironSchool

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store