Skip to content

Instantly share code, notes, and snippets.

@davefp
Created March 7, 2013 14:18

Revisions

  1. David Underwood created this gist Mar 7, 2013.
    25 changes: 25 additions & 0 deletions weather_multiple.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,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