Learn An Easy Way to Implement Weather Forecasting in Rails

with metasyncro

V324ej2tsoqzubhlbxzc?cache=true

Problem and Solution for Adding Weather to a Rails App

You’re building a rails app that is required to display weather data at any point on planet earth. You want to display current conditions as well as the future forecast. 

Solution 

In this lesson, I will show you how to integrate the free tier of Forecast.io into rails. This code is taken directly from a popular weather forecasting app for used by sailors for long distance yacht voyages and it can do the following: 

  • Get Local Weather for set of GPS coordinates, ie 34.0000, -119.00000
  • Get weather data at a specific time, down to the minute. 
  • Provide a daily-overview style weather forecast for the entire week. 

My goal is to give you a solid backend for building a weather forecasting app. Once you have the backend API in place, you can integrate this with google maps or use geocoding to give your users localized weather data. The possibilities are endless. 

Prerequisites

  • Basic Knowledge of Ruby on Rails
  • Basic Knowledge of HTML/CSS and Javascript
  • Rails 4+ 

Initial Configuration with Forecast.io API

First, you'll need a free Forecast.io account to access the API. Head over to https://developer.forecast.io/ and get your API key. We'll use the figaro gem to keep our API key hidden if our codebase is ever public, such as a GitHub repo.

Gemfile

gem 'forecast_io'
gem 'figaro'

then run bundle and figaro install

config/application.yml

forecast_io_key: 'YOUR_API_KEY'

config/initializers/forecast_io.rb

require 'forecast_io'  

ForecastIO.configure do |config|    
  config.api_key = Figaro.env.forecast_io_key  
end

Lastly, let's generate a scaffold that will have latitude and longitude attributes that we can use to query the Forecast API.

$ rails g scaffold Forecast lat:float lng:float

Getting the Current Weather for a Specific Location

This Section is Locked!

Unlock this lesson for free to view all sections.

Showing Weather Data in Rails View

Convert?crop=0,0,1182,486
With just a few lines of code, we are able to display up-to-the-second weather data in the view.

Building a Weekly Weather Forecast

This Section is Locked!

Unlock this lesson for free to view all sections.

Weekly Weather Forecast Data in the Rails View

T48u9xzutaabmbnd4lro
The frontend style is ugly, but you get the idea. The high and low temperatures are shown for each day of the week along with the summary. You can do this for any point on the globe with Forecast.io! Pretty awesome.
Signup and Unlock for free

Grades

b

Graded

A couple of typos: forecasts_controller.rb should have get_weather_data, not get_weather, and show.html.erb should use @current_weather, not @current_conditions. But these issues forced me to learn the code more in depth.

Av 2 4cf64c0e1b3355a4a2080590bb0e1ed92560cfa2d71950969066908e15a100d8

Graded

Was the file controllers/forecasts_contontroller.rb supposed to be created after doing the figaro install because I added mine manually... as aresult my rake would not work lol. any advice?

Av 5 33c4722c06ce9ccb9242211757e5456af19c87eda574d9eba5843d97e8a62838
a

Graded

Simple. Just work.

Av 6 35ca29fbea030fa2382540a06151508af22e7ca546b320764dd1caa63c096f8b
n

Graded

This doesn't seem to work, though I may be missing something basic as I'm fairly new. I keep getting method errors.

Av 4 683c35d46801fc76e24da362c8e2a708ca71bb39a8d7aa6e7212f93b16fc4a34
a

Graded

This is a solid lesson. I had no idea that getting weather would be this easy. Pretty basic implementation by the possibilities are endless. Looking forward to more...

avatar image
a

Graded

Super simple and easy to follow code. My only criticism is that I hope we see some more expansion on this feature, liking choosing by city rather than GPS coordinates. Google Maps would be sweet! Thanks.

Av 1 6de6c68998209aa73ceb4952e20f35de3e0e321b80400da723550f17f6175432
a 97.0%
Technology Code

  • 133 Unlocks
  • 1582 Total Reads
  • 24 minutes Est. Learning Time
Top Rated
Top Seller