Skip to content

Instantly share code, notes, and snippets.

@pimeys
Created October 21, 2012 08:55
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 pimeys/3926413 to your computer and use it in GitHub Desktop.
Save pimeys/3926413 to your computer and use it in GitHub Desktop.
Weather information for Xmobar
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment