Class: WeatherReportForecasts

Inherits:
Array
  • Object
show all
Includes:
WeatherReportLocation
Defined in:
lib/weather_report.rb

Overview

A collection of forecasts for a particular location

Defined Under Namespace

Classes: WeatherReportForecast

Constant Summary

Constants included from WeatherReportLocation

WeatherReportLocation::TITLE

Instance Attribute Summary collapse

Attributes included from WeatherReportLocation

#country, #latitude, #longtitude, #name

Instance Method Summary collapse

Methods included from WeatherReportLocation

#loadLocation

Constructor Details

#initialize(document) ⇒ WeatherReportForecasts

Constructs the weather forecasts from an XML string or file containing the XML in BBC Backstage weather format



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/weather_report.rb', line 135

def initialize(document)
  doc = REXML::Document.new document
  @reading_date = DateTime.now
  loadLocation(doc)
  doc.elements.each("rss/channel/image/url") { |element| @image_url = element.text }
  
  doc.elements.each("rss/channel/item/") { |element| 
    self << WeatherReportForecast.new(element)
  }
  
  raise WeatherReport::WeatherReportFormatError if length == 0
  
  rescue
    raise WeatherReport::WeatherReportFormatError
end

Instance Attribute Details

#image_urlObject (readonly)

Returns the value of attribute image_url.



132
133
134
# File 'lib/weather_report.rb', line 132

def image_url
  @image_url
end

#reading_dateObject (readonly)

Returns the value of attribute reading_date.



132
133
134
# File 'lib/weather_report.rb', line 132

def reading_date
  @reading_date
end

Instance Method Details

#for(date = Date.today) ⇒ Object

Returns a forecast for a day given by a Date, DateTime, Time, or a string that can be parsed to a date. If there is no forecast for the given date then nil is returned.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/weather_report.rb', line 163

def for(date = Date.today)
  date = case date.class.name
         when 'String'
           Date.parse(date)
         when 'Date'
           date
         when 'DateTime'
           Date.new(date.year, date.month, date.day)
         when 'Time'
           Date.new(date.year, date.month, date.day)
         end
  
  day = nil
  self.each do |fd|
    day = fd if date == fd.date
  end
  return day
end

#for_todayObject

Returns the forecast for today, or nil is there is no such forecast.



152
153
154
# File 'lib/weather_report.rb', line 152

def for_today
  self.for(Date.today)
end

#for_tomorrowObject

Returns the forecast for tomorrow, or nil is there is no such forecast.



157
158
159
# File 'lib/weather_report.rb', line 157

def for_tomorrow
  self.for(Date.today+1)
end