Integrating External API Ruby on Rails
By: Saima Rahman
Tags: Ruby, Json, Rails, Zomato Api, API

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:

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 G emfile is located in the root folder.

file. This . file must be created in the root folder, not inside any other folders, for example, our G is located in the root folder. Save the API key which we have generated from the Zomato API inside the .env file.

file. .env file should look like this: API_KEY=abc22223333575757someapikey

file should look like this: 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

end end ## 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"]}

// Read about // 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.

: From there we are indexing into the first city and saving the first city to . 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.

If the we are getting is true, meaning it exists, then we send another HTTP GET request to the cuisines endpoint, sending the 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

After we have received the cuisines, we will add a key called cuisines whose value will be all the cuisines to our object and render it on 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:

Awesome Great work folks!

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