Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Wednesday 20 March 2013

XML example Yahoo Weather

This example is more or less a direct translation of the vanilla processing example to ruby-processing (the main difference is how easy it to create a formatted string in ruby cf java)...
NB: requires current version of ruby-processing-2+
#
# Loading XML Data
# by Daniel Shiffman.  
#
# This example demonstrates how to use loadXML()
# to retrieve data from an XML document via a URI
#

attr_reader :zip, :weather, :temperature

def setup
  size(600, 360)
  @zip = 10003
  font = create_font("Merriweather-Light.ttf", 28)
  text_font(font)

  # The URL for the XML document
  url = "http://xml.weather.yahoo.com/forecastrss?p=#{zip}"

  # Load the XML document
  xml = loadXML(url)

  # Grab the element we want
  forecast = xml.get_child("channel").get_child("item").get_child("yweather:forecast")

  # Get the attributes we want
  @temperature = forecast.get_int("high")
  @weather = forecast.get_string("text")
end

def draw
  background(255)
  fill(0)
  # Display all the stuff we want to display
  text("Zip code: #{zip}", width*0.15, height*0.33)
  text("Today's high: #{temperature}", width*0.15, height*0.5)
  text("Forecast: #{weather}", width*0.15, height*0.66)
end

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2