yahoo_weatherman 0.3 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,8 +10,7 @@ module Weatherman
10
10
 
11
11
  def initialize(raw, language = nil)
12
12
  @document_root = Nokogiri::XML.parse(raw).xpath('rss/channel')
13
- @language = !!language
14
- load_language_yaml!(language) if @language
13
+ @language = language
15
14
  end
16
15
 
17
16
  #
@@ -25,8 +24,7 @@ module Weatherman
25
24
  #
26
25
  def condition
27
26
  condition = item_attribute 'yweather:condition'
28
- condition = do_convertions(condition, [:code, :to_i], [:temp, :to_i], [:date, :to_date], :text)
29
- translate! condition
27
+ translate! do_convertions(condition, [:code, :to_i], [:temp, :to_i], [:date, :to_date], :text)
30
28
  end
31
29
 
32
30
  #
@@ -38,8 +36,7 @@ module Weatherman
38
36
  # wind['chill'] => 9.66
39
37
  #
40
38
  def wind
41
- wind = attribute 'yweather:wind'
42
- do_convertions(wind, [:chill, :to_i], [:direction, :to_i], [:speed, :to_f])
39
+ do_convertions(attribute('yweather:wind'), [:chill, :to_i], [:direction, :to_i], [:speed, :to_f])
43
40
  end
44
41
 
45
42
  #
@@ -54,7 +51,8 @@ module Weatherman
54
51
  #
55
52
  def forecasts
56
53
  item_attribute('yweather:forecast').collect do |forecast|
57
- translate! do_convertions(forecast, [:date, :to_date], [:low, :to_i], [:high, :to_i], [:code, :to_i], :day, :text)
54
+ convertions = [[:date, :to_date], [:low, :to_i], [:high, :to_i], [:code, :to_i], :day, :text]
55
+ translate! do_convertions(forecast, *convertions)
58
56
  end
59
57
  end
60
58
 
@@ -67,8 +65,7 @@ module Weatherman
67
65
  # location['city'] => Belo Horizonte
68
66
  #
69
67
  def location
70
- location = attribute 'yweather:location'
71
- translate! location
68
+ translate! attribute('yweather:location')
72
69
  end
73
70
 
74
71
  # Units:
@@ -126,11 +123,28 @@ module Weatherman
126
123
  end
127
124
 
128
125
  #
129
- # A short HTML snippet with a simple weather description.
126
+ # Description image. You might gonna need this if you have to customize the
127
+ # forecast summary.
128
+ #
129
+ def description_image
130
+ parsed_description.css('img').first # there's only one
131
+ end
132
+
133
+ #
134
+ # A short HTML snippet (raw text) with a simple weather description.
130
135
  #
131
136
  def description
132
137
  text_attribute 'description'
133
138
  end
139
+ alias :summary :description
140
+
141
+ #
142
+ # Description parsed by Nokogiri. This is better then #description
143
+ # if you have to walk through its nodes.
144
+ #
145
+ def parsed_description
146
+ @parsed_description ||= Nokogiri::HTML(description)
147
+ end
134
148
 
135
149
  private
136
150
  def attribute(attr, root = @document_root)
@@ -154,50 +168,57 @@ module Weatherman
154
168
 
155
169
  def do_convertions(attributes, *pairs)
156
170
  pairs.inject({}) do |hash, (attr, method)|
157
- value = attributes[attr.to_s]
158
- hash[attr.to_s] = convert(value, method)
171
+ key = attr.to_s
172
+ hash[key] = convert(attributes[key], method)
159
173
  hash
160
174
  end
161
175
  end
162
176
 
163
177
  def convert(value, method)
164
- if method == :to_date
165
- Date.parse(value)
166
- else
167
- method ? value.send(method) : value
168
- end
178
+ return value unless method
179
+ method == :to_date ? Date.parse(value) : value.send(method)
169
180
  end
170
181
 
