class NationalWeather::Forecast
Public Class Methods
new(xml_string)
click to toggle source
# File lib/nationalweather/forecast.rb, line 8 def initialize(xml_string) @xml = REXML::Document.new xml_string @values = Hash.new end
Public Instance Methods
conditions()
click to toggle source
Returns all Conditions
objects for this forecast
MULTIPLE: <weather-conditions weather-summary=“Chance Rain Showers”>
<value coverage="chance" intensity="light" weather-type="rain showers" qualifier="none"/> <value coverage="patchy" intensity="none" additive="and" weather-type="fog" qualifier="none"/>
</weather-conditions>
EMPTY: <weather-conditions weather-summary=“Partly Sunny”/>
SINGLE: <weather-conditions weather-summary=“Chance Rain Showers”>
<value coverage="chance" intensity="light" weather-type="rain showers" qualifier="none"/>
</weather-conditions>
# File lib/nationalweather/forecast.rb, line 109 def conditions if !@values.has_key?('conditions') allConditions = Array.new @values['conditions'] = REXML::XPath.match(@xml, '/dwml/data[1]/parameters[1]/weather[1]/weather-conditions').map {|node| # handle weather-conditions with child values if node.has_elements? # Array to hold <value> attributes cValues = Array.new # gather the attributes of each <value> into a Hash node.get_elements("value").each do |v| atts = Hash.new v.attributes.each do |k, v| atts[k] = v.to_s end cValues.push(atts) end end allConditions.push(NationalWeather::Conditions.new(node.attributes["weather-summary"], cValues)) } @values['conditions'] = allConditions end @values['conditions'] end
day(index)
click to toggle source
# File lib/nationalweather/forecast.rb, line 13 def day(index) days[index] end
days()
click to toggle source
# File lib/nationalweather/forecast.rb, line 17 def days if !@values.has_key?('days') days = Array.new length.times do |i| d = NationalWeather::Day.new d.high = high_temperatures[i] d.low = low_temperatures[i] d.start_time = start_times[i] d.end_time = end_times[i] d.conditions = conditions[i] d.icon = icons[i] d.precipitation_probability_day = precipitation_probabilities[i*2] d.precipitation_probability_night = precipitation_probabilities[i*2+1] days.push(d) end @values['days'] = days end @values['days'] end
end_times()
click to toggle source
# File lib/nationalweather/forecast.rb, line 57 def end_times values('/dwml/data[1]/time-layout[@summarization="24hourly"][1]/end-valid-time', 'end_times') {|node| Time.parse(node) } end
hazards()
click to toggle source
Returns any Hazards (Watches, Warnings, and Advisories) for the forecast time period.
SINGLE:
<hazard hazardCode="LW.Y" phenomena="Lake Wind" significance="Advisory" hazardType="long duration"> <hazardTextURL>http://forecast.weather.gov/wwamap/wwatxtget.php?cwa=usa&wwa=Lake%20Wind%20Advisory</hazardTextURL> </hazard>
EMPTY:
<hazard-conditions xsi:nil="true"/>
# File lib/nationalweather/forecast.rb, line 74 def hazards if !@values.has_key?('hazards') @values['hazards'] = REXML::XPath.match(@xml, '/dwml/data[1]/parameters[1]/hazards[1]/hazard-conditions[1]/hazard').map {|node| # handle empty nodes like <hazards-conditions xsi:nil="true" /> if node.has_elements? code = node.attributes["hazardCode"] phenomena = node.attributes["phenomena"] significance = node.attributes["significance"] type = node.attributes["hazardType"] url = node.get_elements("hazardTextURL")[0].text NationalWeather::Hazard.new(code, phenomena, significance, type, url) else nil end } end @values['hazards'] end
high_temperatures()
click to toggle source
# File lib/nationalweather/forecast.rb, line 41 def high_temperatures values('/dwml/data[1]/parameters[1]/temperature[@type="maximum"][1]/value', 'high_temperatures') {|node| node.to_i } end
icons()
click to toggle source
# File lib/nationalweather/forecast.rb, line 61 def icons values('/dwml/data[1]/parameters[1]/conditions-icon[1]/icon-link', 'icons') end
length()
click to toggle source
# File lib/nationalweather/forecast.rb, line 37 def length start_times.length end
low_temperatures()
click to toggle source
# File lib/nationalweather/forecast.rb, line 45 def low_temperatures values('/dwml/data[1]/parameters[1]/temperature[@type="minimum"][1]/value', 'low_temperatures') {|node| node.to_i } end
precipitation_probabilities()
click to toggle source
# File lib/nationalweather/forecast.rb, line 49 def precipitation_probabilities values('/dwml/data[1]/parameters[1]/probability-of-precipitation[1]/value', 'precipitation_probabilities') {|node| node.to_i } end
start_times()
click to toggle source
# File lib/nationalweather/forecast.rb, line 53 def start_times values('/dwml/data[1]/time-layout[@summarization="24hourly"][1]/start-valid-time', 'start_times') {|node| Time.parse(node) } end
Private Instance Methods
values(xpath_string, key) { |text| ... }
click to toggle source
Returns an Array of values for the given XPath query. A block to format each value String can be included with the call to this method. If no block is given the values in the Array will be Strings. This method cached the result so it doesn’t need to query the XML and format the nodes each time it’s called
# File lib/nationalweather/forecast.rb, line 139 def values(xpath_string, key) if !@values.has_key?(key) @values[key] = REXML::XPath.match(@xml, xpath_string).map {|node| # handle empty nodes like <value xsi:nil="true" /> if node.has_text? if block_given? # format the String with the supplied block yield(node.text) else # default: return a String node.text end else nil end } end @values[key] end