regentanz 0.2.0
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/.gitignore +5 -0
- data/CHANGELOG.rdoc +22 -0
- data/Gemfile +4 -0
- data/LICENSE +24 -0
- data/README.rdoc +46 -0
- data/Rakefile +23 -0
- data/lib/regentanz/astronomy.rb +69 -0
- data/lib/regentanz/cache/base.rb +51 -0
- data/lib/regentanz/cache/file.rb +86 -0
- data/lib/regentanz/cache.rb +2 -0
- data/lib/regentanz/callbacks.rb +18 -0
- data/lib/regentanz/conditions/base.rb +16 -0
- data/lib/regentanz/conditions/current.rb +14 -0
- data/lib/regentanz/conditions/forecast.rb +14 -0
- data/lib/regentanz/conditions.rb +3 -0
- data/lib/regentanz/configuration.rb +55 -0
- data/lib/regentanz/configurator.rb +22 -0
- data/lib/regentanz/google_weather.rb +151 -0
- data/lib/regentanz/parser/google_weather.rb +100 -0
- data/lib/regentanz/parser.rb +1 -0
- data/lib/regentanz/test_helper.rb +52 -0
- data/lib/regentanz/version.rb +4 -0
- data/lib/regentanz.rb +12 -0
- data/regentanz.gemspec +31 -0
- data/test/factories.rb +8 -0
- data/test/support/support_mailer.rb +7 -0
- data/test/support/tmp/.gitignore +1 -0
- data/test/support/valid_response.xml.erb +26 -0
- data/test/test_helper.rb +18 -0
- data/test/unit/astronomy_test.rb +26 -0
- data/test/unit/cache/base_test.rb +53 -0
- data/test/unit/cache/file_test.rb +141 -0
- data/test/unit/callbacks_test.rb +27 -0
- data/test/unit/configuration_test.rb +57 -0
- data/test/unit/current_condition_test.rb +33 -0
- data/test/unit/forecast_condition_test.rb +35 -0
- data/test/unit/google_weather_test.rb +131 -0
- data/test/unit/parser/google_weather_parser_test.rb +71 -0
- metadata +219 -0
@@ -0,0 +1,131 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
2
|
+
|
3
|
+
class GoogleWeatherTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
TEST_CACHE_FILE_NAME = File.join(Regentanz.configuration.cache_dir, "regentanz_test.xml")
|
6
|
+
|
7
|
+
def setup
|
8
|
+
# FIXME Remove ActionMailer
|
9
|
+
ActionMailer::Base.deliveries.clear
|
10
|
+
setup_regentanz_test_configuration!
|
11
|
+
end
|
12
|
+
|
13
|
+
def teardown
|
14
|
+
Dir.glob(File.join(Regentanz.configuration.cache_dir, "**", "*")).each { |file| File.delete(file) }
|
15
|
+
end
|
16
|
+
|
17
|
+
test "should initialize with options hash" do
|
18
|
+
Regentanz::GoogleWeather.any_instance.expects(:get_weather).never()
|
19
|
+
options = { :location => "Test Valley", :lang => "es" }
|
20
|
+
obj = Regentanz::GoogleWeather.new(options)
|
21
|
+
assert_equal "Test Valley", obj.location
|
22
|
+
assert_equal "es", obj.lang
|
23
|
+
end
|
24
|
+
|
25
|
+
test "should be compatible with old constructor" do
|
26
|
+
Regentanz::GoogleWeather.any_instance.expects(:get_weather).never()
|
27
|
+
obj = Factory(:google_weather, :location => "Test Valley")
|
28
|
+
assert_equal "Test Valley", obj.location
|
29
|
+
assert_equal "de", obj.lang
|
30
|
+
end
|
31
|
+
|
32
|
+
test "should have cache instance" do
|
33
|
+
obj = Factory(:google_weather)
|
34
|
+
assert_respond_to obj, :cache
|
35
|
+
assert_kind_of Regentanz.configuration.cache_backend, obj.cache
|
36
|
+
end
|
37
|
+
|
38
|
+
test "should enter retry state and send email if invalid xml file has been found" do
|
39
|
+
stub_valid_xml_api_response!
|
40
|
+
Regentanz::GoogleWeather.any_instance.expects(:api_failure_detected) # callback
|
41
|
+
|
42
|
+
weather = Factory(:google_weather)
|
43
|
+
assert !weather.waiting_for_retry?
|
44
|
+
create_invalid_xml_response(TEST_CACHE_FILE_NAME)
|
45
|
+
|
46
|
+
assert_difference "ActionMailer::Base.deliveries.size" do
|
47
|
+
weather.get_weather!
|
48
|
+
assert weather.waiting_for_retry?
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
test "should leave retry state remove invalid cache file when retry_ttl waittime is over" do
|
53
|
+
File.new(Regentanz.configuration.retry_marker, "w+").close
|
54
|
+
assert File.exists?(Regentanz.configuration.retry_marker) # ensure test setup
|
55
|
+
Regentanz.configuration.retry_ttl = 0; sleep 0.1
|
56
|
+
Regentanz::GoogleWeather.any_instance.expects(:api_failure_resumed) # callback
|
57
|
+
|
58
|
+
create_invalid_xml_response(TEST_CACHE_FILE_NAME)
|
59
|
+
weather = Factory(:google_weather)
|
60
|
+
|
61
|
+
assert_difference "ActionMailer::Base.deliveries.size" do
|
62
|
+
weather.get_weather!
|
63
|
+
assert !weather.waiting_for_retry?
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
test "should have lang option" do
|
68
|
+
weather = Factory(:google_weather, :lang => :es)
|
69
|
+
assert_equal "es", weather.lang
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_should_calculate_sunrise_and_sunset_based_on_geodata
|
73
|
+
weather = Regentanz::GoogleWeather.new("Berlin", weather_options(:geo_location => nil) )
|
74
|
+
assert weather.respond_to? :sunrise
|
75
|
+
assert weather.respond_to? :sunset
|
76
|
+
assert_nil weather.sunrise
|
77
|
+
assert_nil weather.sunset
|
78
|
+
|
79
|
+
lat = 52.5163253260716
|
80
|
+
lng = 13.3780860900879
|
81
|
+
|
82
|
+
weather = Regentanz::GoogleWeather.new("Berlin", weather_options(:geodata => {:lat => lat, :lng => lng}) )
|
83
|
+
assert weather.sunrise.is_a? Time
|
84
|
+
assert weather.sunset.is_a? Time
|
85
|
+
end
|
86
|
+
|
87
|
+
test "should set default cache_id by location" do
|
88
|
+
obj = Regentanz::GoogleWeather.new(:location => "Test Valley", :cache_id => nil)
|
89
|
+
assert_not_nil obj.cache_id
|
90
|
+
end
|
91
|
+
|
92
|
+
test "should work with caching disabled" do
|
93
|
+
Regentanz.configuration.cache_backend = nil
|
94
|
+
stub_valid_xml_api_response!
|
95
|
+
obj = Factory(:google_weather, :cache_id => nil)
|
96
|
+
assert_nil obj.cache
|
97
|
+
obj.get_weather!
|
98
|
+
end
|
99
|
+
|
100
|
+
test "should delegate retry state to cache instance" do
|
101
|
+
obj = Factory(:google_weather)
|
102
|
+
assert_respond_to obj, :waiting_for_retry?
|
103
|
+
assert_not_nil obj.cache
|
104
|
+
assert !obj.waiting_for_retry?
|
105
|
+
obj.cache.expects(:waiting_for_retry?).returns(true)
|
106
|
+
assert obj.waiting_for_retry?
|
107
|
+
end
|
108
|
+
|
109
|
+
test "should respond_to present?" do
|
110
|
+
obj = Factory(:google_weather)
|
111
|
+
assert_respond_to obj, :present?
|
112
|
+
|
113
|
+
Regentanz::GoogleWeather.any_instance.expects(:current).returns(nil)
|
114
|
+
Regentanz::GoogleWeather.any_instance.expects(:forecast).returns(nil)
|
115
|
+
assert_equal false, obj.present?
|
116
|
+
|
117
|
+
Regentanz::GoogleWeather.any_instance.expects(:current).returns(nil)
|
118
|
+
Regentanz::GoogleWeather.any_instance.expects(:forecast).returns(true)
|
119
|
+
assert_equal true, obj.present?
|
120
|
+
Regentanz::GoogleWeather.any_instance.expects(:current).returns(true)
|
121
|
+
assert_equal true, obj.present?
|
122
|
+
end
|
123
|
+
|
124
|
+
private
|
125
|
+
|
126
|
+
# Return a few default options for the test environment
|
127
|
+
def weather_options(options = {})
|
128
|
+
{:cache_id => "test"}.merge(options.symbolize_keys!)
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
|
2
|
+
|
3
|
+
class GoogleWeatherParserTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
setup_regentanz_test_configuration!
|
7
|
+
end
|
8
|
+
|
9
|
+
test "should have parser instance" do
|
10
|
+
obj = Regentanz::GoogleWeather.new
|
11
|
+
assert_respond_to obj, :parser
|
12
|
+
assert_not_nil obj.parser
|
13
|
+
assert_kind_of Regentanz::Parser::GoogleWeather, obj.parser
|
14
|
+
end
|
15
|
+
|
16
|
+
test "should convert encoding" do
|
17
|
+
obj = Regentanz::Parser::GoogleWeather.new
|
18
|
+
assert_respond_to obj, :convert_encoding
|
19
|
+
assert_nil obj.convert_encoding(nil)
|
20
|
+
assert_nil obj.convert_encoding("")
|
21
|
+
# FIXME test actual conversion
|
22
|
+
end
|
23
|
+
|
24
|
+
test "should parse xml for current condition" do
|
25
|
+
obj = Regentanz::Parser::GoogleWeather.new
|
26
|
+
assert_respond_to obj, :parse_current!
|
27
|
+
assert_nil obj.parse_current!(nil)
|
28
|
+
assert_nil obj.parse_current!("")
|
29
|
+
|
30
|
+
xml = valid_xml_response
|
31
|
+
parsed = obj.parse_current!(xml)
|
32
|
+
assert_kind_of Regentanz::Conditions::Current, parsed
|
33
|
+
assert_equal "Feuchtigkeit: 57 %", parsed.humidity
|
34
|
+
assert_equal "Wind: W mit 24 km/h", parsed.wind_condition
|
35
|
+
assert_equal 25, parsed.temp_c
|
36
|
+
assert_equal 77, parsed.temp_f
|
37
|
+
assert_equal "http://www.google.com/ig/images/weather/sunny.gif", parsed.icon
|
38
|
+
assert_equal "Klar", parsed.condition
|
39
|
+
end
|
40
|
+
|
41
|
+
test "should rescue current condition parsing from parse erros" do
|
42
|
+
obj = Regentanz::Parser::GoogleWeather.new
|
43
|
+
xml = invalid_xml_response
|
44
|
+
assert_nothing_raised { assert_nil obj.parse_current!(xml) }
|
45
|
+
end
|
46
|
+
|
47
|
+
test "should parse xml for forecast" do
|
48
|
+
obj = Regentanz::Parser::GoogleWeather.new
|
49
|
+
assert_respond_to obj, :parse_forecast!
|
50
|
+
assert_nil obj.parse_forecast!(nil)
|
51
|
+
assert_nil obj.parse_forecast!("")
|
52
|
+
|
53
|
+
xml = valid_xml_response
|
54
|
+
parsed = obj.parse_forecast!(xml)
|
55
|
+
assert_kind_of Array, parsed
|
56
|
+
assert_equal 1, parsed.size
|
57
|
+
assert_kind_of Regentanz::Conditions::Forecast, parsed.first
|
58
|
+
assert_equal "Do.", parsed.first.day_of_week
|
59
|
+
assert_equal 31, parsed.first.high
|
60
|
+
assert_equal 16, parsed.first.low
|
61
|
+
assert_equal "http://www.google.com/ig/images/weather/chance_of_rain.gif", parsed.first.icon
|
62
|
+
assert_equal "Vereinzelt Regen", parsed.first.condition
|
63
|
+
end
|
64
|
+
|
65
|
+
test "should rescue forecast parsing from parse erros" do
|
66
|
+
obj = Regentanz::Parser::GoogleWeather.new
|
67
|
+
xml = invalid_xml_response
|
68
|
+
assert_nothing_raised { assert_nil obj.parse_forecast!(xml) }
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,219 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: regentanz
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Carsten Zimmermann
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-01-30 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: actionmailer
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 19
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
- 10
|
34
|
+
version: 3.0.10
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: activesupport
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 19
|
46
|
+
segments:
|
47
|
+
- 3
|
48
|
+
- 0
|
49
|
+
- 10
|
50
|
+
version: 3.0.10
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rake
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
type: :development
|
66
|
+
version_requirements: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
name: mocha
|
69
|
+
prerelease: false
|
70
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
type: :development
|
80
|
+
version_requirements: *id004
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: bundler
|
83
|
+
prerelease: false
|
84
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 3
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
type: :development
|
94
|
+
version_requirements: *id005
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: redgreen
|
97
|
+
prerelease: false
|
98
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
hash: 3
|
104
|
+
segments:
|
105
|
+
- 0
|
106
|
+
version: "0"
|
107
|
+
type: :development
|
108
|
+
version_requirements: *id006
|
109
|
+
- !ruby/object:Gem::Dependency
|
110
|
+
name: factory_girl
|
111
|
+
prerelease: false
|
112
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
hash: 3
|
118
|
+
segments:
|
119
|
+
- 0
|
120
|
+
version: "0"
|
121
|
+
type: :development
|
122
|
+
version_requirements: *id007
|
123
|
+
description: Library to access the Google Weather API
|
124
|
+
email:
|
125
|
+
- cz@aegisnet.de
|
126
|
+
executables: []
|
127
|
+
|
128
|
+
extensions: []
|
129
|
+
|
130
|
+
extra_rdoc_files: []
|
131
|
+
|
132
|
+
files:
|
133
|
+
- .gitignore
|
134
|
+
- CHANGELOG.rdoc
|
135
|
+
- Gemfile
|
136
|
+
- LICENSE
|
137
|
+
- README.rdoc
|
138
|
+
- Rakefile
|
139
|
+
- lib/regentanz.rb
|
140
|
+
- lib/regentanz/astronomy.rb
|
141
|
+
- lib/regentanz/cache.rb
|
142
|
+
- lib/regentanz/cache/base.rb
|
143
|
+
- lib/regentanz/cache/file.rb
|
144
|
+
- lib/regentanz/callbacks.rb
|
145
|
+
- lib/regentanz/conditions.rb
|
146
|
+
- lib/regentanz/conditions/base.rb
|
147
|
+
- lib/regentanz/conditions/current.rb
|
148
|
+
- lib/regentanz/conditions/forecast.rb
|
149
|
+
- lib/regentanz/configuration.rb
|
150
|
+
- lib/regentanz/configurator.rb
|
151
|
+
- lib/regentanz/google_weather.rb
|
152
|
+
- lib/regentanz/parser.rb
|
153
|
+
- lib/regentanz/parser/google_weather.rb
|
154
|
+
- lib/regentanz/test_helper.rb
|
155
|
+
- lib/regentanz/version.rb
|
156
|
+
- regentanz.gemspec
|
157
|
+
- test/factories.rb
|
158
|
+
- test/support/support_mailer.rb
|
159
|
+
- test/support/tmp/.gitignore
|
160
|
+
- test/support/valid_response.xml.erb
|
161
|
+
- test/test_helper.rb
|
162
|
+
- test/unit/astronomy_test.rb
|
163
|
+
- test/unit/cache/base_test.rb
|
164
|
+
- test/unit/cache/file_test.rb
|
165
|
+
- test/unit/callbacks_test.rb
|
166
|
+
- test/unit/configuration_test.rb
|
167
|
+
- test/unit/current_condition_test.rb
|
168
|
+
- test/unit/forecast_condition_test.rb
|
169
|
+
- test/unit/google_weather_test.rb
|
170
|
+
- test/unit/parser/google_weather_parser_test.rb
|
171
|
+
has_rdoc: true
|
172
|
+
homepage: ""
|
173
|
+
licenses: []
|
174
|
+
|
175
|
+
post_install_message:
|
176
|
+
rdoc_options: []
|
177
|
+
|
178
|
+
require_paths:
|
179
|
+
- lib
|
180
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
181
|
+
none: false
|
182
|
+
requirements:
|
183
|
+
- - ">="
|
184
|
+
- !ruby/object:Gem::Version
|
185
|
+
hash: 3
|
186
|
+
segments:
|
187
|
+
- 0
|
188
|
+
version: "0"
|
189
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
190
|
+
none: false
|
191
|
+
requirements:
|
192
|
+
- - ">="
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
hash: 3
|
195
|
+
segments:
|
196
|
+
- 0
|
197
|
+
version: "0"
|
198
|
+
requirements: []
|
199
|
+
|
200
|
+
rubyforge_project: regentanz
|
201
|
+
rubygems_version: 1.4.2
|
202
|
+
signing_key:
|
203
|
+
specification_version: 3
|
204
|
+
summary: Library to access the Google Weather API
|
205
|
+
test_files:
|
206
|
+
- test/factories.rb
|
207
|
+
- test/support/support_mailer.rb
|
208
|
+
- test/support/tmp/.gitignore
|
209
|
+
- test/support/valid_response.xml.erb
|
210
|
+
- test/test_helper.rb
|
211
|
+
- test/unit/astronomy_test.rb
|
212
|
+
- test/unit/cache/base_test.rb
|
213
|
+
- test/unit/cache/file_test.rb
|
214
|
+
- test/unit/callbacks_test.rb
|
215
|
+
- test/unit/configuration_test.rb
|
216
|
+
- test/unit/current_condition_test.rb
|
217
|
+
- test/unit/forecast_condition_test.rb
|
218
|
+
- test/unit/google_weather_test.rb
|
219
|
+
- test/unit/parser/google_weather_parser_test.rb
|