Skip to content

Instantly share code, notes, and snippets.

@dan-c-underwood
Last active November 21, 2015 00:33
Show Gist options
  • Save dan-c-underwood/67bf4a8cb8b8a3671793 to your computer and use it in GitHub Desktop.
Save dan-c-underwood/67bf4a8cb8b8a3671793 to your computer and use it in GitHub Desktop.
Alternate version (with Emoji) of the Today-Scripts weather script.
require "json"
require "net/http"
require "colorize"
def formatTemp(temp)
temp_i = temp.to_i
temp = temp + "°C"
case
when temp_i <= 0
temp.colorize(:blue)
when temp_i < 5
temp.colorize(:light_blue)
when temp_i < 10
temp.colorize(:cyan)
when temp_i < 15
temp.colorize(:light_cyan)
when temp_i < 20
temp.colorize(:yellow)
when temp_i < 25
temp.colorize(:light_yellow)
when temp_i < 30
temp.colorize(:light_red)
when temp_i >= 30
temp.colorize(:red)
else
temp
end
end
url = "http://api.openweathermap.org/data/2.5/weather?q=" + ARGV[0] + "," + ARGV[1] + "&units=metric"
resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body
map = JSON.parse(data)
weather = map['weather'][0]['main'].downcase
weatherDesc = map['weather'][0]['description'].capitalize
weatherId = map['weather'][0]['id'].to_i
weatherIcon = case weather
when "rain", "drizzle" then case weatherId
when 300..500 then "💦"
when 501..511 then "💧"
when 520..531 then "☔️"
else weather.capitalize
end
when "thunderstorm" then "⚡️"
when "clouds", "atmosphere", "extreme" then case weatherId
when 701, 711, 721, 741 then "🌁"
when 731, 751 then "⏳"
when 762 then "🌋"
when 771, 781, 900, 902 then "🌀"
when 800, 904 then "☀️"
when 801..802 then "⛅️"
when 803..804 then "☁️"
when 901 then "⚡️"
when 903, 906 then "❄️"
when 904 then "☀️"
when 905 then "💨"
else weather.capitalize
end
when "snow" then weatherIcon = "❄️"
when "additional" then weatherIcon = "💨"
else weather.capitalize
end
puts "Weather".bold.colorize(:magenta) + ": #{weatherIcon} #{weatherDesc}"
puts ""
temp = formatTemp(map['main']['temp'].round.to_s)
low = formatTemp(map['main']['temp_min'].round.to_s)
high = formatTemp(map['main']['temp_max'].round.to_s)
puts "Temperature".bold.colorize(:yellow) + ": #{temp}, " +
"Low".bold.colorize(:blue) + ": #{low}, " + "High".bold.colorize(:red) +
": #{high}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment