public
Created

Weather information for Xmobar

  • Download Gist
weather.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
# encoding: utf-8
 
require 'json'
require 'net/http'
 
def styled_temp(temp)
if temp < 15.0
"<fc=#AAC0FF>#{temp}</fc>"
elsif temp < 20.0
"<fc=#FFFF00>#{temp}</fc>"
else
"<fc=#FF0000>#{temp}</fc>"
end
end
 
begin
uri = URI('http://weather.yahooapis.com/forecastjson?w=638242&u=c')
info = JSON.parse(Net::HTTP.get(uri))
 
temp_today = styled_temp(info["condition"]["temperature"])
cond_today = info["condition"]["text"].downcase
temp_next = styled_temp(info["forecast"].first["high_temperature"])
cond_next = info["forecast"].first["condition"].downcase
temp_tomorrow = styled_temp(info["forecast"].last["high_temperature"])
cond_tomorrow = info["forecast"].last["condition"].downcase
 
puts "#{cond_today} #{temp_today} (f: #{temp_next}, #{cond_tomorrow} #{temp_tomorrow})"
rescue
puts "N/A"
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.