|
| 1 | +require 'picrate' |
| 2 | +# Loading XML Data |
| 3 | +# by Martin Prout |
| 4 | +# |
| 5 | +# This example demonstrates how to use loadXML |
| 6 | +# to retrieve data from an XML document via a URL |
| 7 | +Weather = Struct.new(:location, :weather, :celsius, :fahrenheit) |
| 8 | + |
| 9 | +class NOAAWeather < Processing::App |
| 10 | + attr_reader :weather |
| 11 | + NOAA = 'KMIA' # NOAA Weather Miami Airport |
| 12 | + def setup |
| 13 | + sketch_title "NOAA's National Weather Service" |
| 14 | + # font = create_font('Times Roman', 26) |
| 15 | + # font = create_font('Merriweather-Light.ttf', 28) |
| 16 | + # text_font(font) |
| 17 | + # The URL for the XML document |
| 18 | + |
| 19 | + fo = "https://w1.weather.gov/xml/current_obs/display.php?stid=%s" |
| 20 | + url = format(fo, NOAA) |
| 21 | + # Load the XML document |
| 22 | + xml = loadXML(url) |
| 23 | + @weather = Weather.new( |
| 24 | + xml.get_child('location'), |
| 25 | + xml.get_child('weather'), |
| 26 | + xml.get_child('temp_f'), |
| 27 | + xml.get_child('temp_c') |
| 28 | + ) |
| 29 | + end |
| 30 | + |
| 31 | + def draw |
| 32 | + background(255) |
| 33 | + fill(0) |
| 34 | + # Display all the stuff we want to display |
| 35 | + text(format("Location: %s", weather.location.get_content), width * 0.15, height * 0.36) |
| 36 | + text(format("Temperature: %s Fahrenheit", weather.fahrenheit.get_content), width * 0.15, height * 0.5) |
| 37 | + text(format("Temperature: %s Celsius", weather.celsius.get_content), width * 0.15, height * 0.66) |
| 38 | + text(format('Weather: %s', weather.weather.get_content), width * 0.15, height * 0.82) |
| 39 | + no_loop |
| 40 | + end |
| 41 | + |
| 42 | + def settings |
| 43 | + size 600, 360 |
| 44 | + end |
| 45 | +end |
| 46 | + |
| 47 | +NOAAWeather.new |
0 commit comments