Skip to content

Instantly share code, notes, and snippets.

@dmerrick
Created February 7, 2019 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmerrick/93eff5ca02082959cd6a4520e40a61e6 to your computer and use it in GitHub Desktop.
Save dmerrick/93eff5ca02082959cd6a4520e40a61e6 to your computer and use it in GitHub Desktop.
#!/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