Skip to content

Instantly share code, notes, and snippets.

@davefp
Created March 7, 2013 14:18
Show Gist options
  • Save davefp/5108340 to your computer and use it in GitHub Desktop.
Save davefp/5108340 to your computer and use it in GitHub Desktop.
Send to multiple weather widgets from a single job
require 'net/http'
require 'xmlsimple'
# Get a WOEID (Where On Earth ID)
# for your location from here:
# http://woeid.rosselliot.co.nz/
woe_ids = {"weather-ottawa" => 3369, "weather-toronto" => 123, "weather-montreal" => 456}
# Temerature format:
# 'c' for Celcius
# 'f' for Fahrenheit
format = 'c'
SCHEDULER.every '15m', :first_in => 0 do |job|
woe_ids.each do |widget_name, woe_id|
update_weather widget_name, woe_id
end
end
def update_weather(widget_name, woe_id)
http = Net::HTTP.new('weather.yahooapis.com')
response = http.request(Net::HTTP::Get.new("/forecastrss?w=#{woe_id}&u=#{format}"))
weather_data = XmlSimple.xml_in(response.body, { 'ForceArray' => false })['channel']['item']['condition']
send_event(widget_name, { :temp => "#{weather_data['temp']}°#{format.upcase}", :condition => weather_data['text'] })
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment