Created
February 7, 2019 23:38
-
-
Save dmerrick/93eff5ca02082959cd6a4520e40a61e6 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
require 'pp' | |
require 'open-uri' | |
require 'json' | |
input = "61.210841,-149.888735\nSan Francisco\n33132" | |
places = input.split(/\n/) | |
results = {} | |
def is_coordinate?(string) | |
string.include?(',') && string !~ /[a-zA-Z]/ | |
end | |
def fetch_data_for(place) | |
json = '' | |
if is_coordinate?(place) | |
lat, lon = place.split(/,/) | |
url = "https://api.openweathermap.org/data/2.5/weather?lat=#{lat}&lon=#{lon}&appid=d7457467f2492af08b5aa255be8b2e2e" | |
json = open(url) {|f| f.read } | |
else | |
url = "https://api.openweathermap.org/data/2.5/weather?q=#{place}&appid=d7457467f2492af08b5aa255be8b2e2e" | |
json = open(url) {|f| f.read } | |
end | |
return JSON.parse(json) | |
end | |
places.each do |place| | |
parsed_json = fetch_data_for(place) | |
temp = parsed_json["main"]["temp"] | |
name = parsed_json["name"] | |
results[name] = temp | |
end | |
max = results.values.max | |
puts "highest temp is in: #{results.key(max)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment