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