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

Things we need:

  • create a rails API app $ rails new my_api --api

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.

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.

Fire up your server and see the results:

  • on your terminal, type rails s

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