Skip to content

Instantly share code, notes, and snippets.

@coderforhire
Created May 11, 2012 19:38
Show Gist options
  • Save coderforhire/2661970 to your computer and use it in GitHub Desktop.
Save coderforhire/2661970 to your computer and use it in GitHub Desktop.
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
~
~
@tomciopp
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment