| Class | WeatherReport |
| In: |
lib/weather_report.rb
|
| Parent: | Object |
Class containing weather observations and forecasts for the location specified by id in the constructor.
| VERSION | = | '0.0.4' |
| id | [R] | The id is the number (1..9999) the BBC uses to identify cities for weather reports |
Requires a valid BBC Backstage weather id (or any call to the API will return a FormatError)
# File lib/weather_report.rb, line 24 def initialize(location_id) @id = location_id end
Returns the weather forecast for the current id. If one is not currently loaded then this will go to the BBC Backstage API and download it. Alternatively, by specifying force_reload = true the observation will be loaded from the BBC regardless of whether one has already been downloaded.
# File lib/weather_report.rb, line 37 def forecast(force_reload = false) @forecast = WeatherReportForecasts.new(fetch(forecast_url())) if force_reload or @forecast.nil? @forecast end
Returns the weather observation for the current id. If one is not currently loaded then this will go to the BBC Backstage API and download it. Alternatively, by specifying force_reload = true the observation will be loaded from the BBC regardless of whether one has already been downloaded.
# File lib/weather_report.rb, line 30 def observation(force_reload = false) @observation = WeatherReportObservation.new(fetch(observation_url())) if force_reload or @observation.nil? @observation end
Fetch Response from the api
# File lib/weather_report.rb, line 45 def fetch(url) response = Net::HTTP.get_response(URI.parse(url)).body # Check if a response was returned at all raise(WeatherReport::WeatherReportNoResponseError, "WeatherReport Error: No Response.") unless response response end
The url for getting weather forecasts from BBC Backstage
# File lib/weather_report.rb, line 61 def forecast_url() "http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/#{@id}.xml" #"http://newsrss.bbc.co.uk/weather/forecast/#{@id}/Next3DaysRSS.xml" end