Class: WeatherReportForecasts
- Inherits:
-
Array
- Object
- Array
- WeatherReportForecasts
- 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
Instance Attribute Summary collapse
-
#image_url ⇒ Object
readonly
Returns the value of attribute image_url.
-
#reading_date ⇒ Object
readonly
Returns the value of attribute reading_date.
Attributes included from WeatherReportLocation
#country, #latitude, #longtitude, #name
Instance Method Summary collapse
-
#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.
-
#for_today ⇒ Object
Returns the forecast for today, or nil is there is no such forecast.
-
#for_tomorrow ⇒ Object
Returns the forecast for tomorrow, or nil is there is no such forecast.
-
#initialize(document) ⇒ WeatherReportForecasts
constructor
Constructs the weather forecasts from an XML string or file containing the XML in BBC Backstage weather format.
Methods included from WeatherReportLocation
Constructor Details
#initialize(document) ⇒ WeatherReportForecasts
Constructs the weather forecasts from an XML string or file containing the XML in BBC Backstage weather format
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/weather_report.rb', line 133 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_url ⇒ Object (readonly)
Returns the value of attribute image_url.
130 131 132 |
# File 'lib/weather_report.rb', line 130 def image_url @image_url end |
#reading_date ⇒ Object (readonly)
Returns the value of attribute reading_date.
130 131 132 |
# File 'lib/weather_report.rb', line 130 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.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/weather_report.rb', line 161 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_today ⇒ Object
Returns the forecast for today, or nil is there is no such forecast.
150 151 152 |
# File 'lib/weather_report.rb', line 150 def for_today self.for(Date.today) end |
#for_tomorrow ⇒ Object
Returns the forecast for tomorrow, or nil is there is no such forecast.
155 156 157 |
# File 'lib/weather_report.rb', line 155 def for_tomorrow self.for(Date.today+1) end |