I'll be using OpenWeather API in the below examples.
Let's get started by creating a new rails app.
rails new weather-app
Install Faraday
Faraday is used to make HTTP requests to API
Add this line to the top of your application's Gemfile:
gem 'faraday'
Execute:
bundle
Generate Controller & View for making API requests
rails g controller search index
Now open /config/routes.rb
and replace get 'search/index'
with:
get '/search' => 'search#index', :as => 'search'
We'll be making API requests using this route and also display response on browser on the same route.
Open /app/views/search/index.html.erb
and fill it with:
<h2>Search for world weather by location</h2>
<%= form_with(url: search_path, method: "get", local: true) do %>
<%= label_tag(:location, "Enter Location name:") %>
<%= text_field_tag(:location) %>
<%= submit_tag("Search") %>
<% end %>
<% if @response %>
<div>
<%= @response.body %>
</div>
<% end %>
Which gives us a form to enter a location whose weather data we can pull from OpenWeather. And in if
statement we have @response
variable which is set to response from API if we made a call in the controller.
Create API service
Now let's actually get into the process of making API request to OpenWeather.
Although we have Faraday installed, it can't be directly called from within the controller.
Create a new folder named services
inside /app
. Make sure not to change the name of the folder.
Next add a ruby file in /app/services
, this file can be named anything you wish but the same name albeit capitalized should be used for the module inside it.
I'll create openweather.rb
inside /app/services
module Openweather
class Search
def self.by_location(location)
Faraday.get 'https://api.openweathermap.org/data/2.5/weather?q=' + location + '&appid=' + ENV['API_KEY']
end
end
end
The class inside the module and the method inside the class can be named anything.
From the code above, we can make out that - by calling Openweather::Search.by_location
method with a location
parameter would make an API call to OpenWeather, but what about ENV['API_KEY']
?
We sure can replace ENV['API_KEY']
with actual API key provided by OpenWeather, but leaving an API key within code is not right. So, we are going to place our API key as an Environment variable
.
First let's place API key as Environment variable in our local machine.
To do this we'll be using Dotenv gem
Install Dotenv
Add this line to the top of your application's Gemfile:
gem 'dotenv-rails', groups: [:development, :test]
And then execute:
bundle
Now add a file named .env
to the root of your application. Make sure not to change the name of the file. Add your API key in the first line like so:
API_KEY=your_api_key
Replace your_api_key
with your actual key provided by OpenWeather.
If your going to commit your work add .env
file to .gitignore
so that it is not tracked by git.
Call API service from the controller
Now for the final part, add this to index
action of your controller /app/controllers/search_controller.rb
if params['location']
@response = Openweather::Search.by_location(params['location'])
end
Make sure match the names of module, class and function that you named earlier.
Check the result
Now, open the server
rails server
And visit http://localhost:3000/search
from your browser.
Search for a location to see the response.
Top comments (1)
Nice tutorial, thanks