yahoo_weatherman 0.5 → 0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/yahoo_weatherman/response.rb +24 -22
- data/lib/yahoo_weatherman.rb +12 -1
- data/spec/files/{belo_horizonte.rss → belo_horizonte_c.rss} +0 -0
- data/spec/files/belo_horizonte_f.rss +49 -0
- data/spec/spec_helper.rb +9 -3
- data/spec/yahoo_weatherman/response_spec.rb +16 -0
- data/yahoo_weatherman.gemspec +2 -2
- metadata +5 -4
@@ -6,8 +6,13 @@ module Weatherman
|
|
6
6
|
# We also use this class to do the i18n stuff.
|
7
7
|
#
|
8
8
|
class Response
|
9
|
+
|
9
10
|
attr_accessor :document_root
|
10
11
|
|
12
|
+
attr_accessor :language
|
13
|
+
|
14
|
+
LANGUAGES = {}
|
15
|
+
|
11
16
|
def initialize(raw, language = nil)
|
12
17
|
@document_root = Nokogiri::XML.parse(raw).xpath('rss/channel')
|
13
18
|
@language = language
|
@@ -179,46 +184,43 @@ module Weatherman
|
|
179
184
|
method == :to_date ? Date.parse(value) : value.send(method)
|
180
185
|
end
|
181
186
|
|
182
|
-
def translate!(
|
187
|
+
def translate!(attributes)
|
183
188
|
if i18n?
|
184
|
-
translate_text!
|
185
|
-
translate_locations!
|
189
|
+
translate_text! attributes
|
190
|
+
translate_locations! attributes
|
186
191
|
end
|
187
|
-
|
192
|
+
attributes
|
188
193
|
end
|
189
194
|
|
190
|
-
def translate_text!(
|
191
|
-
if
|
192
|
-
|
195
|
+
def translate_text!(attributes)
|
196
|
+
if attributes['text']
|
197
|
+
attributes['text'] = language_translations[attributes['code']]
|
193
198
|
end
|
194
199
|
end
|
195
200
|
|
196
|
-
def translate_locations!(
|
197
|
-
|
201
|
+
def translate_locations!(attributes)
|
202
|
+
locations_translations = language_translations['locations']
|
198
203
|
|
199
204
|
%w(city country region).each do |key|
|
200
|
-
next unless
|
205
|
+
next unless attributes.key? key
|
201
206
|
|
202
|
-
if translated =
|
203
|
-
|
207
|
+
if translated = locations_translations[attributes[key]]
|
208
|
+
attributes[key] = translated
|
204
209
|
end
|
205
210
|
end
|
206
211
|
end
|
207
212
|
|
208
|
-
def
|
209
|
-
|
210
|
-
@language_config ||= load_language_yaml!(@language)
|
211
|
-
end
|
212
|
-
@language_config
|
213
|
+
def language_translations
|
214
|
+
LANGUAGES[language] ||= load_language_yaml!
|
213
215
|
end
|
214
216
|
|
215
|
-
def
|
216
|
-
|
217
|
+
def load_language_yaml!
|
218
|
+
stream = File.read(File.join([I18N_YAML_DIR, language + '.yml']))
|
219
|
+
YAML.load(stream)
|
217
220
|
end
|
218
221
|
|
219
|
-
def
|
220
|
-
|
221
|
-
@language_config = YAML.load(stream)
|
222
|
+
def i18n?
|
223
|
+
!!@language
|
222
224
|
end
|
223
225
|
end
|
224
226
|
end
|
data/lib/yahoo_weatherman.rb
CHANGED
@@ -23,6 +23,17 @@ module Weatherman
|
|
23
23
|
class Client
|
24
24
|
attr_reader :options
|
25
25
|
|
26
|
+
#
|
27
|
+
# Accepts a optional hash containing the client options.
|
28
|
+
#
|
29
|
+
# Options:
|
30
|
+
#
|
31
|
+
# +unit+: the unit used for the temperature (defaults to Celsius).
|
32
|
+
# "f" => Fahrenheight
|
33
|
+
# "c" => Celsius
|
34
|
+
#
|
35
|
+
# +lang+: the language used in the response
|
36
|
+
#
|
26
37
|
def initialize(options = {})
|
27
38
|
@options = options
|
28
39
|
end
|
@@ -45,7 +56,7 @@ module Weatherman
|
|
45
56
|
end
|
46
57
|
|
47
58
|
def degrees_units
|
48
|
-
options[:
|
59
|
+
(options[:unit] || 'c').downcase
|
49
60
|
end
|
50
61
|
|
51
62
|
def get(url)
|
File without changes
|
@@ -0,0 +1,49 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
2
|
+
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
|
3
|
+
<channel>
|
4
|
+
|
5
|
+
<title>Yahoo! Weather - Belo Horizonte, BR</title>
|
6
|
+
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Belo_Horizonte__BR/*http://weather.yahoo.com/forecast/BRXX0033_f.html</link>
|
7
|
+
<description>Yahoo! Weather for Belo Horizonte, BR</description>
|
8
|
+
<language>en-us</language>
|
9
|
+
<lastBuildDate>Thu, 18 Mar 2010 12:00 am LST</lastBuildDate>
|
10
|
+
<ttl>60</ttl>
|
11
|
+
<yweather:location city="Belo Horizonte" region="MG" country="Brazil"/>
|
12
|
+
|
13
|
+
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
|
14
|
+
<yweather:wind chill="66" direction="100" speed="2" />
|
15
|
+
<yweather:atmosphere humidity="88" visibility="6.21" pressure="30.15" rising="0" />
|
16
|
+
<yweather:astronomy sunrise="5:59 am" sunset="6:08 pm"/>
|
17
|
+
<image>
|
18
|
+
<title>Yahoo! Weather</title>
|
19
|
+
<width>142</width>
|
20
|
+
<height>18</height>
|
21
|
+
<link>http://weather.yahoo.com</link>
|
22
|
+
<url>http://l.yimg.com/a/i/us/nws/th/main_142b.gif</url>
|
23
|
+
</image>
|
24
|
+
<item>
|
25
|
+
|
26
|
+
<title>Conditions for Belo Horizonte, BR at 12:00 am LST</title>
|
27
|
+
<geo:lat>-19.95</geo:lat>
|
28
|
+
<geo:long>-43.93</geo:long>
|
29
|
+
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Belo_Horizonte__BR/*http://weather.yahoo.com/forecast/BRXX0033_f.html</link>
|
30
|
+
<pubDate>Thu, 18 Mar 2010 12:00 am LST</pubDate>
|
31
|
+
<yweather:condition text="Fair" code="33" temp="66" date="Thu, 18 Mar 2010 12:00 am LST" />
|
32
|
+
<description><![CDATA[
|
33
|
+
<img src="http://l.yimg.com/a/i/us/we/52/33.gif"/><br />
|
34
|
+
<b>Current Conditions:</b><br />
|
35
|
+
Fair, 66 F<BR />
|
36
|
+
<BR /><b>Forecast:</b><BR />
|
37
|
+
Thu - Showers. High: 75 Low: 63<br />
|
38
|
+
Fri - Scattered Thunderstorms. High: 79 Low: 63<br />
|
39
|
+
<br />
|
40
|
+
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Belo_Horizonte__BR/*http://weather.yahoo.com/forecast/BRXX0033_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
|
41
|
+
(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
|
42
|
+
]]></description>
|
43
|
+
<yweather:forecast day="Thu" date="18 Mar 2010" low="63" high="75" text="Showers" code="11" />
|
44
|
+
<yweather:forecast day="Fri" date="19 Mar 2010" low="63" high="79" text="Scattered Thunderstorms" code="38" />
|
45
|
+
<guid isPermaLink="false">BRXX0033_2010_03_18_0_00_LST</guid>
|
46
|
+
</item>
|
47
|
+
|
48
|
+
</channel>
|
49
|
+
</rss>
|
data/spec/spec_helper.rb
CHANGED
@@ -6,10 +6,16 @@ require 'fakeweb'
|
|
6
6
|
require 'yahoo_weatherman'
|
7
7
|
require 'spec'
|
8
8
|
|
9
|
-
def
|
10
|
-
filepath = File.expand_path(File.dirname(__FILE__) + "/files/
|
9
|
+
def celsius_fixture
|
10
|
+
filepath = File.expand_path(File.dirname(__FILE__) + "/files/belo_horizonte_c.rss")
|
11
|
+
File.read filepath
|
12
|
+
end
|
13
|
+
|
14
|
+
def fahrenheight_fixture
|
15
|
+
filepath = File.expand_path(File.dirname(__FILE__) + "/files/belo_horizonte_f.rss")
|
11
16
|
File.read filepath
|
12
17
|
end
|
13
18
|
|
14
19
|
FakeWeb.allow_net_connect = false
|
15
|
-
FakeWeb.register_uri(:get, "http://weather.yahooapis.com/forecastrss?w=455821&u=c", :body =>
|
20
|
+
FakeWeb.register_uri(:get, "http://weather.yahooapis.com/forecastrss?w=455821&u=c", :body => celsius_fixture)
|
21
|
+
FakeWeb.register_uri(:get, "http://weather.yahooapis.com/forecastrss?w=455821&u=f", :body => fahrenheight_fixture)
|
@@ -109,4 +109,20 @@ DESCRIPTION
|
|
109
109
|
@response.forecasts.last['text'].should == 'Tempestades Intermitentes'
|
110
110
|
end
|
111
111
|
end
|
112
|
+
|
113
|
+
context 'using fahrenheiht as temperature unit' do
|
114
|
+
|
115
|
+
it 'should return the temperature as fahrenheight' do
|
116
|
+
client = Weatherman::Client.new :unit => 'F'
|
117
|
+
response = client.lookup_by_woeid 455821
|
118
|
+
|
119
|
+
response.units['temperature'].should == 'F'
|
120
|
+
response.forecasts.first['low'].should == 63
|
121
|
+
response.forecasts.last['low'].should == 63
|
122
|
+
response.forecasts.first['high'].should == 75
|
123
|
+
response.forecasts.last['high'].should == 79
|
124
|
+
response.condition['temp'].should == 66
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
112
128
|
end
|
data/yahoo_weatherman.gemspec
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
Gem::Specification.new do |gem|
|
2
2
|
gem.name = "yahoo_weatherman"
|
3
|
-
gem.version = "0.
|
3
|
+
gem.version = "0.6"
|
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."
|
7
7
|
gem.files = [
|
8
8
|
"yahoo_weatherman.gemspec",
|
9
9
|
"lib/yahoo_weatherman/image.rb", "lib/yahoo_weatherman/response.rb", "lib/yahoo_weatherman.rb",
|
10
|
-
"i18n/pt-br.yml", "spec/files/
|
10
|
+
"i18n/pt-br.yml", "spec/files/belo_horizonte_c.rss", "spec/files/belo_horizonte_f.rss", "spec/spec_helper.rb",
|
11
11
|
"spec/yahoo_weatherman/response_spec.rb", "spec/yahoo_weatherman/yahoo_weatherman_spec.rb"
|
12
12
|
]
|
13
13
|
gem.homepage = "http://github.com/dlt/yahoo_weatherman"
|
metadata
CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
version: "0.
|
7
|
+
- 6
|
8
|
+
version: "0.6"
|
9
9
|
platform: ruby
|
10
10
|
authors:
|
11
11
|
- Dalto Curvelano Junior
|
@@ -13,7 +13,7 @@ autorequire:
|
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
15
|
|
16
|
-
date: 2010-03-
|
16
|
+
date: 2010-03-18 00:00:00 -03:00
|
17
17
|
default_executable:
|
18
18
|
dependencies: []
|
19
19
|
|
@@ -31,7 +31,8 @@ files:
|
|
31
31
|
- lib/yahoo_weatherman/response.rb
|
32
32
|
- lib/yahoo_weatherman.rb
|
33
33
|
- i18n/pt-br.yml
|
34
|
-
- spec/files/
|
34
|
+
- spec/files/belo_horizonte_c.rss
|
35
|
+
- spec/files/belo_horizonte_f.rss
|
35
36
|
- spec/spec_helper.rb
|
36
37
|
- spec/yahoo_weatherman/response_spec.rb
|
37
38
|
- spec/yahoo_weatherman/yahoo_weatherman_spec.rb
|