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.

Things we need:

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,

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!

@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

Fire up your server and see the results:

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

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:

Software Engineer Apprentice at IBM