barometer 0.3.2 → 0.5.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.
Files changed (108) hide show
  1. data/README.rdoc +78 -70
  2. data/VERSION.yml +2 -2
  3. data/bin/barometer +100 -37
  4. data/lib/barometer.rb +12 -8
  5. data/lib/barometer/base.rb +48 -20
  6. data/lib/barometer/data.rb +5 -1
  7. data/lib/barometer/data/current.rb +23 -15
  8. data/lib/barometer/data/distance.rb +15 -5
  9. data/lib/barometer/data/forecast.rb +23 -5
  10. data/lib/barometer/data/geo.rb +16 -54
  11. data/lib/barometer/data/local_datetime.rb +137 -0
  12. data/lib/barometer/data/local_time.rb +134 -0
  13. data/lib/barometer/data/location.rb +6 -1
  14. data/lib/barometer/data/measurement.rb +71 -42
  15. data/lib/barometer/data/night.rb +69 -0
  16. data/lib/barometer/data/pressure.rb +15 -5
  17. data/lib/barometer/data/speed.rb +16 -5
  18. data/lib/barometer/data/sun.rb +8 -20
  19. data/lib/barometer/data/temperature.rb +22 -9
  20. data/lib/barometer/data/units.rb +10 -19
  21. data/lib/barometer/data/zone.rb +135 -9
  22. data/lib/barometer/formats.rb +12 -0
  23. data/lib/barometer/formats/coordinates.rb +42 -0
  24. data/lib/barometer/formats/format.rb +46 -0
  25. data/lib/barometer/formats/geocode.rb +51 -0
  26. data/lib/barometer/formats/icao.rb +37 -0
  27. data/lib/barometer/formats/postalcode.rb +22 -0
  28. data/lib/barometer/formats/short_zipcode.rb +17 -0
  29. data/lib/barometer/formats/weather_id.rb +107 -0
  30. data/lib/barometer/formats/zipcode.rb +31 -0
  31. data/lib/barometer/query.rb +61 -232
  32. data/lib/barometer/services.rb +14 -4
  33. data/lib/barometer/translations/icao_country_codes.yml +9 -0
  34. data/lib/barometer/translations/weather_country_codes.yml +17 -0
  35. data/lib/barometer/weather.rb +51 -30
  36. data/lib/barometer/{services → weather_services}/google.rb +23 -26
  37. data/lib/barometer/weather_services/noaa.rb +6 -0
  38. data/lib/barometer/{services → weather_services}/service.rb +101 -92
  39. data/lib/barometer/weather_services/weather_bug.rb +6 -0
  40. data/lib/barometer/weather_services/weather_dot_com.rb +261 -0
  41. data/lib/barometer/{services → weather_services}/wunderground.rb +58 -76
  42. data/lib/barometer/{services → weather_services}/yahoo.rb +91 -121
  43. data/lib/barometer/web_services/geocode.rb +33 -0
  44. data/lib/barometer/web_services/weather_id.rb +37 -0
  45. data/lib/barometer/web_services/web_service.rb +32 -0
  46. data/lib/demometer/demometer.rb +31 -4
  47. data/lib/demometer/views/forecast.erb +20 -0
  48. data/lib/demometer/views/index.erb +10 -3
  49. data/lib/demometer/views/measurement.erb +8 -3
  50. data/lib/demometer/views/readme.erb +63 -24
  51. data/spec/barometer_spec.rb +18 -36
  52. data/spec/{data_current_spec.rb → data/current_spec.rb} +73 -49
  53. data/spec/{data_distance_spec.rb → data/distance_spec.rb} +30 -30
  54. data/spec/{data_forecast_spec.rb → data/forecast_spec.rb} +57 -15
  55. data/spec/data/geo_spec.rb +91 -0
  56. data/spec/data/local_datetime_spec.rb +269 -0
  57. data/spec/data/local_time_spec.rb +239 -0
  58. data/spec/{data_location_spec.rb → data/location_spec.rb} +12 -1
  59. data/spec/{data_measurement_spec.rb → data/measurement_spec.rb} +135 -66
  60. data/spec/data/night_measurement_spec.rb +136 -0
  61. data/spec/{data_pressure_spec.rb → data/pressure_spec.rb} +29 -29
  62. data/spec/{data_speed_spec.rb → data/speed_spec.rb} +30 -30
  63. data/spec/data/sun_spec.rb +49 -0
  64. data/spec/{data_temperature_spec.rb → data/temperature_spec.rb} +44 -44
  65. data/spec/{units_spec.rb → data/units_spec.rb} +6 -6
  66. data/spec/{data_zone_spec.rb → data/zone_spec.rb} +15 -15
  67. data/spec/fixtures/formats/weather_id/90210.xml +1 -0
  68. data/spec/fixtures/formats/weather_id/atlanta.xml +1 -0
  69. data/spec/fixtures/formats/weather_id/from_USGA0028.xml +1 -0
  70. data/spec/fixtures/formats/weather_id/new_york.xml +1 -0
  71. data/spec/fixtures/{geocode_40_73.xml → geocode/40_73.xml} +0 -0
  72. data/spec/fixtures/{geocode_90210.xml → geocode/90210.xml} +0 -0
  73. data/spec/fixtures/{geocode_T5B4M9.xml → geocode/T5B4M9.xml} +0 -0
  74. data/spec/fixtures/geocode/atlanta.xml +1 -0
  75. data/spec/fixtures/{geocode_calgary_ab.xml → geocode/calgary_ab.xml} +0 -0
  76. data/spec/fixtures/{geocode_ksfo.xml → geocode/ksfo.xml} +0 -0
  77. data/spec/fixtures/{geocode_newyork_ny.xml → geocode/newyork_ny.xml} +0 -0
  78. data/spec/fixtures/{google_calgary_ab.xml → services/google/calgary_ab.xml} +0 -0
  79. data/spec/fixtures/services/weather_dot_com/90210.xml +1 -0
  80. data/spec/fixtures/{current_calgary_ab.xml → services/wunderground/current_calgary_ab.xml} +0 -0
  81. data/spec/fixtures/{forecast_calgary_ab.xml → services/wunderground/forecast_calgary_ab.xml} +0 -0
  82. data/spec/fixtures/{yahoo_90210.xml → services/yahoo/90210.xml} +0 -0
  83. data/spec/formats/coordinates_spec.rb +158 -0
  84. data/spec/formats/format_spec.rb +73 -0
  85. data/spec/formats/geocode_spec.rb +179 -0
  86. data/spec/formats/icao_spec.rb +61 -0
  87. data/spec/formats/postalcode_spec.rb +59 -0
  88. data/spec/formats/short_zipcode_spec.rb +53 -0
  89. data/spec/formats/weather_id_spec.rb +191 -0
  90. data/spec/formats/zipcode_spec.rb +111 -0
  91. data/spec/query_spec.rb +261 -288
  92. data/spec/spec_helper.rb +128 -4
  93. data/spec/{service_google_spec.rb → weather_services/google_spec.rb} +46 -46
  94. data/spec/weather_services/services_spec.rb +1118 -0
  95. data/spec/weather_services/weather_dot_com_spec.rb +327 -0
  96. data/spec/weather_services/wunderground_spec.rb +332 -0
  97. data/spec/{service_yahoo_spec.rb → weather_services/yahoo_spec.rb} +65 -81
  98. data/spec/weather_spec.rb +73 -61
  99. data/spec/web_services/geocode_spec.rb +45 -0
  100. data/spec/web_services/web_services_spec.rb +26 -0
  101. metadata +88 -36
  102. data/lib/barometer/services/noaa.rb +0 -6
  103. data/lib/barometer/services/weather_bug.rb +0 -6
  104. data/lib/barometer/services/weather_dot_com.rb +0 -6
  105. data/spec/data_geo_spec.rb +0 -94
  106. data/spec/data_sun_spec.rb +0 -76
  107. data/spec/service_wunderground_spec.rb +0 -330
  108. data/spec/services_spec.rb +0 -1106
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Data::Geo" do
4
+
5
+ describe "when initialized" do
6
+
7
+ before(:each) do
8
+ @geo = Data::Geo.new
9
+ end
10
+
11
+ it "responds to query" do
12
+ @geo.query.should be_nil
13
+ end
14
+
15
+ it "responds to latitude" do
16
+ @geo.latitude.should be_nil
17
+ end
18
+
19
+ it "responds to longitude" do
20
+ @geo.longitude.should be_nil
21
+ end
22
+
23
+ it "responds to country_code" do
24
+ @geo.country_code.should be_nil
25
+ end
26
+
27
+ it "responds to locality" do
28
+ @geo.locality.should be_nil
29
+ end
30
+
31
+ it "responds to region" do
32
+ @geo.region.should be_nil
33
+ end
34
+
35
+ it "responds to country" do
36
+ @geo.country.should be_nil
37
+ end
38
+
39
+ it "responds to address" do
40
+ @geo.address.should be_nil
41
+ end
42
+
43
+ it "responds to coordinates" do
44
+ @geo.longitude = "99.99"
45
+ @geo.latitude = "88.88"
46
+ @geo.coordinates.should == [@geo.latitude, @geo.longitude].join(',')
47
+ end
48
+
49
+ it "should print a string" do
50
+ @geo = Data::Geo.new
51
+ @geo.to_s.should == ""
52
+ @geo.address = "address"
53
+ @geo.to_s.should == "address"
54
+ @geo.locality = "locality"
55
+ @geo.to_s.should == "address, locality"
56
+ @geo.country_code = "code"
57
+ @geo.to_s.should == "address, locality, code"
58
+ end
59
+
60
+ it "requires Hash object" do
61
+ lambda { Data::Geo.new(1) }.should raise_error(ArgumentError)
62
+ lambda { Data::Geo.new(Hash.new) }.should_not raise_error(ArgumentError)
63
+ end
64
+
65
+ it "returns a Barometer::Geo object" do
66
+ geo = Data::Geo.new(Hash.new)
67
+ geo.is_a?(Data::Geo).should be_true
68
+ end
69
+
70
+ end
71
+
72
+ describe "when converting" do
73
+
74
+ before(:each) do
75
+ @geo = Data::Geo.new
76
+ end
77
+
78
+ describe "from HTTParty" do
79
+
80
+ it "accepts HTTParty::Response object" do
81
+ location = Hash.new
82
+ lambda { @geo.build_from_hash(1) }.should raise_error(ArgumentError)
83
+ lambda { @geo.build_from_hash }.should_not raise_error(ArgumentError)
84
+ lambda { @geo.build_from_hash(location) }.should_not raise_error(ArgumentError)
85
+ end
86
+
87
+ end
88
+
89
+ end
90
+
91
+ end
@@ -0,0 +1,269 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Data::LocalDateTime" do
4
+
5
+ before(:each) do
6
+ @y = 2009
7
+ @mon = 5
8
+ @d = 1
9
+ @h = 12
10
+ @m = 11
11
+ @s = 10
12
+ end
13
+
14
+ describe "when initialized" do
15
+
16
+ before(:each) do
17
+ @datetime = Data::LocalDateTime.new(@y,@mon,@d)
18
+ end
19
+
20
+ it "responds to hour" do
21
+ @datetime.hour.should == 0
22
+ end
23
+
24
+ it "responds to min" do
25
+ @datetime.min.should == 0
26
+ end
27
+
28
+ it "responds to sec" do
29
+ @datetime.sec.should == 0
30
+ end
31
+
32
+ it "responds to year" do
33
+ @datetime.year.should == @y
34
+ end
35
+
36
+ it "responds to month" do
37
+ @datetime.month.should == @mon
38
+ end
39
+
40
+ it "responds to day" do
41
+ @datetime.day.should == @d
42
+ end
43
+
44
+ end
45
+
46
+ describe "conversion" do
47
+
48
+ before(:each) do
49
+ @datetime = Data::LocalDateTime.new(@y,@mon,@d,@h,@m,@s)
50
+ end
51
+
52
+ it "converts to a DateTime object" do
53
+ @datetime.is_a?(Data::LocalDateTime)
54
+ datetime = @datetime.to_dt
55
+ datetime.is_a?(DateTime).should be_true
56
+ datetime.year.should == @y
57
+ datetime.mon.should == @mon
58
+ datetime.day.should == @d
59
+ datetime.hour.should == @h
60
+ datetime.min.should == @m
61
+ datetime.sec.should == @s
62
+ end
63
+
64
+ it "converts to a Date object" do
65
+ @datetime.is_a?(Data::LocalDateTime)
66
+ date = @datetime.to_d
67
+ date.is_a?(Date).should be_true
68
+ date.year.should == @y
69
+ date.mon.should == @mon
70
+ date.day.should == @d
71
+ end
72
+
73
+ it "converts to a Time object" do
74
+ @datetime.is_a?(Data::LocalDateTime)
75
+ time = @datetime.to_t
76
+ time.is_a?(Time).should be_true
77
+ time.hour.should == @h
78
+ time.min.should == @m
79
+ time.sec.should == @s
80
+ end
81
+
82
+ end
83
+
84
+ describe "parsing" do
85
+
86
+ before(:each) do
87
+ @y = 2009
88
+ @mon = 5
89
+ @d = 1
90
+ @h = 12
91
+ @m = 11
92
+ @s = 10
93
+ @datetime = Data::LocalDateTime.new(@y,@mon,@d,@h,@m,@s)
94
+ end
95
+
96
+ it "parses a Time object" do
97
+ time = Time.local(@y,@mon,@d,@h,@m,@s)
98
+ @datetime.parse(time)
99
+ @datetime.year.should == @y
100
+ @datetime.month.should == @mon
101
+ @datetime.day.should == @d
102
+ @datetime.hour.should == @h
103
+ @datetime.min.should == @m
104
+ @datetime.sec.should == @s
105
+ end
106
+
107
+ it "parses a DateTime object" do
108
+ datetime = DateTime.new(@y,@mon,@d,@h,@m,@s)
109
+ @datetime.parse(datetime)
110
+ @datetime.year.should == @y
111
+ @datetime.month.should == @mon
112
+ @datetime.day.should == @d
113
+ @datetime.hour.should == @h
114
+ @datetime.min.should == @m
115
+ @datetime.sec.should == @s
116
+ end
117
+
118
+ it "parses a Date object" do
119
+ date = Date.civil(@y,@mon,@d)
120
+ @datetime.parse(date)
121
+ @datetime.year.should == @y
122
+ @datetime.month.should == @mon
123
+ @datetime.day.should == @d
124
+ end
125
+
126
+ it "parses a String" do
127
+ string = "#{@y}-#{@mon}-#{@d} #{@h}:#{@m}:#{@s}"
128
+ @datetime.parse(string)
129
+ @datetime.year.should == @y
130
+ @datetime.month.should == @mon
131
+ @datetime.day.should == @d
132
+ @datetime.hour.should == @h
133
+ @datetime.min.should == @m
134
+ @datetime.sec.should == @s
135
+ end
136
+
137
+ it "parses a String (using class method)" do
138
+ string = "#{@y}-#{@mon}-#{@d} #{@h}:#{@m}:#{@s}"
139
+ datetime = Data::LocalDateTime.parse(string)
140
+ datetime.year.should == @y
141
+ datetime.month.should == @mon
142
+ datetime.day.should == @d
143
+ datetime.hour.should == @h
144
+ datetime.min.should == @m
145
+ datetime.sec.should == @s
146
+ end
147
+
148
+ end
149
+
150
+ describe "storing" do
151
+
152
+ before(:each) do
153
+ @datetime = Data::LocalDateTime.new(@y,@mon,@d,@h,@m,@s)
154
+ end
155
+
156
+ it "requires Fixnum (accepts nil)" do
157
+ invalid_data = "s"
158
+ valid_data = 1
159
+ lambda { @datetime.year = invalid_data }.should raise_error(ArgumentError)
160
+ lambda { @datetime.year = valid_data }.should_not raise_error(ArgumentError)
161
+ lambda { @datetime.year = nil }.should raise_error(ArgumentError)
162
+ lambda { @datetime.month = invalid_data }.should raise_error(ArgumentError)
163
+ lambda { @datetime.month = valid_data }.should_not raise_error(ArgumentError)
164
+ lambda { @datetime.month = nil }.should raise_error(ArgumentError)
165
+ lambda { @datetime.day = invalid_data }.should raise_error(ArgumentError)
166
+ lambda { @datetime.day = valid_data }.should_not raise_error(ArgumentError)
167
+ lambda { @datetime.day = nil }.should raise_error(ArgumentError)
168
+ end
169
+
170
+ it "rejects invalid dates during init" do
171
+ lambda { Data::LocalDateTime.new(2009,0,1) }.should raise_error(ArgumentError)
172
+ lambda { Data::LocalDateTime.new(2009,1,0) }.should raise_error(ArgumentError)
173
+ lambda { Data::LocalDateTime.new(2009,13,1) }.should raise_error(ArgumentError)
174
+ lambda { Data::LocalDateTime.new(2009,1,32) }.should raise_error(ArgumentError)
175
+ end
176
+
177
+ it "rejects invalid days" do
178
+ lambda { @datetime.day = nil }.should raise_error(ArgumentError)
179
+ lambda { @datetime.day = 32 }.should raise_error(ArgumentError)
180
+ lambda { @datetime.day = "a" }.should raise_error(ArgumentError)
181
+ lambda { @datetime.day = 0 }.should raise_error(ArgumentError)
182
+ end
183
+
184
+ it "rejects invalid months" do
185
+ lambda { @datetime.month = nil }.should raise_error(ArgumentError)
186
+ lambda { @datetime.month = 32 }.should raise_error(ArgumentError)
187
+ lambda { @datetime.month = "a" }.should raise_error(ArgumentError)
188
+ lambda { @datetime.month = 0 }.should raise_error(ArgumentError)
189
+ end
190
+
191
+ it "rejects invalid years" do
192
+ lambda { @datetime.year = nil }.should raise_error(ArgumentError)
193
+ lambda { @datetime.year = "a" }.should raise_error(ArgumentError)
194
+ end
195
+
196
+ end
197
+
198
+ describe "retrieving" do
199
+
200
+ before(:each) do
201
+ @datetime = Data::LocalDateTime.new(@y,@mon,@d,@h,@m,@s)
202
+ end
203
+
204
+ it "returns pretty string" do
205
+ @datetime.to_s.should == "2009-05-01"
206
+ @datetime.to_s(true).should == "2009-05-01 12:11:10 pm"
207
+ end
208
+
209
+ end
210
+
211
+ describe "comparators" do
212
+
213
+ before(:each) do
214
+ @datetime_low = Data::LocalDateTime.new(2009,5,4,0,0,1)
215
+ @datetime_mid = Data::LocalDateTime.new(2009,5,5,12,0,0)
216
+ @datetime_high = Data::LocalDateTime.new(2009,5,6,23,59,59)
217
+ end
218
+
219
+ it "counts days" do
220
+ Data::LocalDateTime.new(0,1,1,0,0,0)._total_days.should == 1
221
+ Data::LocalDateTime.new(0,2,1,0,0,0)._total_days.should == 32
222
+ Data::LocalDateTime.new(1,1,1,0,0,0)._total_days.should == 367
223
+ end
224
+
225
+ it "defines <=>" do
226
+ Data::LocalDateTime.method_defined?("<=>").should be_true
227
+ (@datetime_low <=> @datetime_high).should == -1
228
+ (@datetime_high <=> @datetime_low).should == 1
229
+ (@datetime_mid <=> @datetime_mid).should == 0
230
+ end
231
+
232
+ it "compares to a Time object" do
233
+ time = Time.local(2009,5,5,12,0,0)
234
+ (@datetime_low <=> time).should == -1
235
+ (@datetime_high <=> time).should == 1
236
+ (@datetime_mid <=> time).should == 0
237
+ end
238
+
239
+ it "compares to a DateTime object" do
240
+ time = DateTime.new(2009,5,5,12,0,0)
241
+ (@datetime_low <=> time).should == -1
242
+ (@datetime_high <=> time).should == 1
243
+ (@datetime_mid <=> time).should == 0
244
+ end
245
+
246
+ it "compares to a Date object" do
247
+ date = Date.civil(2009,5,5)
248
+ (@datetime_low <=> date).should == -1
249
+ (@datetime_high <=> date).should == 1
250
+ (@datetime_mid <=> date).should == 0
251
+ end
252
+
253
+ it "compares to a String object" do
254
+ time = "2009-5-5 12:00:00"
255
+ (@datetime_low <=> time).should == -1
256
+ (@datetime_high <=> time).should == 1
257
+ (@datetime_mid <=> time).should == 0
258
+ end
259
+
260
+ it "compares to a Data::LocalTime object" do
261
+ local_time = Data::LocalTime.new(12,0,0)
262
+ (@datetime_low <=> local_time).should == -1
263
+ (@datetime_high <=> local_time).should == 1
264
+ (@datetime_mid <=> local_time).should == 0
265
+ end
266
+
267
+ end
268
+
269
+ end
@@ -0,0 +1,239 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Data::LocalTime" do
4
+
5
+ describe "when initialized" do
6
+
7
+ before(:each) do
8
+ @time = Data::LocalTime.new
9
+ end
10
+
11
+ it "responds to hour" do
12
+ @time.hour.should == 0
13
+ end
14
+
15
+ it "responds to min" do
16
+ @time.min.should == 0
17
+ end
18
+
19
+ it "responds to sec" do
20
+ @time.sec.should == 0
21
+ end
22
+
23
+ end
24
+
25
+ describe "conversion" do
26
+
27
+ before(:each) do
28
+ @h = 12
29
+ @m = 11
30
+ @s = 10
31
+ @time = Data::LocalTime.new(@h,@m,@s)
32
+ end
33
+
34
+ it "converts to a Time object" do
35
+ @time.is_a?(Data::LocalTime)
36
+ @time.to_t.is_a?(Time).should be_true
37
+ @time.hour.should == @h
38
+ @time.min.should == @m
39
+ @time.sec.should == @s
40
+ end
41
+
42
+ end
43
+
44
+ describe "parsing" do
45
+
46
+ before(:each) do
47
+ @h = 12
48
+ @m = 11
49
+ @s = 10
50
+ @time = Data::LocalTime.new(@h,@m,@s)
51
+ end
52
+
53
+ it "parses a Time object" do
54
+ time = Time.local(2009,1,1,@h,@m,@s)
55
+ @time.parse(time)
56
+ @time.hour.should == @h
57
+ @time.min.should == @m
58
+ @time.sec.should == @s
59
+ end
60
+
61
+ it "parses a DateTime object" do
62
+ time = DateTime.new(2009,1,1,@h,@m,@s)
63
+ @time.parse(time)
64
+ @time.hour.should == @h
65
+ @time.min.should == @m
66
+ @time.sec.should == @s
67
+ end
68
+
69
+ it "parses a String" do
70
+ time = "#{@h}:#{@m}:#{@s}"
71
+ @time.parse(time)
72
+ @time.hour.should == @h
73
+ @time.min.should == @m
74
+ @time.sec.should == @s
75
+ end
76
+
77
+ it "parses a String (using class method)" do
78
+ time = "#{@h}:#{@m}:#{@s}"
79
+ time = Data::LocalTime.parse(time)
80
+ time.hour.should == @h
81
+ time.min.should == @m
82
+ time.sec.should == @s
83
+ end
84
+
85
+ end
86
+
87
+ describe "storing" do
88
+
89
+ before(:each) do
90
+ @h = 12
91
+ @m = 11
92
+ @s = 10
93
+ @time = Data::LocalTime.new(@h,@m,@s)
94
+ end
95
+
96
+ it "requires Fixnum (accepts nil)" do
97
+ invalid_data = "s"
98
+ valid_data = 1
99
+ lambda { @time.hour = invalid_data }.should raise_error(ArgumentError)
100
+ lambda { @time.hour = valid_data }.should_not raise_error(ArgumentError)
101
+ lambda { @time.hour = nil }.should_not raise_error(ArgumentError)
102
+ lambda { @time.min = invalid_data }.should raise_error(ArgumentError)
103
+ lambda { @time.min = valid_data }.should_not raise_error(ArgumentError)
104
+ lambda { @time.min = nil }.should_not raise_error(ArgumentError)
105
+ lambda { @time.sec = invalid_data }.should raise_error(ArgumentError)
106
+ lambda { @time.sec = valid_data }.should_not raise_error(ArgumentError)
107
+ lambda { @time.sec = nil }.should_not raise_error(ArgumentError)
108
+ end
109
+
110
+ it "rolls over seconds" do
111
+ time = Data::LocalTime.new(0,0,60)
112
+ time.sec.should == 0
113
+ time.min.should == 1
114
+ end
115
+
116
+ it "rolls over minutes" do
117
+ time = Data::LocalTime.new(0,60,0)
118
+ time.min.should == 0
119
+ time.hour.should == 1
120
+ end
121
+
122
+ it "rolls over hours" do
123
+ time = Data::LocalTime.new(24,0,0)
124
+ time.sec.should == 0
125
+ time.min.should == 0
126
+ time.hour.should == 0
127
+ end
128
+
129
+ it "rolls over everything" do
130
+ time = Data::LocalTime.new(50,600,601)
131
+ time.sec.should == 1
132
+ time.min.should == 10
133
+ time.hour.should == 12
134
+ end
135
+
136
+ it "add seconds" do
137
+ time = Data::LocalTime.new(0,0,0)
138
+ time = time + 61
139
+ time.sec.should == 1
140
+ time.min.should == 1
141
+ end
142
+
143
+ end
144
+
145
+ describe "retrieving" do
146
+
147
+ before(:each) do
148
+ @h = 6
149
+ @m = 30
150
+ @s = 20
151
+ @time = Data::LocalTime.new(@h,@m,@s)
152
+ end
153
+
154
+ it "returns pretty string" do
155
+ @time.to_s.should == "06:30 am"
156
+ @time.to_s(true).should == "06:30:20 am"
157
+ end
158
+
159
+ end
160
+
161
+ describe "comparators" do
162
+
163
+ before(:each) do
164
+ @time_low = Data::LocalTime.new(0,0,1)
165
+ @time_mid = Data::LocalTime.new(12,0,0)
166
+ @time_high = Data::LocalTime.new(23,59,59)
167
+ end
168
+
169
+ it "defines <=>" do
170
+ Data::LocalTime.method_defined?("<=>").should be_true
171
+ (@time_low <=> @time_high).should == -1
172
+ (@time_high <=> @time_low).should == 1
173
+ (@time_mid <=> @time_mid).should == 0
174
+ end
175
+
176
+ it "compares to a Time object" do
177
+ time = Time.local(2009,1,1,12,0,0)
178
+ (@time_low <=> time).should == -1
179
+ (@time_high <=> time).should == 1
180
+ (@time_mid <=> time).should == 0
181
+ end
182
+
183
+ it "compares to a DateTime object" do
184
+ time = DateTime.new(2009,1,1,12,0,0)
185
+ (@time_low <=> time).should == -1
186
+ (@time_high <=> time).should == 1
187
+ (@time_mid <=> time).should == 0
188
+ end
189
+
190
+ it "compares to a String object" do
191
+ time = "12:00:00"
192
+ (@time_low <=> time).should == -1
193
+ (@time_high <=> time).should == 1
194
+ (@time_mid <=> time).should == 0
195
+ end
196
+
197
+ end
198
+
199
+ describe "math" do
200
+
201
+ it "counts the total seconds" do
202
+ Data::LocalTime.new(0,0,1).total_seconds.should == 1
203
+ Data::LocalTime.new(0,1,0).total_seconds.should == (1*60)
204
+ Data::LocalTime.new(1,0,0).total_seconds.should == (1*60*60)
205
+ end
206
+
207
+ it "provides a difference" do
208
+ a = Data::LocalTime.new(0,0,1)
209
+ b = Data::LocalTime.new(1,0,0)
210
+ diff = (1*60*60) - 1
211
+ a.diff(b).should == diff
212
+ b.diff(a).should == diff
213
+ end
214
+
215
+ it "adds time" do
216
+ a = Data::LocalTime.new(0,0,1)
217
+ b = Data::LocalTime.new(1,0,0)
218
+ c = (a + b)
219
+ c.is_a?(Data::LocalTime).should be_true
220
+ c.should == Data::LocalTime.new(1,0,1)
221
+
222
+ b = Data::LocalTime.new(1,0,0)
223
+ (b + 1).should == Data::LocalTime.new(1,0,1)
224
+ end
225
+
226
+ it "subtracts time" do
227
+ a = Data::LocalTime.new(0,0,1)
228
+ b = Data::LocalTime.new(1,0,0)
229
+ c = (b - a)
230
+ c.is_a?(Data::LocalTime).should be_true
231
+ c.should == Data::LocalTime.new(0,59,59)
232
+
233
+ b = Data::LocalTime.new(1,0,0)
234
+ (b - 1).should == Data::LocalTime.new(0,59,59)
235
+ end
236
+
237
+ end
238
+
239
+ end