simple_weather 0.0.12 → 0.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/simple_weather.rb +77 -40
  2. metadata +2 -2
@@ -1,50 +1,87 @@
1
1
  require 'rubygems'
2
- require 'json'
2
+ require 'nokogiri'
3
3
  require 'net/http'
4
4
 
5
5
  # api key from wunderground: 7b1e71b12f5535c0
6
6
  module SimpleWeather
7
- def self.initialize
8
- @last_update = Time.now
9
- @conditions = {}
10
- end
11
-
12
- def self.updateWeather(@conditions, city = 'MD/College Park')
13
- base_url = 'http://api.wunderground.com/api/'
14
- api_key = '7b1e71b12f5535c0'
15
- query = '/conditions/q/'
16
- format = '.json'
17
-
18
- url = URI.escape(base_url << api_key << query << city << format)
19
- res = Net::HTTP.get_response(URI(url))
20
- data = res.body if res.is_a?(Net::HTTPSuccess)
21
-
22
- @conditions = JSON.parse(data)
7
+ class Weather
8
+ @@conditions = {}
23
9
 
24
- if @conditions.has_key? 'error'
25
- raise 'Web service error'
10
+ def self.processAllNodes(root, hash = {})
11
+ if root == nil
12
+ return
13
+ end
14
+ hash[root.name] = {}
15
+ processNodeAttr(root, hash)
16
+ if root.children.length > 0
17
+ root.children.each { |c|
18
+ self.processAllNodes(c, hash[root.name])
19
+ }
20
+ end
26
21
  end
27
- end
28
22
 
29
- def self.getImageUrl
30
- @conditions['current_observation']['icon_url']
31
- end
32
-
33
- def self.getWeather
34
- @conditions['current_observation']['weather']
35
- end
36
-
37
- def self.getTemp_f
38
- @conditions['current_observation']['temp_f']
39
- end
40
-
41
- def self.getTemp_c
42
- @conditions['current_observation']['temp_c']
43
- end
44
-
45
- def self.getFeelsLike
46
- @conditions['current_observation']['fellslike_string']
23
+ def self.processNodeAttr(node, hash = {})
24
+ hash[node.name]['attributes'] = {}
25
+ node.attributes.each { |k, v|
26
+ hash[node.name]['attributes']["#{k}"] = v.to_s
27
+ }
28
+ end
29
+
30
+ def self.updateWeather(city = 'College Park')
31
+ base_url = 'http://www.google.com/ig/api?weather='
32
+ url = URI.escape(base_url << city)
33
+
34
+ begin
35
+ res = Net::HTTP.get_response(URI(url))
36
+ if res.is_a?(Net::HTTPSuccess)
37
+ data = res.body
38
+ else
39
+ raise "Service unavailable"
40
+ end
41
+ rescue
42
+ res = Net::HTTP.get_response(URI(url))
43
+ data = res.body if res.is_a?(Net::HTTPSuccess)
44
+ end
45
+
46
+ xml = Nokogiri::XML::Document.parse(data)
47
+ root = xml.xpath('/xml_api_reply/weather').first
48
+
49
+ processAllNodes(root, @@conditions)
50
+ return getData
51
+ end
52
+
53
+ def self.getImageUrl
54
+ @@conditions['weather']['current_conditions']['icon']['attributes']['data']
55
+ end
56
+
57
+ def self.getCondition
58
+ @@conditions['weather']['current_conditions']['condition']['attributes']['data']
59
+ end
60
+
61
+ def self.getTemp_f
62
+ @@conditions['weather']['current_conditions']['temp_f']['attributes']['data']
63
+ end
64
+
65
+ def self.getTemp_c
66
+ @@conditions['weather']['current_conditions']['temp_c']['attributes']['data']
67
+ end
68
+
69
+ def self.getHumidity
70
+ @@conditions['weather']['current_conditions']['humidity']['attributes']['data']
71
+ end
72
+
73
+ def self.getWindCondition
74
+ @@conditions['weather']['current_conditions']['wind_condition']['attributes']['data']
75
+ end
76
+
77
+ def self.getData
78
+ data = {}
79
+ data['icon_url'] = getImageUrl
80
+ data['conditon'] = getCondition
81
+ data['temp_f'] = getTemp_f
82
+ data['temp_c'] = getTemp_c
83
+ data
84
+ end
47
85
  end
48
86
  end
49
-
50
- ActionView::Base.send(:include, SimpleWeather) if defined?(ActionView::Base)
87
+ # ActionView::Base.send(:include, SimpleWeather) if defined?(ActionView::Base)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_weather
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.17
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-12 00:00:00.000000000 Z
12
+ date: 2012-08-13 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A simple weather gem
15
15
  email: ye.yicheng123@gmail.com