Class: Weather::WeatherFinder

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

Constant Summary collapse

XOAP_HOST =

Weather.com HOST

"xoap.weather.com"

Instance Method Summary collapse

Instance Method Details

#cacheObject

Used to instantiate the MemCache



78
79
80
# File 'lib/weatherfinder.rb', line 78

def cache
	@cache ||= MemCache.new(:namespace => 'Weather')
end

#cache_expiryObject

Defines expiration time in seconds



73
74
75
# File 'lib/weatherfinder.rb', line 73

def cache_expiry
			@cache_expiry || 60 * 10
end

#cache_expiry=(seconds) ⇒ Object

Defines default expiration time in seconds (ten minutes)



68
69
70
# File 'lib/weatherfinder.rb', line 68

def cache_expiry=(seconds)
	@cache_expiry = seconds
end

#cache_ok?Boolean

Verifies if the cache is enabled and actived

Returns:

  • (Boolean)


83
84
85
86
87
88
# File 'lib/weatherfinder.rb', line 83

def cache_ok?
	@enable_cache and 
	@cache.active? and 
	servers = @cache.instance_variable_get(:@servers) and 
    servers.collect{|s| s.alive?}.include?(true)
end

#disable_cacheObject

Disables cache



63
64
65
# File 'lib/weatherfinder.rb', line 63

def disable_cache
	@enable_cache = false
end

#enable_cacheObject

Enables cache



58
59
60
# File 'lib/weatherfinder.rb', line 58

def enable_cache
	@enable_cache = true
end

#find_location_id(search_string) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/weatherfinder.rb', line 45

def find_location_id(search_string)
  # URL encode
  url = "/weather/search/search?where=#{CGI.escape(search_string)}"

  # Retrieving the XML doc
  xml = Net::HTTP.get(XOAP_HOST, url);

  # Using Hpricot to parser the data
  doc = Hpricot.XML(xml)
			location_id = doc.at("/search/loc")["id"]
end

#load_forecast(location_id, days = 1, unit = 's') ⇒ Object

Default unit ajusted to Farenheit degrees :)



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/weatherfinder.rb', line 15

def load_forecast(location_id, days = 1, unit = 's')
  # Partner ID and License Key provided by Weather.com
  partner_id = "1094780686"
  license_key = "d2f90d27351df0a8"

  # URL to fetch the forecast from Weather.com
  url = "/weather/local/#{location_id}?cc=*&dayf=#{days}&par=#{partner_id}&key=#{license_key}&unit=#{unit}"

			if (cache_ok?)
# Retrieving data from cache
xml = @cache.get("#{location_id}:#{days}")
			end

			if (xml == nil)
# Retrieving the XML doc from weather.com
   xml = Net::HTTP.get(XOAP_HOST, url)

   if (cache_ok?)
	# Setting data on the cache
  		@cache.set("#{location_id}:#{days}", xml.to_s, cache_expiry)
  	end
			end

			# Using Hpricot to parse the data
			doc = Hpricot.XML(xml)

			# Passing doc to the Forecast object
			Weather::Forecast.new(doc)
end