Class: YahooWeather::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/yahoo-weather.rb

Overview

The main client object through which the Yahoo! Weather service may be accessed.

Constant Summary collapse

@@API_URL =

the url with which we obtain weather information from yahoo

"http://xml.weather.yahoo.com/forecastrss"

Instance Method Summary collapse

Constructor Details

#initialize(api_url = @@API_URL) ⇒ Client

Returns a new instance of Client.



271
272
273
# File 'lib/yahoo-weather.rb', line 271

def initialize (api_url = @@API_URL)
  @api_url = api_url
end

Instance Method Details

#lookup_location(location, units = 'f') ⇒ Object

Returns a YahooWeather::Response object detailing the current weather information for the specified location.

location can be either a US zip code or a location code. Location codes can be looked up at weather.yahoo.com, where it will appear in the URL that results from searching on the city or zip code. For instance, searching on ‘Seattle, WA’ results in a URL ending in ‘USWA0395.html’, so the location code for Seattle is ‘USWA0395’.

units allows specifying whether to retrieve information in Fahrenheit as f, or Celsius as c, and defaults to f.



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/yahoo-weather.rb', line 286

def lookup_location (location, units = 'f')
  # query the service to grab the xml data
  url = _request_url(location, units)
  #puts url
  begin
     response = Net::HTTP.get_response(URI.parse(url)).body.to_s
  rescue
    raise "failed to get weather via '#{url}': " + $!
  end
 
  # create the response object
  response = XmlSimple.xml_in(response)
 
  YahooWeather::Response.new(location, url, response)
end