public
Created

Send to multiple weather widgets from a single job

  • Download Gist
weather_multiple.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
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.