simple_weather 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/simple_weather.rb +40 -38
- metadata +1 -1
data/lib/simple_weather.rb
CHANGED
@@ -3,47 +3,49 @@ require 'json'
|
|
3
3
|
require 'net/http'
|
4
4
|
|
5
5
|
# api key from wunderground: 7b1e71b12f5535c0
|
6
|
-
|
7
|
-
class Weather
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
def updateWeather(city = 'MD/College Park')
|
14
|
-
base_url = 'http://api.wunderground.com/api/'
|
15
|
-
api_key = '7b1e71b12f5535c0'
|
16
|
-
query = '/conditions/q/'
|
17
|
-
format = '.json'
|
6
|
+
module SimpleWeather
|
7
|
+
class Weather
|
8
|
+
def initialize
|
9
|
+
@last_update = Time.now
|
10
|
+
@conditions = {}
|
11
|
+
end
|
18
12
|
|
19
|
-
|
20
|
-
|
21
|
-
|
13
|
+
def updateWeather(city = 'MD/College Park')
|
14
|
+
base_url = 'http://api.wunderground.com/api/'
|
15
|
+
api_key = '7b1e71b12f5535c0'
|
16
|
+
query = '/conditions/q/'
|
17
|
+
format = '.json'
|
18
|
+
|
19
|
+
url = URI.escape(base_url << api_key << query << city << format)
|
20
|
+
res = Net::HTTP.get_response(URI(url))
|
21
|
+
data = res.body if res.is_a?(Net::HTTPSuccess)
|
22
22
|
|
23
|
-
|
23
|
+
@conditions = JSON.parse(data)
|
24
|
+
|
25
|
+
if @conditions.has_key? 'error'
|
26
|
+
raise 'Web service error'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def getImageUrl()
|
31
|
+
@conditions['current_observation']['icon_url']
|
32
|
+
end
|
24
33
|
|
25
|
-
|
26
|
-
|
34
|
+
def getWeather()
|
35
|
+
@conditions['current_observation']['weather']
|
36
|
+
end
|
37
|
+
|
38
|
+
def getTemp_f()
|
39
|
+
@conditions['current_observation']['temp_f']
|
40
|
+
end
|
41
|
+
|
42
|
+
def getTemp_c()
|
43
|
+
@conditions['current_observation']['temp_c']
|
44
|
+
end
|
45
|
+
|
46
|
+
def getFeelsLike()
|
47
|
+
@conditions['current_observation']['fellslike_string']
|
27
48
|
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def getImageUrl()
|
31
|
-
@conditions['current_observation']['icon_url']
|
32
|
-
end
|
33
|
-
|
34
|
-
def getWeather()
|
35
|
-
@conditions['current_observation']['weather']
|
36
|
-
end
|
37
|
-
|
38
|
-
def getTemp_f()
|
39
|
-
@conditions['current_observation']['temp_f']
|
40
|
-
end
|
41
|
-
|
42
|
-
def getTemp_c()
|
43
|
-
@conditions['current_observation']['temp_c']
|
44
|
-
end
|
45
|
-
|
46
|
-
def getFeelsLike()
|
47
|
-
@conditions['current_observation']['fellslike_string']
|
48
49
|
end
|
49
50
|
end
|
51
|
+
ActionView::Base.send(:include, SimpleWeather) if defined?(ActionView::Base)
|