Class: SimpleWeather::Weather

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

Instance Method Summary collapse

Constructor Details

#initializeWeather

Returns a new instance of Weather.



8
9
10
# File 'lib/simple_weather.rb', line 8

def initialize
	@@conditions = {}
end

Instance Method Details

#getConditionObject



59
60
61
# File 'lib/simple_weather.rb', line 59

def getCondition
	@@conditions['weather']['current_conditions']['condition']['attributes']['data'] 
end

#getDataObject



83
84
85
86
87
88
89
90
91
# File 'lib/simple_weather.rb', line 83

def getData
	data = {}
	data['conditon'] = getCondition
	data['temp_c'] = getTemp_c.to_f
	data['temp_f'] = getTemp_f.to_f
	data['icon_url'] = getImageUrl
	data['location'] = getLocation
	data
end

#getHumidityObject



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

def getHumidity
	@@conditions['weather']['current_conditions']['humidity']['attributes']['data'] 
end

#getImageUrlObject



55
56
57
# File 'lib/simple_weather.rb', line 55

def getImageUrl  
	@@conditions['weather']['current_conditions']['icon']['attributes']['data'] 
end

#getLocationObject



79
80
81
# File 'lib/simple_weather.rb', line 79

def getLocation
	@@conditions['weather']['forecast_information']['city']['attributes']['data']
end

#getTemp_cObject



67
68
69
# File 'lib/simple_weather.rb', line 67

def getTemp_c
	@@conditions['weather']['current_conditions']['temp_c']['attributes']['data'] 
end

#getTemp_fObject



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

def getTemp_f
	@@conditions['weather']['current_conditions']['temp_f']['attributes']['data'] 
end

#getWindConditionObject



75
76
77
# File 'lib/simple_weather.rb', line 75

def getWindCondition 
	@@conditions['weather']['current_conditions']['wind_condition']['attributes']['data'] 
end

#processAllNodes(root, hash = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/simple_weather.rb', line 12

def processAllNodes(root, hash = {})
	if root == nil 
		return 
	end
	hash[root.name] = {}
	processNodeAttr(root, hash)
	if root.children.length > 0
		root.children.each { |c|
			processAllNodes(c, hash[root.name])
		}
	end
end

#processNodeAttr(node, hash = {}) ⇒ Object



25
26
27
28
29
30
# File 'lib/simple_weather.rb', line 25

def processNodeAttr(node, hash = {})
	hash[node.name]['attributes'] = {}
	node.attributes.each { |k, v|
		hash[node.name]['attributes']["#{k}"] = v.to_s
	}
end

#updateWeather(city = 'College Park') ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/simple_weather.rb', line 32

def updateWeather(city = 'College Park')
	base_url = 'http://www.google.com/ig/api?weather='
	url = URI.escape(base_url << city)
	
	begin
		res = Net::HTTP.get_response(URI(url))
		if res.is_a?(Net::HTTPSuccess)
			data = res.body 
		else
			raise "Service unavailable"
		end
	rescue
		res = Net::HTTP.get_response(URI(url))
		data = res.body if res.is_a?(Net::HTTPSuccess)
	end
	
	xml = Nokogiri::XML::Document.parse(data)
	root = xml.xpath('/xml_api_reply/weather').first
	
	processAllNodes(root, @@conditions)
	return getData
end