Created
May 11, 2012 19:38
-
-
Save coderforhire/2661970 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| desc "Fetch weather" | |
| task :fetch_weather => :environment do | |
| require 'nokogiri' | |
| require 'open-uri' | |
| @weathers = Weather.all | |
| @weathers.each do |weather| | |
| url = "http://kitchen.screenfeed.com/weather/both/dJlC_W1LY0-DukSjsfJV1w.xml?location=#{weather.zip}&formatter=newground" | |
| doc = Nokogiri::XML::Reader(open(url)) | |
| doc.each do |node| | |
| puts node.value | |
| weather.update_attributes(:humidity => node.value) | |
| end | |
| end | |
| end | |
| ~ | |
| ~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code will allow you to loop through the document and either reference the name of the field or the content:
doc.xpath('//*').each do |n|
n.name // this will give you the name of the tag
n.content // this will give you the content of the tag
weather.update_attributes( n.name.to_sym => n.content) // if you want to update all the weather attributes it will be something like this
and then just close the loop with an end