Class: YahooWeather::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/yahoo_weather/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#unitsObject (readonly)

Returns the value of attribute units.



2
3
4
# File 'lib/yahoo_weather/client.rb', line 2

def units
  @units
end

#woeidObject (readonly)

Returns the value of attribute woeid.



2
3
4
# File 'lib/yahoo_weather/client.rb', line 2

def woeid
  @woeid
end

Instance Method Details

#fetch(woeid, units = YahooWeather::Units::FAHRENHEIT) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/yahoo_weather/client.rb', line 4

def fetch(woeid, units = YahooWeather::Units::FAHRENHEIT)
  @units = units
  @woeid = woeid
  doc = JSON(read_json)
  if doc['query']['count'] > 0
    YahooWeather::Response.new(doc)
  else
    nil
  end
end

#fetch_by_location(location, units = YahooWeather::Units::FAHRENHEIT) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/yahoo_weather/client.rb', line 15

def fetch_by_location(location, units = YahooWeather::Units::FAHRENHEIT)
  woeid_cache_key = "yahoo_weather_woeid for #{location}"
  unless Rails.cache.exist? woeid_cache_key
    url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.placefinder%20where%20text%3D%22#{::CGI.escape(location)}%22&format=json&callback="
    json = fetch_json(url)
    doc = JSON(json)
    if doc['query']['count'] > 0
      woeid = doc['query']['results']['Result']['woeid']
      Rails.cache.write(woeid_cache_key, woeid)
    else
      return nil
    end
  else
    woeid = Rails.cache.read woeid_cache_key
  end
  fetch(woeid, units)
rescue
  nil
end