barometer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +266 -0
  3. data/VERSION.yml +4 -0
  4. data/bin/barometer +63 -0
  5. data/lib/barometer.rb +52 -0
  6. data/lib/barometer/base.rb +52 -0
  7. data/lib/barometer/data.rb +15 -0
  8. data/lib/barometer/data/current.rb +93 -0
  9. data/lib/barometer/data/distance.rb +131 -0
  10. data/lib/barometer/data/forecast.rb +66 -0
  11. data/lib/barometer/data/geo.rb +98 -0
  12. data/lib/barometer/data/location.rb +20 -0
  13. data/lib/barometer/data/measurement.rb +161 -0
  14. data/lib/barometer/data/pressure.rb +133 -0
  15. data/lib/barometer/data/speed.rb +147 -0
  16. data/lib/barometer/data/sun.rb +35 -0
  17. data/lib/barometer/data/temperature.rb +164 -0
  18. data/lib/barometer/data/units.rb +55 -0
  19. data/lib/barometer/data/zone.rb +124 -0
  20. data/lib/barometer/extensions/graticule.rb +50 -0
  21. data/lib/barometer/extensions/httparty.rb +21 -0
  22. data/lib/barometer/query.rb +228 -0
  23. data/lib/barometer/services.rb +6 -0
  24. data/lib/barometer/services/google.rb +146 -0
  25. data/lib/barometer/services/noaa.rb +6 -0
  26. data/lib/barometer/services/service.rb +324 -0
  27. data/lib/barometer/services/weather_bug.rb +6 -0
  28. data/lib/barometer/services/weather_dot_com.rb +6 -0
  29. data/lib/barometer/services/wunderground.rb +285 -0
  30. data/lib/barometer/services/yahoo.rb +274 -0
  31. data/lib/barometer/weather.rb +187 -0
  32. data/spec/barometer_spec.rb +162 -0
  33. data/spec/data_current_spec.rb +225 -0
  34. data/spec/data_distance_spec.rb +336 -0
  35. data/spec/data_forecast_spec.rb +150 -0
  36. data/spec/data_geo_spec.rb +90 -0
  37. data/spec/data_location_spec.rb +59 -0
  38. data/spec/data_measurement_spec.rb +411 -0
  39. data/spec/data_pressure_spec.rb +336 -0
  40. data/spec/data_speed_spec.rb +374 -0
  41. data/spec/data_sun_spec.rb +76 -0
  42. data/spec/data_temperature_spec.rb +396 -0
  43. data/spec/data_zone_spec.rb +133 -0
  44. data/spec/fixtures/current_calgary_ab.xml +1 -0
  45. data/spec/fixtures/forecast_calgary_ab.xml +1 -0
  46. data/spec/fixtures/geocode_40_73.xml +1 -0
  47. data/spec/fixtures/geocode_90210.xml +1 -0
  48. data/spec/fixtures/geocode_T5B4M9.xml +1 -0
  49. data/spec/fixtures/geocode_calgary_ab.xml +1 -0
  50. data/spec/fixtures/geocode_newyork_ny.xml +1 -0
  51. data/spec/fixtures/google_calgary_ab.xml +1 -0
  52. data/spec/fixtures/yahoo_90210.xml +1 -0
  53. data/spec/query_spec.rb +469 -0
  54. data/spec/service_google_spec.rb +144 -0
  55. data/spec/service_wunderground_spec.rb +330 -0
  56. data/spec/service_yahoo_spec.rb +299 -0
  57. data/spec/services_spec.rb +1106 -0
  58. data/spec/spec_helper.rb +14 -0
  59. data/spec/units_spec.rb +101 -0
  60. data/spec/weather_spec.rb +265 -0
  61. metadata +119 -0
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Sun" do
4
+
5
+ describe "when initialized" do
6
+
7
+ before(:each) do
8
+ @sun = Barometer::Sun.new
9
+ @time_rise = Time.now
10
+ @time_set = Time.now + (60*60*8)
11
+ end
12
+
13
+ it "responds to rise" do
14
+ @sun.respond_to?("rise").should be_true
15
+ end
16
+
17
+ it "responds to set" do
18
+ @sun.respond_to?("set").should be_true
19
+ end
20
+
21
+ it "sets sunrise" do
22
+ sun = Barometer::Sun.new(@time_rise,@time_set)
23
+ sun.rise.should == @time_rise
24
+ end
25
+
26
+ it "sets sunset" do
27
+ sun = Barometer::Sun.new(@time_rise,@time_set)
28
+ sun.set.should == @time_set
29
+ end
30
+
31
+ it "requires Time for sunrise" do
32
+ lambda { Barometer::Sun.new("",@time_set) }.should raise_error(ArgumentError)
33
+ lambda { Barometer::Sun.new(@time_rise,@time_set) }.should_not raise_error(ArgumentError)
34
+ end
35
+
36
+ it "requires Time for sunset" do
37
+ lambda { Barometer::Sun.new(@time_rise,"") }.should raise_error(ArgumentError)
38
+ lambda { Barometer::Sun.new(@time_rise,@time_set) }.should_not raise_error(ArgumentError)
39
+ end
40
+
41
+ it "responds to nil?" do
42
+ @sun.nil?.should be_true
43
+ sun = Barometer::Sun.new(@time_rise, @time_set)
44
+ sun.nil?.should be_false
45
+ end
46
+
47
+ end
48
+
49
+ describe "when adjusting times" do
50
+
51
+ before(:each) do
52
+ @time_rise = Time.now
53
+ @time_set = Time.now + (60*60*8)
54
+ @sun = Barometer::Sun.new(@time_rise, @time_set)
55
+ end
56
+
57
+ it "requires a Barometer::Sun object" do
58
+ lambda { Barometer::Sun.add_days!("invalid") }.should raise_error(ArgumentError)
59
+ lambda { Barometer::Sun.add_days!(@sun) }.should_not raise_error(ArgumentError)
60
+ end
61
+
62
+ it "requires a Fixnum object" do
63
+ lambda { Barometer::Sun.add_days!(@sun,1.1) }.should raise_error(ArgumentError)
64
+ lambda { Barometer::Sun.add_days!(@sun,1) }.should_not raise_error(ArgumentError)
65
+ end
66
+
67
+ it "adds X days to both sun_rise and sun_set" do
68
+ days_to_add = 2
69
+ new_sun = Barometer::Sun.add_days!(@sun, days_to_add)
70
+ new_sun.rise.should == @sun.rise + (60*60*24*days_to_add)
71
+ new_sun.set.should == @sun.set + (60*60*24*days_to_add)
72
+ end
73
+
74
+ end
75
+
76
+ end
@@ -0,0 +1,396 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Temperature" do
4
+
5
+ describe "when initialized" do
6
+
7
+ it "defines METRIC_UNITS" do
8
+ Barometer::Temperature.const_defined?("METRIC_UNITS").should be_true
9
+ Barometer::Temperature::METRIC_UNITS.should == "C"
10
+ end
11
+
12
+ it "defines IMPERIAL_UNITS" do
13
+ Barometer::Temperature.const_defined?("IMPERIAL_UNITS").should be_true
14
+ Barometer::Temperature::IMPERIAL_UNITS.should == "F"
15
+ end
16
+
17
+ before(:each) do
18
+ @temp = Barometer::Temperature.new
19
+ end
20
+
21
+ it "responds to celcius" do
22
+ @temp.celsius.should be_nil
23
+ end
24
+
25
+ it "responds to fahrenheit" do
26
+ @temp.fahrenheit.should be_nil
27
+ end
28
+
29
+ it "responds to kelvin" do
30
+ @temp.kelvin.should be_nil
31
+ end
32
+
33
+ it "responds to metric_default" do
34
+ lambda { @temp.metric_default = 5 }.should_not raise_error(NotImplementedError)
35
+ end
36
+
37
+ it "responds to imperial_default" do
38
+ lambda { @temp.imperial_default = 5 }.should_not raise_error(NotImplementedError)
39
+ end
40
+
41
+ it "responds to nil?" do
42
+ @temp.nil?.should be_true
43
+ @temp.c = 5
44
+ @temp.nil?.should be_false
45
+ end
46
+
47
+ end
48
+
49
+ describe "conversion" do
50
+
51
+ # For all conversions
52
+ # 20.0 C = 293.15 K = 68.0 F
53
+ before(:each) do
54
+ @f = 68.0
55
+ @c = 20.0
56
+ @k = 293.15
57
+ end
58
+
59
+ it "requires a value, that is either Integer or Float" do
60
+ Barometer::Temperature.c_to_k(nil).should be_nil
61
+ Barometer::Temperature.c_to_f(nil).should be_nil
62
+ Barometer::Temperature.f_to_k(nil).should be_nil
63
+ Barometer::Temperature.f_to_c(nil).should be_nil
64
+ Barometer::Temperature.k_to_c(nil).should be_nil
65
+ Barometer::Temperature.k_to_f(nil).should be_nil
66
+
67
+ not_float_or_integer = "string"
68
+ Barometer::Temperature.c_to_k(not_float_or_integer).should be_nil
69
+ Barometer::Temperature.c_to_f(not_float_or_integer).should be_nil
70
+ Barometer::Temperature.f_to_k(not_float_or_integer).should be_nil
71
+ Barometer::Temperature.f_to_c(not_float_or_integer).should be_nil
72
+ Barometer::Temperature.k_to_c(not_float_or_integer).should be_nil
73
+ Barometer::Temperature.k_to_f(not_float_or_integer).should be_nil
74
+ end
75
+
76
+ it "converts C to K" do
77
+ # 0 C = 273.15 K
78
+ Barometer::Temperature.c_to_k(@c).should == @k
79
+ end
80
+
81
+ it "converts C to F" do
82
+ # Tf = (9/5)*Tc+32
83
+ Barometer::Temperature.c_to_f(@c).should == @f
84
+ end
85
+
86
+ it "converts F to C" do
87
+ # Tc = (5/9)*(Tf-32)
88
+ Barometer::Temperature.f_to_c(@f).should == @c
89
+ end
90
+
91
+ it "converts F to K" do
92
+ Barometer::Temperature.f_to_k(@f).should == @k
93
+ end
94
+
95
+ it "converts K to C" do
96
+ Barometer::Temperature.k_to_c(@k).should == @c
97
+ end
98
+
99
+ it "converts K to F" do
100
+ Barometer::Temperature.k_to_f(@k).should == @f
101
+ end
102
+
103
+ end
104
+
105
+ describe "updating" do
106
+
107
+ before(:each) do
108
+ @temp = Barometer::Temperature.new
109
+ @f = 68.0
110
+ @c = 20.0
111
+ end
112
+
113
+ it "nils F if new C converts to a F that changes more then 1 degree" do
114
+ @temp.fahrenheit = (@f + 1.1)
115
+ @temp.update_fahrenheit(@c)
116
+ @temp.fahrenheit.should be_nil
117
+ end
118
+
119
+ it "doesn't update F if new C converts to a F that does not change more then 1 degree" do
120
+ @temp.fahrenheit = (@f + 0.9)
121
+ @temp.update_fahrenheit(@c)
122
+ @temp.fahrenheit.should == (@f + 0.9)
123
+ end
124
+
125
+ it "doesn't set F if not already set" do
126
+ @temp.fahrenheit.should be_nil
127
+ @temp.celsius.should be_nil
128
+ @temp.update_fahrenheit(@c)
129
+ @temp.fahrenheit.should be_nil
130
+ @temp.celsius.should be_nil
131
+ end
132
+
133
+ it "nils C if new F converts to a C that changes more then 1 degree" do
134
+ @temp.celsius = (@c + 1.1)
135
+ @temp.update_celsius(@f)
136
+ @temp.celsius.should be_nil
137
+ end
138
+
139
+ it "doesn't update C if new F converts to a C that does not change more then 1 degree" do
140
+ @temp.celsius = (@c + 0.9)
141
+ @temp.update_celsius(@f)
142
+ @temp.celsius.should == (@c + 0.9)
143
+ end
144
+
145
+ it "doesn't set C if not already set" do
146
+ @temp.fahrenheit.should be_nil
147
+ @temp.celsius.should be_nil
148
+ @temp.update_celsius(@f)
149
+ @temp.fahrenheit.should be_nil
150
+ @temp.celsius.should be_nil
151
+ end
152
+
153
+ end
154
+
155
+ describe "storing" do
156
+
157
+ before(:each) do
158
+ @temp = Barometer::Temperature.new
159
+ @f = 68.0
160
+ @c = 20.0
161
+ @k = 293.15
162
+ end
163
+
164
+ it "doesn't update C if nil value (or equivalent)" do
165
+ @temp.celsius.should be_nil
166
+ @temp.c = nil
167
+ @temp.celsius.should be_nil
168
+ @temp.c = "na"
169
+ @temp.celsius.should be_nil
170
+ end
171
+
172
+ it "stores C, convert to K and reset F" do
173
+ @temp.celsius.should be_nil
174
+ @temp.fahrenheit = (@f + 1.1)
175
+ @temp.kelvin.should be_nil
176
+ @temp.c = @c
177
+ @temp.celsius.should == @c
178
+ @temp.kelvin.should == @k
179
+ @temp.fahrenheit.should be_nil
180
+ end
181
+
182
+ it "doesn't update F if nil value (or equivalent)" do
183
+ @temp.fahrenheit.should be_nil
184
+ @temp.f = nil
185
+ @temp.fahrenheit.should be_nil
186
+ @temp.f = "na"
187
+ @temp.fahrenheit.should be_nil
188
+ end
189
+
190
+ it "stores F, convert to K and reset C" do
191
+ @temp.fahrenheit.should be_nil
192
+ @temp.celsius = (@c + 1.1)
193
+ @temp.kelvin.should be_nil
194
+ @temp.f = @f
195
+ @temp.fahrenheit.should == @f
196
+ @temp.kelvin.should == @k
197
+ @temp.celsius.should be_nil
198
+ end
199
+
200
+ it "doesn't update K if nil value (or equivalent)" do
201
+ @temp.kelvin.should be_nil
202
+ @temp.k = nil
203
+ @temp.kelvin.should be_nil
204
+ @temp.k = "na"
205
+ @temp.kelvin.should be_nil
206
+ end
207
+
208
+ it "stores K, convert to F and C" do
209
+ @temp.celsius.should be_nil
210
+ @temp.fahrenheit.should be_nil
211
+ @temp.kelvin.should be_nil
212
+ @temp.k = @k
213
+ @temp.celsius.should == @c
214
+ @temp.fahrenheit.should == @f
215
+ @temp.kelvin.should == @k
216
+ end
217
+
218
+ end
219
+
220
+ describe "retrieving" do
221
+
222
+ before(:each) do
223
+ @temp = Barometer::Temperature.new
224
+ @f = 68.0
225
+ @c = 20.0
226
+ @k = 293.15
227
+ end
228
+
229
+ it "returns C if it exists" do
230
+ @temp.c = @c
231
+ @temp.celsius.should == @c
232
+ @temp.c.should == @c
233
+ end
234
+
235
+ it "auto converts from K if C is nil and K exists" do
236
+ @temp.f = @f
237
+ @temp.fahrenheit.should == @f
238
+ @temp.kelvin.should == @k
239
+ @temp.celsius.should be_nil
240
+ @temp.c.should == @c
241
+ end
242
+
243
+ it "allows a float to be returned for C" do
244
+ c = 20.12
245
+ @temp.c = c
246
+ @temp.celsius.should == c
247
+ @temp.c(true).should == c.to_i
248
+ @temp.c(false).should == c.to_f
249
+ end
250
+
251
+ it "allows only 2 decimal precision for C" do
252
+ c = 20.1234
253
+ @temp.c = c
254
+ @temp.celsius.should == c
255
+ @temp.c(false).should == 20.12
256
+ end
257
+
258
+ it "returns F if it exists" do
259
+ @temp.f = @f
260
+ @temp.fahrenheit.should == @f
261
+ @temp.f.should == @f
262
+ end
263
+
264
+ it "auto converts from K if F is nil and K exists" do
265
+ @temp.c = @c
266
+ @temp.celsius.should == @c
267
+ @temp.kelvin.should == @k
268
+ @temp.fahrenheit.should be_nil
269
+ @temp.f.should == @f
270
+ end
271
+
272
+ it "allows a float to be returned for F" do
273
+ f = 68.12
274
+ @temp.f = f
275
+ @temp.fahrenheit.should == f
276
+ @temp.f(true).should == f.to_i
277
+ @temp.f(false).should == f.to_f
278
+ end
279
+
280
+ it "allows only 2 decimal precision for F" do
281
+ f = 68.1234
282
+ @temp.f = f
283
+ @temp.fahrenheit.should == f
284
+ @temp.f(false).should == 68.12
285
+ end
286
+
287
+ end
288
+
289
+ describe "operators" do
290
+
291
+ before(:each) do
292
+ @f = 68.0
293
+ @c = 20.0
294
+ @k = 293.15
295
+ @temp_low = Barometer::Temperature.new
296
+ @temp_low.k = (@k - 1.0)
297
+ @temp_high = Barometer::Temperature.new
298
+ @temp_high.k = (@k + 1.0)
299
+ @temp = Barometer::Temperature.new
300
+ @temp.k = @k
301
+ @temp_same = Barometer::Temperature.new
302
+ @temp_same.k = @k
303
+ end
304
+
305
+ it "defines <=>" do
306
+ Barometer::Temperature.method_defined?("<=>").should be_true
307
+ (@temp_low <=> @temp_high).should == -1
308
+ (@temp_high <=> @temp_low).should == 1
309
+ (@temp <=> @temp_same).should == 0
310
+ end
311
+
312
+ it "defines <" do
313
+ Barometer::Temperature.method_defined?("<").should be_true
314
+ @temp_low.should < @temp_high
315
+ @temp_high.should_not < @temp_low
316
+ @temp.should_not < @temp_same
317
+ end
318
+
319
+ it "defines >" do
320
+ Barometer::Temperature.method_defined?(">").should be_true
321
+ @temp_low.should_not > @temp_high
322
+ @temp_high.should > @temp_low
323
+ @temp.should_not > @temp_same
324
+ end
325
+
326
+ it "defines ==" do
327
+ Barometer::Temperature.method_defined?("==").should be_true
328
+ @temp_low.should_not == @temp_high
329
+ @temp.should == @temp_same
330
+ end
331
+
332
+ it "defines <=" do
333
+ Barometer::Temperature.method_defined?("<=").should be_true
334
+ @temp_low.should <= @temp_high
335
+ @temp_high.should_not <= @temp_low
336
+ @temp.should <= @temp_same
337
+ end
338
+
339
+ it "defines >=" do
340
+ Barometer::Temperature.method_defined?(">=").should be_true
341
+ @temp_low.should_not >= @temp_high
342
+ @temp_high.should >= @temp_low
343
+ @temp.should >= @temp_same
344
+ end
345
+
346
+ end
347
+
348
+ describe "changing units" do
349
+
350
+ before(:each) do
351
+ @c = 20.5
352
+ @f = Barometer::Temperature.c_to_f(@c)
353
+ @k = Barometer::Temperature.c_to_k(@c)
354
+ @temp = Barometer::Temperature.new
355
+ @temp.k = @k
356
+ end
357
+
358
+ it "returns just the integer value (no units)" do
359
+ @temp.metric?.should be_true
360
+ @temp.to_i.should == @c.to_i
361
+
362
+ @temp.imperial!
363
+ @temp.metric?.should be_false
364
+ @temp.to_i.should == @f.to_i
365
+ end
366
+
367
+ it "returns just the float value (no units)" do
368
+ @temp.metric?.should be_true
369
+ @temp.to_f.should == @c.to_f
370
+
371
+ @temp.imperial!
372
+ @temp.metric?.should be_false
373
+ @temp.to_f.should == @f.to_f
374
+ end
375
+
376
+ it "returns just the integer value with units" do
377
+ @temp.metric?.should be_true
378
+ @temp.to_s.should == "#{@c.to_i} #{Barometer::Temperature::METRIC_UNITS}"
379
+
380
+ @temp.imperial!
381
+ @temp.metric?.should be_false
382
+ @temp.to_s.should == "#{@f.to_i} #{Barometer::Temperature::IMPERIAL_UNITS}"
383
+ end
384
+
385
+ it "returns just the units" do
386
+ @temp.metric?.should be_true
387
+ @temp.units.should == Barometer::Temperature::METRIC_UNITS
388
+
389
+ @temp.imperial!
390
+ @temp.metric?.should be_false
391
+ @temp.units.should == Barometer::Temperature::IMPERIAL_UNITS
392
+ end
393
+
394
+ end
395
+
396
+ end