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 Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#license_keyObject

Returns the value of attribute license_key.



11
12
13
# File 'lib/weatherfinder.rb', line 11

def license_key
  @license_key
end

#partner_idObject

Returns the value of attribute partner_id.



11
12
13
# File 'lib/weatherfinder.rb', line 11

def partner_id
  @partner_id
end

Instance Method Details

#cacheObject

Used to instantiate the MemCache



76
77
78
# File 'lib/weatherfinder.rb', line 76

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

#cache_expiryObject

Defines expiration time in seconds



71
72
73
# File 'lib/weatherfinder.rb', line 71

def cache_expiry
			@cache_expiry || 60 * 10
end

#cache_expiry=(seconds) ⇒ Object

Defines default expiration time in seconds (ten minutes)



66
67
68
# File 'lib/weatherfinder.rb', line 66

def cache_expiry=(seconds)
	@cache_expiry = seconds
end

#cache_ok?Boolean

Verifies if the cache is enabled and actived

Returns:

  • (Boolean)


81
82
83
84
85
86
# File 'lib/weatherfinder.rb', line 81

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



61
62
63
# File 'lib/weatherfinder.rb', line 61

def disable_cache
	@enable_cache = false
end

#enable_cacheObject

Enables cache



56
57
58
# File 'lib/weatherfinder.rb', line 56

def enable_cache
	@enable_cache = true
end

#find_location_id(search_string) ⇒ Object



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

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 :)



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
# File 'lib/weatherfinder.rb', line 17

def load_forecast(location_id, days = 1, unit = 's')
  # 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