| Class | WeatherReportForecasts::WeatherReportForecast |
| In: |
lib/weather_report.rb
|
| Parent: | Object |
| SUMMARY | = | /([\w -\/]+): ([\w -]+|N\/A|NA|\(none\)), Max Temp: (.*)/m |
| DESCRIPTION | = | /Max Temp: ([-\d\.]+|N\/A|NA|\(none\))(.+)Min Temp: ([-\d\.]+|N\/A|NA|\(none\))(.+)Wind Direction: ([\w -\/\(\)]*), Wind Speed: ([-\d\.]+|N\/A|NA|\(none\))mph, Visibility: ([\w -\/]+), Pressure: ([\d\.]+|N\/A|NA|\(none\))m([bB]), Humidity: ([\d\.]+|N\/A|NA|\(none\))(.*), (.+)/m |
| advance_days | [R] | |
| date | [R] | |
| description | [R] | |
| humidity | [R] | |
| max_temperature | [R] | |
| min_temperature | [R] | |
| pressure | [R] | |
| visibility | [R] | |
| wind_direction | [R] | |
| wind_speed | [R] |
Constructs the single day forecast from an REXML element containing the forecast in BBC Backstage weather format
# File lib/weather_report.rb, line 191 def initialize(item) item.elements.each("title[1]") { |element| md = SUMMARY.match(element.text) @description = md[2] @advance_days = day_diff(md[1]) raise(WeatherReport::WeatherReportFormatError, "WeatherReport Error: Day mismatch.") if @advance_days.nil? @date = Date.today+@advance_days } item.elements.each("description[1]") { |element| md = DESCRIPTION.match(element.text) @max_temperature = md[1].to_f @min_temperature = md[3].to_f @wind_direction = md[5] @wind_speed = md[6].to_f * 1.61 @visibility = md[7] @pressure = md[8].to_f @humidity = md[10].to_f } end
Calculate the number of days the given day name is from today‘s day name assuming it is no more than 5 days in the future or no further back than yesterday. Eg. If today is Wednesday, then if passed Tuesday this function will return -1, if passed Friday it will return 2, and if passed Wednesday it will return 0.
# File lib/weather_report.rb, line 216 def day_diff(day_from) start = Date::DAYNAMES.index(day_from.downcase.gsub!(/^[a-z]|\s+[a-z]/) { |a| a.upcase }) return if start.nil? finish = Date.today.wday result = start - finish result = result+7 if result < -1 result = result-7 if result >5 result end