ya_yahoo_weather 0.0.2 → 0.0.3
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.
- data/lib/ya_yahoo_weather/client.rb +11 -2
- data/lib/ya_yahoo_weather/version.rb +1 -1
- data/spec/ya_yahoo_weather/client_spec.rb +49 -15
- metadata +2 -2
@@ -3,17 +3,26 @@ module YaYahooWeather
|
|
3
3
|
class Client
|
4
4
|
@@YAHOO_WEATHER_API = "http://weather.yahooapis.com/forecastrss"
|
5
5
|
|
6
|
-
def
|
6
|
+
def get_detailed_weather_by_woeid(woeid)
|
7
7
|
begin
|
8
8
|
url = @@YAHOO_WEATHER_API + "?w=" + woeid.to_s + "&u=f"
|
9
9
|
response = do_http_get(url)
|
10
10
|
response_hash = Nori.parse(response)
|
11
|
-
|
11
|
+
# move some specific details to the item level
|
12
|
+
item_hash = response_hash['rss']['channel']['item']
|
13
|
+
['yweather:location', 'yweather:wind', 'yweather:atmosphere', 'yweather:astronomy'].each do |key|
|
14
|
+
item_hash[key] = response_hash['rss']['channel'][key]
|
15
|
+
end
|
16
|
+
item_hash
|
12
17
|
rescue => e
|
13
18
|
raise RuntimeError.new("Failed to get weather. woeid => #{woeid.inspect} url => #{url.inspect} e => #{e.inspect}")
|
14
19
|
end
|
15
20
|
end
|
16
21
|
|
22
|
+
def get_weather_conditions_by_woeid(woeid)
|
23
|
+
get_detailed_weather_by_woeid(woeid)['yweather:condition']
|
24
|
+
end
|
25
|
+
|
17
26
|
private
|
18
27
|
def do_http_get(url)
|
19
28
|
Net::HTTP.get_response(URI.parse(url)).body.to_s
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require './lib/ya_yahoo_weather'
|
2
2
|
|
3
3
|
def sample
|
4
|
+
# copied from http://developer.yahoo.com/weather/
|
4
5
|
<<END
|
5
6
|
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
6
7
|
<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#">
|
@@ -51,34 +52,67 @@ END
|
|
51
52
|
end
|
52
53
|
|
53
54
|
describe YaYahooWeather::Client do
|
54
|
-
# copied from http://developer.yahoo.com/weather/
|
55
55
|
describe "#initialize" do
|
56
56
|
it "should instantiate" do
|
57
57
|
client = YaYahooWeather::Client.new
|
58
58
|
end
|
59
59
|
end
|
60
|
-
describe "#
|
60
|
+
describe "#get_detailed_weather_by_woeid" do
|
61
61
|
context "on success" do
|
62
|
-
|
63
|
-
|
64
|
-
|
62
|
+
before :each do
|
63
|
+
@client = YaYahooWeather::Client.new
|
64
|
+
@client.stub!(:do_http_get).and_return(sample)
|
65
|
+
@response = @client.get_detailed_weather_by_woeid("mywoeid")
|
66
|
+
end
|
67
|
+
it "should return non-null result" do
|
68
|
+
@response.should_not be_nil
|
69
|
+
end
|
70
|
+
it "should return conditions" do
|
71
|
+
# check this value specifically as a sanity test
|
72
|
+
@response['yweather:condition']['@text'].should == 'Mostly Cloudy'
|
73
|
+
end
|
74
|
+
it "should return the forecast" do
|
75
|
+
# check this value specifically as a sanity test
|
76
|
+
@response['yweather:forecast'].first['@high'].should == '62'
|
77
|
+
end
|
78
|
+
it "should return astronomy data" do
|
79
|
+
@response['yweather:astronomy']['@sunrise'].should == '7:17 am'
|
80
|
+
end
|
65
81
|
end
|
66
|
-
|
67
|
-
|
68
|
-
|
82
|
+
context "on error" do
|
83
|
+
before :each do
|
84
|
+
@client = YaYahooWeather::Client.new
|
85
|
+
@client.stub!(:do_http_get).and_raise("Yahoo hates you")
|
86
|
+
end
|
87
|
+
it "should raise runtime exception" do
|
88
|
+
lambda {
|
89
|
+
@client.get_weather_conditions_by_woeid("mywoeid")
|
90
|
+
}.should raise_exception
|
91
|
+
end
|
69
92
|
end
|
70
|
-
|
93
|
+
end
|
94
|
+
describe "#get_weather_conditions_by_woeid" do
|
95
|
+
context "on success" do
|
71
96
|
before :each do
|
72
|
-
@
|
97
|
+
@client = YaYahooWeather::Client.new
|
98
|
+
@client.stub!(:do_http_get).and_return(sample)
|
73
99
|
end
|
74
|
-
it "should
|
75
|
-
@
|
100
|
+
it "should return non-null result" do
|
101
|
+
response = @client.get_weather_conditions_by_woeid("mywoeid")
|
102
|
+
response.should_not be_nil
|
76
103
|
end
|
77
|
-
|
78
|
-
|
104
|
+
describe "the result" do
|
105
|
+
before :each do
|
106
|
+
@response = @client.get_weather_conditions_by_woeid("mywoeid")
|
107
|
+
end
|
108
|
+
it "should be a hash" do
|
109
|
+
@response.instance_of?(Hash).should be_true
|
110
|
+
end
|
111
|
+
it "should contain the condition code" do
|
112
|
+
@response['@code'].to_i.should == 28
|
113
|
+
end
|
79
114
|
end
|
80
115
|
end
|
81
|
-
end
|
82
116
|
context "on error" do
|
83
117
|
before :each do
|
84
118
|
@client = YaYahooWeather::Client.new
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: ya_yahoo_weather
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- John Hinnegan
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-06-
|
13
|
+
date: 2011-06-09 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|