Skip to content

Instantly share code, notes, and snippets.

@diraulo
Created February 27, 2018 14:55
Show Gist options
  • Save diraulo/f2deed3fef57f0bd82ceb4a8a56564f3 to your computer and use it in GitHub Desktop.
Save diraulo/f2deed3fef57f0bd82ceb4a8a56564f3 to your computer and use it in GitHub Desktop.
Open Weather API Ruby Demo
# Example using the net/http module of ruby - Remember to set your api_key in an .env file
require 'awesome_print'
require 'dotenv/load'
require 'json'
require 'net/http'
require 'pry-byebug'
api_key = ENV['OPENWEATHER_API_KEY']
city = 'Pretoria, ZA'
# GET Request
url = "http://api.openweathermap.org/data/2.5/weather?q=#{city}&appid=#{api_key}"
uri = URI(url)
response = Net::HTTP.get(uri)
json = JSON.parse(response)
puts "Current weather information in #{city}"
puts "Description: #{json['weather'].first['description']}"
puts "Temperature: #{json['main']['temp']}"
# This demo uses the open-weather ruby client
require 'awesome_print'
require 'dotenv/load'
require 'open_weather'
require 'pry-byebug'
api_key = ENV['OPENWEATHER_API_KEY']
city = 'Stockholm, SV'
options = { APPID: api_key }
json = OpenWeather::Current.city(city, options)
puts "Current weather information in #{city}"
puts "Description: #{json['weather'].first['description']}"
puts "Temperature: #{json['main']['temp']}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment