Created
February 27, 2018 14:55
-
-
Save diraulo/f2deed3fef57f0bd82ceb4a8a56564f3 to your computer and use it in GitHub Desktop.
Open Weather API Ruby Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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