171
- def load_language_yaml!(language)
172
- stream = File.read(File.join([I18N_YAML_DIR, language + '.yml']))
173
- @language_config = YAML.load(stream)
182
+ def translate!(attribute)
183
+ if i18n?
184
+ translate_text! attribute
185
+ translate_locations! attribute
186
+ end
187
+ attribute
174
188
  end
175
189
 
176
- def make_translations!(condition)
177
- if condition['text']
178
- condition['text'] = @language_config[condition['code']]
190
+ def translate_text!(attribute)
191
+ if attribute['text']
192
+ attribute['text'] = language_config[attribute['code']]
179
193
  end
194
+ end
180
195
 
181
- %w(city country region).each do |attr|
182
- next unless condition[attr]
183
- if translated = @language_config['locations'][condition[attr]]
184
- condition[attr] = translated
196
+ def translate_locations!(attribute)
197
+ locations_config = language_config['locations']
198
+
199
+ %w(city country region).each do |key|
200
+ next unless attribute[key]
201
+
202
+ if translated = locations_config[attribute[key]]
203
+ attribute[key] = translated
185
204
  end
186
205
  end
187
-
188
- condition
189
206
  end
190
207
 
191
- def translate!(attribute)
208
+ def language_config
192
209
  if i18n?
193
- make_translations! attribute
194
- else
195
- attribute
210
+ @language_config ||= load_language_yaml!(@language)
196
211
  end
212
+ @language_config
197
213
  end
198
214
 
199
215
  def i18n?
200
- @language
216
+ !!@language
217
+ end
218
+
219
+ def load_language_yaml!(language)
220
+ stream = File.read(File.join([I18N_YAML_DIR, language + '.yml']))
221
+ @language_config = YAML.load(stream)
201
222
  end
202
223
  end
203
224
  end
@@ -54,7 +54,7 @@ describe Weatherman::Response do
54
54
  last['code'].should == 38
55
55
  end
56
56
 
57
- it 'it should provide latitude and longitude' do
57
+ it 'should provide latitude and longitude' do
58
58
  @response.latitude.should == -19.95
59
59
  @response.longitude.should == -43.93
60
60
  end
@@ -74,6 +74,7 @@ Sun - Scattered Thunderstorms. High: 27 Low: 18<br />
74
74
  DESCRIPTION
75
75
 
76
76
  @response.description.should == description
77
+ @response.description.should == @response.summary
77
78
  end
78
79
 
79
80
  it 'should provide the weather image attributes' do
@@ -85,12 +86,17 @@ DESCRIPTION
85
86
  image['url'].should == 'http://l.yimg.com/a/i/us/nws/th/main_142b.gif'
86
87
  end
87
88
 
89
+ it 'should provide the forecast description icon' do
90
+ image = @response.description_image
91
+ image['src'].should == 'http://l.yimg.com/a/i/us/we/52/28.gif'
92
+ end
93
+
88
94
  context 'using internationalization' do
89
95
  before do
90
96
  @response = Weatherman::Client.new(:lang => 'pt-br').lookup_by_woeid 455821
91
97
  end
92
98
 
93
- it 'should translate the response#conditions attributes' do
99
+ it 'should translate the conditions details' do
94
100
  @response.condition['text'].should == 'Predominantemente Nublado'
95
101
  end
96
102
 
@@ -102,6 +108,5 @@ DESCRIPTION
102
108
  @response.forecasts.first['text'].should == 'Chuva'
103
109
  @response.forecasts.last['text'].should == 'Tempestades Intermitentes'
104
110
  end
105
-
106
111
  end
107
112
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "yahoo_weatherman"
3
- gem.version = "0.3"
3
+ gem.version = "0.5"
4
4
  gem.authors = ["Dalto Curvelano Junior"]
5
5
  gem.description = "A ruby wrapper to the Yahoo! Weather feed with i18n support."
6
6
  gem.summary = "A ruby wrapper to the Yahoo! Weather feed with i18n support."
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
8
- version: "0.3"
7
+ - 5
8
+ version: "0.5"
9
9
  platform: ruby
10
10
  authors:
11
11
  - Dalto Curvelano Junior