barometer 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (321) hide show
  1. data/.gitignore +2 -0
  2. data/.pelusa.yml +7 -0
  3. data/.travis.yml +6 -3
  4. data/Gemfile +11 -1
  5. data/README.md +89 -244
  6. data/Rakefile +1 -1
  7. data/barometer.gemspec +22 -24
  8. data/lib/barometer.rb +14 -65
  9. data/lib/barometer/base.rb +35 -65
  10. data/lib/barometer/data.rb +15 -20
  11. data/lib/barometer/data/attribute.rb +19 -0
  12. data/lib/barometer/data/attributes/distance.rb +11 -0
  13. data/lib/barometer/data/attributes/float.rb +11 -0
  14. data/lib/barometer/data/attributes/integer.rb +11 -0
  15. data/lib/barometer/data/attributes/location.rb +11 -0
  16. data/lib/barometer/data/attributes/pressure.rb +11 -0
  17. data/lib/barometer/data/attributes/sun.rb +15 -0
  18. data/lib/barometer/data/attributes/temperature.rb +11 -0
  19. data/lib/barometer/data/attributes/time.rb +11 -0
  20. data/lib/barometer/data/attributes/vector.rb +11 -0
  21. data/lib/barometer/data/attributes/zone.rb +11 -0
  22. data/lib/barometer/data/convertable_units.rb +145 -0
  23. data/lib/barometer/data/coordinates.rb +16 -0
  24. data/lib/barometer/data/distance.rb +14 -133
  25. data/lib/barometer/data/geo.rb +22 -59
  26. data/lib/barometer/data/location.rb +14 -20
  27. data/lib/barometer/data/pressure.rb +14 -135
  28. data/lib/barometer/data/sun.rb +24 -24
  29. data/lib/barometer/data/temperature.rb +14 -169
  30. data/lib/barometer/data/vector.rb +44 -0
  31. data/lib/barometer/data/zone.rb +109 -112
  32. data/lib/barometer/query.rb +23 -161
  33. data/lib/barometer/query/base.rb +88 -0
  34. data/lib/barometer/query/converter.rb +64 -0
  35. data/lib/barometer/query/converters/from_coordinates_to_noaa_station_id.rb +30 -0
  36. data/lib/barometer/query/converters/from_geocode_to_coordinates.rb +28 -0
  37. data/lib/barometer/query/converters/from_short_zipcode_to_zipcode.rb +28 -0
  38. data/lib/barometer/query/converters/from_weather_id_to_geocode.rb +30 -0
  39. data/lib/barometer/query/converters/from_woe_id_or_ipv4_to_geocode.rb +32 -0
  40. data/lib/barometer/query/converters/to_geocode.rb +30 -0
  41. data/lib/barometer/query/converters/to_weather_id.rb +30 -0
  42. data/lib/barometer/query/converters/to_woe_id.rb +30 -0
  43. data/lib/barometer/query/format.rb +59 -0
  44. data/lib/barometer/query/formats/base.rb +22 -0
  45. data/lib/barometer/query/formats/coordinates.rb +14 -0
  46. data/lib/barometer/query/formats/geocode.rb +15 -0
  47. data/lib/barometer/query/formats/icao.rb +35 -0
  48. data/lib/barometer/query/formats/ipv4_address.rb +19 -0
  49. data/lib/barometer/query/formats/noaa_station_id.rb +15 -0
  50. data/lib/barometer/query/formats/postalcode.rb +20 -0
  51. data/lib/barometer/query/formats/short_zipcode.rb +15 -0
  52. data/lib/barometer/{translations → query/formats/translations}/icao_country_codes.yml +0 -0
  53. data/lib/barometer/{translations → query/formats/translations}/weather_country_codes.yml +0 -0
  54. data/lib/barometer/query/formats/unknown.rb +14 -0
  55. data/lib/barometer/query/formats/weather_id.rb +33 -0
  56. data/lib/barometer/query/formats/woe_id.rb +28 -0
  57. data/lib/barometer/query/formats/zipcode.rb +15 -0
  58. data/lib/barometer/query/service.rb +13 -0
  59. data/lib/barometer/query/services/apis/geonames_timezone.rb +26 -0
  60. data/lib/barometer/query/services/apis/google_geocode.rb +35 -0
  61. data/lib/barometer/query/services/apis/noaa_station.rb +31 -0
  62. data/lib/barometer/query/services/apis/weather_id.rb +35 -0
  63. data/lib/barometer/query/services/apis/wunderground_timezone.rb +26 -0
  64. data/lib/barometer/query/services/apis/yahoo_placefinder.rb +35 -0
  65. data/lib/barometer/query/services/apis/yahoo_weather.rb +31 -0
  66. data/lib/barometer/query/services/from_weather_id.rb +64 -0
  67. data/lib/barometer/query/services/geonames_timezone.rb +18 -0
  68. data/lib/barometer/query/services/google_geocode.rb +106 -0
  69. data/lib/barometer/query/services/noaa_station_id.rb +28 -0
  70. data/lib/barometer/query/services/to_weather_id.rb +25 -0
  71. data/lib/barometer/query/services/to_woe_id.rb +29 -0
  72. data/lib/barometer/query/services/wunderground_timezone.rb +18 -0
  73. data/lib/barometer/query/services/yahoo_geocode.rb +69 -0
  74. data/lib/barometer/response.rb +12 -0
  75. data/lib/barometer/response/base.rb +57 -0
  76. data/lib/barometer/response/current.rb +27 -0
  77. data/lib/barometer/response/prediction.rb +41 -0
  78. data/lib/barometer/response/prediction_collection.rb +48 -0
  79. data/lib/barometer/utils.rb +17 -0
  80. data/lib/barometer/utils/address.rb +33 -0
  81. data/lib/barometer/utils/api.rb +30 -0
  82. data/lib/barometer/utils/config_reader.rb +40 -0
  83. data/lib/barometer/utils/get.rb +17 -0
  84. data/lib/barometer/utils/json_reader.rb +22 -0
  85. data/lib/barometer/utils/payload.rb +100 -0
  86. data/lib/barometer/utils/payload_request.rb +37 -0
  87. data/lib/barometer/utils/post.rb +24 -0
  88. data/lib/barometer/utils/time.rb +78 -0
  89. data/lib/barometer/{translations → utils/translations}/zone_codes.yml +0 -0
  90. data/lib/barometer/utils/versioned_registration.rb +70 -0
  91. data/lib/barometer/utils/xml_reader.rb +27 -0
  92. data/lib/barometer/utils/zone_code_lookup.rb +30 -0
  93. data/lib/barometer/version.rb +1 -1
  94. data/lib/barometer/weather.rb +44 -173
  95. data/lib/barometer/weather_service.rb +41 -0
  96. data/lib/barometer/weather_services/base.rb +50 -0
  97. data/lib/barometer/weather_services/forecast_io.rb +36 -0
  98. data/lib/barometer/weather_services/forecast_io/api.rb +22 -0
  99. data/lib/barometer/weather_services/forecast_io/query.rb +38 -0
  100. data/lib/barometer/weather_services/forecast_io/response.rb +31 -0
  101. data/lib/barometer/weather_services/forecast_io/response/current_weather.rb +78 -0
  102. data/lib/barometer/weather_services/forecast_io/response/forecasted_weather.rb +74 -0
  103. data/lib/barometer/weather_services/forecast_io/response/location.rb +19 -0
  104. data/lib/barometer/weather_services/forecast_io/response/timezone.rb +25 -0
  105. data/lib/barometer/weather_services/noaa.rb +21 -305
  106. data/lib/barometer/weather_services/noaa/current_api.rb +25 -0
  107. data/lib/barometer/weather_services/noaa/current_query.rb +30 -0
  108. data/lib/barometer/weather_services/noaa/current_response.rb +29 -0
  109. data/lib/barometer/weather_services/noaa/forecast_api.rb +25 -0
  110. data/lib/barometer/weather_services/noaa/forecast_query.rb +39 -0
  111. data/lib/barometer/weather_services/noaa/forecast_response.rb +28 -0
  112. data/lib/barometer/weather_services/noaa/response/current_location.rb +42 -0
  113. data/lib/barometer/weather_services/noaa/response/current_station.rb +46 -0
  114. data/lib/barometer/weather_services/noaa/response/current_weather.rb +82 -0
  115. data/lib/barometer/weather_services/noaa/response/forecasted_weather.rb +90 -0
  116. data/lib/barometer/weather_services/noaa/response/location.rb +19 -0
  117. data/lib/barometer/weather_services/noaa/response/timezone.rb +15 -0
  118. data/lib/barometer/weather_services/response.rb +9 -0
  119. data/lib/barometer/weather_services/response/location.rb +42 -0
  120. data/lib/barometer/weather_services/response/time_zone.rb +19 -0
  121. data/lib/barometer/weather_services/weather_bug.rb +24 -280
  122. data/lib/barometer/weather_services/weather_bug/current_api.rb +26 -0
  123. data/lib/barometer/weather_services/weather_bug/current_response.rb +33 -0
  124. data/lib/barometer/weather_services/weather_bug/forecast_api.rb +26 -0
  125. data/lib/barometer/weather_services/weather_bug/forecast_response.rb +29 -0
  126. data/lib/barometer/weather_services/weather_bug/query.rb +42 -0
  127. data/lib/barometer/weather_services/weather_bug/response/current_weather.rb +82 -0
  128. data/lib/barometer/weather_services/weather_bug/response/forecasted_weather.rb +67 -0
  129. data/lib/barometer/weather_services/weather_bug/response/location.rb +23 -0
  130. data/lib/barometer/weather_services/weather_bug/response/station.rb +43 -0
  131. data/lib/barometer/weather_services/weather_bug/response/sun.rb +32 -0
  132. data/lib/barometer/weather_services/weather_bug/response/time_helper.rb +52 -0
  133. data/lib/barometer/weather_services/weather_bug/response/timezone.rb +15 -0
  134. data/lib/barometer/weather_services/wunderground_v1.rb +32 -0
  135. data/lib/barometer/weather_services/wunderground_v1/current_api.rb +21 -0
  136. data/lib/barometer/weather_services/wunderground_v1/current_response.rb +31 -0
  137. data/lib/barometer/weather_services/wunderground_v1/forecast_api.rb +21 -0
  138. data/lib/barometer/weather_services/wunderground_v1/forecast_response.rb +33 -0
  139. data/lib/barometer/weather_services/wunderground_v1/query.rb +30 -0
  140. data/lib/barometer/weather_services/wunderground_v1/response/current_weather.rb +92 -0
  141. data/lib/barometer/weather_services/wunderground_v1/response/forecasted_weather.rb +87 -0
  142. data/lib/barometer/weather_services/wunderground_v1/response/full_timezone.rb +22 -0
  143. data/lib/barometer/weather_services/wunderground_v1/response/location.rb +43 -0
  144. data/lib/barometer/weather_services/wunderground_v1/response/station.rb +39 -0
  145. data/lib/barometer/weather_services/wunderground_v1/response/sun.rb +53 -0
  146. data/lib/barometer/weather_services/wunderground_v1/response/timezone.rb +15 -0
  147. data/lib/barometer/weather_services/yahoo.rb +16 -198
  148. data/lib/barometer/weather_services/yahoo/api.rb +21 -0
  149. data/lib/barometer/weather_services/yahoo/query.rb +42 -0
  150. data/lib/barometer/weather_services/yahoo/response.rb +39 -0
  151. data/lib/barometer/weather_services/yahoo/response/current_weather.rb +86 -0
  152. data/lib/barometer/weather_services/yahoo/response/forecasted_weather.rb +71 -0
  153. data/lib/barometer/weather_services/yahoo/response/location.rb +47 -0
  154. data/lib/barometer/weather_services/yahoo/response/sun.rb +43 -0
  155. data/lib/barometer/weather_services/yahoo/response/timezone.rb +15 -0
  156. data/spec/barometer_spec.rb +18 -120
  157. data/spec/base_spec.rb +114 -0
  158. data/spec/cassettes/Converter_FromCoordinatesToNoaaStationId.json +1 -0
  159. data/spec/cassettes/Converter_FromWeatherIdToGeocode.json +1 -0
  160. data/spec/cassettes/Converter_FromWoeIdOrIpv4ToGeocode.json +1 -0
  161. data/spec/cassettes/Converter_ToGeocode.json +1 -0
  162. data/spec/cassettes/Converter_ToWeatherId.json +1 -0
  163. data/spec/cassettes/Converter_ToWoeId.json +1 -0
  164. data/spec/cassettes/Service_FromWeatherId.json +1 -0
  165. data/spec/cassettes/Service_GoogleGeocode.json +1 -0
  166. data/spec/cassettes/Service_NoaaStation.json +1 -0
  167. data/spec/cassettes/Service_ToWeatherId.json +1 -0
  168. data/spec/cassettes/Service_ToWoeId.json +1 -0
  169. data/spec/cassettes/Service_YahooGeocode.json +1 -0
  170. data/spec/cassettes/WeatherService_ForecastIo.json +1 -0
  171. data/spec/cassettes/WeatherService_Noaa.json +1 -1
  172. data/spec/cassettes/WeatherService_WeatherBug.json +1 -1
  173. data/spec/cassettes/WeatherService_WundergroundV1.json +1 -0
  174. data/spec/cassettes/WeatherService_Yahoo.json +1 -1
  175. data/spec/data/attributes/distance_spec.rb +60 -0
  176. data/spec/data/attributes/location_spec.rb +41 -0
  177. data/spec/data/attributes/pressure_spec.rb +60 -0
  178. data/spec/data/attributes/sun_spec.rb +33 -0
  179. data/spec/data/attributes/temperature_spec.rb +60 -0
  180. data/spec/data/attributes/time_spec.rb +58 -0
  181. data/spec/data/attributes/vector_spec.rb +43 -0
  182. data/spec/data/attributes/zone_spec.rb +34 -0
  183. data/spec/data/convertable_units_spec.rb +299 -0
  184. data/spec/data/coordinates_spec.rb +15 -0
  185. data/spec/data/distance_spec.rb +49 -333
  186. data/spec/data/geo_spec.rb +72 -71
  187. data/spec/data/location_spec.rb +70 -65
  188. data/spec/data/pressure_spec.rb +49 -333
  189. data/spec/data/sun_spec.rb +57 -81
  190. data/spec/data/temperature_spec.rb +49 -393
  191. data/spec/data/vector_spec.rb +100 -0
  192. data/spec/data/zone_spec.rb +199 -266
  193. data/spec/query/base_spec.rb +296 -0
  194. data/spec/query/converter_spec.rb +98 -0
  195. data/spec/query/converters/from_coordinates_to_noaa_station_id_spec.rb +35 -0
  196. data/spec/query/converters/from_geocode_to_coordinates_spec.rb +25 -0
  197. data/spec/query/converters/from_short_zipcode_to_zipcode_spec.rb +31 -0
  198. data/spec/query/converters/from_weather_id_to_geocode_spec.rb +40 -0
  199. data/spec/query/converters/from_woe_id_or_ipv4_to_geocode_spec.rb +51 -0
  200. data/spec/query/converters/to_geocode_spec.rb +99 -0
  201. data/spec/query/converters/to_weather_id_spec.rb +35 -0
  202. data/spec/query/converters/to_woe_id_spec.rb +74 -0
  203. data/spec/query/formats/base_spec.rb +15 -0
  204. data/spec/query/formats/coordinates_spec.rb +13 -0
  205. data/spec/query/formats/geocode_spec.rb +9 -0
  206. data/spec/query/formats/icao_spec.rb +20 -0
  207. data/spec/query/formats/ipv4_address_spec.rb +33 -0
  208. data/spec/query/formats/noaa_station_id_spec.rb +9 -0
  209. data/spec/query/formats/postalcode_spec.rb +18 -0
  210. data/spec/query/formats/short_zipcode_spec.rb +18 -0
  211. data/spec/query/formats/unknown_spec.rb +9 -0
  212. data/spec/query/formats/weather_id_spec.rb +31 -0
  213. data/spec/query/formats/woe_id_spec.rb +45 -0
  214. data/spec/query/formats/zipcode_spec.rb +18 -0
  215. data/spec/query/formats_spec.rb +55 -0
  216. data/spec/query/services/from_weather_id_spec.rb +54 -0
  217. data/spec/query/services/google_geocode_spec.rb +60 -0
  218. data/spec/query/services/noaa_station_id_spec.rb +23 -0
  219. data/spec/query/services/to_weather_id_spec.rb +23 -0
  220. data/spec/query/services/to_woe_id_spec.rb +51 -0
  221. data/spec/query/services/yahoo_geocode_spec.rb +72 -0
  222. data/spec/response/base_spec.rb +103 -0
  223. data/spec/response/current_spec.rb +33 -0
  224. data/spec/response/predicition_spec.rb +65 -0
  225. data/spec/response/prediction_collection_spec.rb +97 -0
  226. data/spec/spec_helper.rb +14 -12
  227. data/spec/support/key_file_parser.rb +22 -0
  228. data/spec/support/matchers/formats.rb +36 -0
  229. data/spec/support/matchers/have_data.rb +46 -0
  230. data/spec/support/matchers/have_field.rb +128 -0
  231. data/spec/support/matchers/have_forecast.rb +46 -0
  232. data/spec/support/matchers/path.rb +23 -0
  233. data/spec/support/query_factory.rb +20 -0
  234. data/spec/utils/address_spec.rb +67 -0
  235. data/spec/utils/config_reader_spec.rb +157 -0
  236. data/spec/utils/get_spec.rb +22 -0
  237. data/spec/utils/payload_request_spec.rb +69 -0
  238. data/spec/utils/payload_spec.rb +168 -0
  239. data/spec/utils/post_spec.rb +20 -0
  240. data/spec/utils/time_spec.rb +93 -0
  241. data/spec/utils/versioned_registration_spec.rb +105 -0
  242. data/spec/weather_services/base_spec.rb +116 -0
  243. data/spec/weather_services/forecast_io_spec.rb +71 -0
  244. data/spec/weather_services/noaa/current_response_spec.rb +22 -0
  245. data/spec/weather_services/noaa/forecast_response_spec.rb +33 -0
  246. data/spec/weather_services/noaa_spec.rb +68 -166
  247. data/spec/weather_services/weather_bug/current_response_spec.rb +64 -0
  248. data/spec/weather_services/weather_bug/forecast_response_spec.rb +23 -0
  249. data/spec/weather_services/weather_bug_spec.rb +62 -202
  250. data/spec/weather_services/wunderground_v1/current_response_spec.rb +19 -0
  251. data/spec/weather_services/wunderground_v1/forecast_response_spec.rb +62 -0
  252. data/spec/weather_services/wunderground_v1_spec.rb +78 -0
  253. data/spec/weather_services/yahoo/response_spec.rb +73 -0
  254. data/spec/weather_services/yahoo_spec.rb +52 -135
  255. data/spec/weather_services_spec.rb +103 -0
  256. data/spec/weather_spec.rb +167 -347
  257. metadata +342 -123
  258. data/.document +0 -5
  259. data/TODO +0 -60
  260. data/bin/barometer +0 -441
  261. data/lib/barometer/data/local_datetime.rb +0 -145
  262. data/lib/barometer/data/local_time.rb +0 -134
  263. data/lib/barometer/data/speed.rb +0 -158
  264. data/lib/barometer/data/units.rb +0 -49
  265. data/lib/barometer/formats.rb +0 -13
  266. data/lib/barometer/formats/coordinates.rb +0 -57
  267. data/lib/barometer/formats/format.rb +0 -64
  268. data/lib/barometer/formats/geocode.rb +0 -60
  269. data/lib/barometer/formats/icao.rb +0 -37
  270. data/lib/barometer/formats/postalcode.rb +0 -22
  271. data/lib/barometer/formats/short_zipcode.rb +0 -17
  272. data/lib/barometer/formats/weather_id.rb +0 -92
  273. data/lib/barometer/formats/woe_id.rb +0 -150
  274. data/lib/barometer/formats/zipcode.rb +0 -31
  275. data/lib/barometer/key_file_parser.rb +0 -20
  276. data/lib/barometer/measurements/measurement.rb +0 -202
  277. data/lib/barometer/measurements/result.rb +0 -207
  278. data/lib/barometer/measurements/result_array.rb +0 -75
  279. data/lib/barometer/services.rb +0 -19
  280. data/lib/barometer/weather_services/service.rb +0 -189
  281. data/lib/barometer/weather_services/wunderground.rb +0 -264
  282. data/lib/barometer/web_services/geocode.rb +0 -34
  283. data/lib/barometer/web_services/noaa_station_id.rb +0 -53
  284. data/lib/barometer/web_services/placemaker.rb +0 -95
  285. data/lib/barometer/web_services/timezone.rb +0 -38
  286. data/lib/barometer/web_services/weather_id.rb +0 -50
  287. data/lib/barometer/web_services/web_service.rb +0 -29
  288. data/spec/cassettes/Barometer.json +0 -1
  289. data/spec/cassettes/Query.json +0 -1
  290. data/spec/cassettes/Query_Format_Coordinates.json +0 -1
  291. data/spec/cassettes/Query_Format_Geocode.json +0 -1
  292. data/spec/cassettes/Query_Format_WeatherID.json +0 -1
  293. data/spec/cassettes/Query_Format_WoeID.json +0 -1
  294. data/spec/cassettes/WeatherService.json +0 -1
  295. data/spec/cassettes/WeatherService_Wunderground.json +0 -1
  296. data/spec/cassettes/WebService_Geocode.json +0 -1
  297. data/spec/cassettes/WebService_NoaaStation.json +0 -1
  298. data/spec/data/local_datetime_spec.rb +0 -274
  299. data/spec/data/local_time_spec.rb +0 -239
  300. data/spec/data/speed_spec.rb +0 -374
  301. data/spec/data/units_spec.rb +0 -101
  302. data/spec/formats/coordinates_spec.rb +0 -166
  303. data/spec/formats/format_spec.rb +0 -74
  304. data/spec/formats/geocode_spec.rb +0 -163
  305. data/spec/formats/icao_spec.rb +0 -55
  306. data/spec/formats/postalcode_spec.rb +0 -53
  307. data/spec/formats/short_zipcode_spec.rb +0 -47
  308. data/spec/formats/weather_id_spec.rb +0 -182
  309. data/spec/formats/woe_id_spec.rb +0 -211
  310. data/spec/formats/zipcode_spec.rb +0 -103
  311. data/spec/key_file_parser_spec.rb +0 -28
  312. data/spec/measurements/measurement_spec.rb +0 -381
  313. data/spec/measurements/result_array_spec.rb +0 -150
  314. data/spec/measurements/result_spec.rb +0 -632
  315. data/spec/query_spec.rb +0 -498
  316. data/spec/weather_services/services_spec.rb +0 -135
  317. data/spec/weather_services/wunderground_spec.rb +0 -179
  318. data/spec/web_services/geocode_spec.rb +0 -31
  319. data/spec/web_services/noaa_station_id_spec.rb +0 -33
  320. data/spec/web_services/placemaker_spec.rb +0 -41
  321. data/spec/web_services/web_services_spec.rb +0 -20
@@ -0,0 +1,100 @@
1
+ require_relative '../spec_helper'
2
+
3
+ module Barometer::Data
4
+ describe Vector do
5
+ describe '.initialize' do
6
+ it 'sets kph' do
7
+ vector = Vector.new(16.09, nil, nil)
8
+ expect( vector.kph ).to eq 16.09
9
+ end
10
+
11
+ it 'sets mph' do
12
+ vector = Vector.new(nil, 10, nil)
13
+ expect( vector.mph ).to eq 10
14
+ end
15
+
16
+ it 'sets bearing' do
17
+ vector = Vector.new(nil, nil, 270)
18
+ expect( vector.bearing ).to eq 270
19
+ end
20
+
21
+ it 'defaults to metric' do
22
+ vector = Vector.new(20, 270)
23
+ expect( vector.to_s ).to eq '20 kph @ 270 degrees'
24
+ end
25
+ end
26
+
27
+ describe '#mph' do
28
+ it 'returns known value as mph' do
29
+ vector = Vector.new(:imperial, 10, nil)
30
+ expect( vector.mph ).to eq 10
31
+ end
32
+ end
33
+
34
+ describe '#kph' do
35
+ it 'returns known value as kph' do
36
+ vector = Vector.new(:metric, 20, nil)
37
+ expect( vector.kph ).to eq 20
38
+ end
39
+ end
40
+
41
+ describe '#units' do
42
+ context 'when vector is metric' do
43
+ it 'returns kph' do
44
+ vector = Vector.new(:metric, 20.0, 10.0, nil)
45
+ expect( vector.units ).to eq 'kph'
46
+ end
47
+ end
48
+
49
+ context 'when vector is imperial' do
50
+ it 'returns mph' do
51
+ vector = Vector.new(:imperial, 20.0, 10.0, nil)
52
+ expect( vector.units ).to eq 'mph'
53
+ end
54
+ end
55
+ end
56
+
57
+ describe '#to_s' do
58
+ context 'when vector is metric' do
59
+ it 'returns kph only when no bearing' do
60
+ vector = Vector.new(:metric, 16, nil, nil)
61
+ expect( vector.to_s ).to eq '16 kph'
62
+ end
63
+
64
+ it 'returns bearing only when no kph' do
65
+ vector = Vector.new(:metric, nil, nil, 270)
66
+ expect( vector.to_s ).to eq '270 degrees'
67
+ end
68
+
69
+ it 'returns kph and bearing' do
70
+ vector = Vector.new(:metric, 16, nil, 270)
71
+ expect( vector.to_s ).to eq '16 kph @ 270 degrees'
72
+ end
73
+ end
74
+
75
+ context 'when vector is imperial' do
76
+ it 'returns mph only when no bearing' do
77
+ vector = Vector.new(:imperial, nil, 10, nil)
78
+ expect( vector.to_s ).to eq '10 mph'
79
+ end
80
+
81
+ it 'returns bearing only when no mph' do
82
+ vector = Vector.new(:imperial, nil, nil, 270)
83
+ expect( vector.to_s ).to eq '270 degrees'
84
+ end
85
+
86
+ it 'returns mph and bearing' do
87
+ vector = Vector.new(:imperial, nil, 10, 270)
88
+ expect( vector.to_s ).to eq '10 mph @ 270 degrees'
89
+ end
90
+ end
91
+ end
92
+
93
+ describe '#nil?' do
94
+ it 'returns false if only bearing set' do
95
+ vector = Vector.new(nil, nil, 270)
96
+ expect( vector ).not_to be_nil
97
+ end
98
+ end
99
+ end
100
+ end
@@ -1,366 +1,299 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
1
+ require_relative '../spec_helper'
2
2
 
3
- describe "Data::Zone" do
3
+ module Barometer::Data
4
+ describe Zone do
5
+ describe '#new' do
6
+ let(:zone) { double(:zone) }
4
7
 
5
- # describe "and class methods" do
6
- #
7
- # it "responds to now and returns Time object" do
8
- # Data::Zone.respond_to?("now").should be_true
9
- # Data::Zone.now.is_a?(Time).should be_true
10
- # end
11
- #
12
- # it "responds to today and returns Date object" do
13
- # Data::Zone.respond_to?("today").should be_true
14
- # Data::Zone.today.is_a?(Date).should be_true
15
- # end
16
- #
17
- # end
8
+ before do
9
+ ZoneFull.stub(detect?: false)
10
+ ZoneOffset.stub(detect?: false)
11
+ ZoneCode.stub(detect?: false)
12
+ end
18
13
 
19
- describe "when initialized" do
14
+ it 'detects a full timezone input' do
15
+ ZoneFull.stub(detect?: true, new: nil)
16
+ Zone.new(zone)
17
+ expect( ZoneFull ).to have_received(:new).with(zone)
18
+ end
20
19
 
21
- describe "with a full zone" do
20
+ it 'detects a timezone code input' do
21
+ ZoneOffset.stub(detect?: true, new: nil)
22
+ Zone.new(zone)
23
+ expect( ZoneOffset ).to have_received(:new).with(zone)
24
+ end
22
25
 
23
- before(:each) do
24
- @utc = Time.now.utc
25
- @timezone = "Europe/Paris"
26
- @zone = Data::Zone.new(@timezone)
26
+ it 'detects a timezone offset input' do
27
+ ZoneCode.stub(detect?: true, new: nil)
28
+ Zone.new(zone)
29
+ expect( ZoneCode ).to have_received(:new).with(zone)
27
30
  end
28
31
 
29
- it "responds to zone_full" do
30
- @zone.zone_full.should_not be_nil
31
- @zone.zone_full.should == @timezone
32
+ it 'raises an error when nothing detected' do
33
+ expect {
34
+ Zone.new(zone)
35
+ }.to raise_error(ArgumentError)
32
36
  end
37
+ end
38
+ end
39
+
40
+ describe ZoneFull do
41
+ def stub_time(utc_now)
42
+ now = double(:now, utc: utc_now)
43
+ double(:time_class, now: now)
44
+ end
33
45
 
34
- it "responds to zone_code" do
35
- @zone.zone_code.should be_nil
46
+ describe '.detect?' do
47
+ it 'returns true when given a full timezone' do
48
+ expect( ZoneFull.detect?('America/Los_Angeles') ).to be_true
36
49
  end
37
50
 
38
- it "responds to zone_offset" do
39
- @zone.zone_offset.should be_nil
51
+ it 'returns false when given a timezone code' do
52
+ expect( ZoneFull.detect?('PST') ).to be_false
40
53
  end
41
54
 
42
- it "responds to tz" do
43
- lambda { Data::Zone.new("invalid timezone") }.should raise_error(ArgumentError)
55
+ it 'returns false when given an offset' do
56
+ expect( ZoneFull.detect?(10) ).to be_false
57
+ end
44
58
 
45
- zone = Data::Zone.new(@timezone)
46
- zone.tz.should_not be_nil
59
+ it 'returns false when given nothing' do
60
+ expect( ZoneFull.detect?('') ).to be_false
61
+ expect( ZoneFull.detect?(nil) ).to be_false
47
62
  end
63
+ end
48
64
 
49
- it "responds to full" do
50
- @zone.respond_to?("full").should be_true
51
- zone = Data::Zone.new(@timezone)
52
- zone.tz = nil
53
- zone.tz.should be_nil
54
- zone.full.should == @timezone
65
+ describe '#code' do
66
+ it 'returns the correct non-DST zone code' do
67
+ time = stub_time(::Time.utc(2013, 1, 1))
68
+ zone = ZoneFull.new('America/Los_Angeles', time)
55
69
 
56
- zone = Data::Zone.new(@timezone)
57
- zone.full.should == @timezone
70
+ expect( zone.code ).to eq 'PST'
58
71
  end
59
72
 
60
- it "responds to code" do
61
- @zone.respond_to?("code").should be_true
62
- zone = Data::Zone.new(@timezone)
63
- zone.tz = nil
64
- zone.tz.should be_nil
65
- zone.code.should be_nil
73
+ it 'returns the correct DST zone code' do
74
+ time = stub_time(::Time.utc(2013, 6, 1))
75
+ zone = ZoneFull.new('America/Los_Angeles', time)
66
76
 
67
- zone = Data::Zone.new(@timezone)
68
- # the expected result of this depends on the time of year
69
- # when summer expect "CEST", otherwise "CET"
70
- # just let TZINFO handle this
71
- zone.code.should == TZInfo::Timezone.get(@timezone).period_for_utc(Time.now.utc).zone_identifier.to_s
77
+ expect( zone.code ).to eq 'PDT'
72
78
  end
79
+ end
80
+
81
+ describe '#offset' do
82
+ it 'returns the current non-DST offset' do
83
+ time = stub_time(::Time.utc(2013, 1, 1, 18, 0, 0))
84
+ zone = ZoneFull.new('America/Los_Angeles', time)
73
85
 
74
- it "responds to dst?" do
75
- @zone.respond_to?("dst?").should be_true
76
- zone = Data::Zone.new(@timezone)
77
- zone.tz = nil
78
- zone.tz.should be_nil
79
- zone.dst?.should be_nil
86
+ expect( zone.offset ).to eq(-8 * 60 * 60)
80
87
  end
81
88
 
82
- it "responds to now" do
83
- @zone.respond_to?("now").should be_true
84
- @zone.now.is_a?(Time).should be_true
89
+ it 'returns the current DST offset' do
90
+ time = stub_time(::Time.utc(2013, 6, 1, 18, 0, 0))
91
+ zone = ZoneFull.new('America/Los_Angeles', time)
92
+
93
+ expect( zone.offset ).to eq(-7 * 60 * 60)
94
+ end
95
+ end
85
96
 
86
- period = @zone.tz.period_for_utc(Time.now)
87
- actual_now = Time.now.utc + period.utc_total_offset
97
+ describe '#now' do
98
+ it 'returns the current non-DST local time' do
99
+ time = stub_time(::Time.utc(2013, 1, 1, 18, 0, 0))
100
+ zone = ZoneFull.new('America/Los_Angeles', time)
88
101
 
89
- now = @zone.now
90
- now.hour.should == actual_now.hour
91
- now.min.should == actual_now.min
92
- now.sec.should == actual_now.sec
93
- now.year.should == actual_now.year
94
- now.month.should == actual_now.month
95
- now.day.should == actual_now.day
102
+ expect( zone.now ).to eq ::Time.utc(2013, 1, 1, 10, 0, 0)
96
103
  end
97
104
 
98
- it "responds to today" do
99
- @zone.respond_to?("today").should be_true
100
- @zone.today.is_a?(Date).should be_true
105
+ it 'returns the current DST local time' do
106
+ time = stub_time(::Time.utc(2013, 6, 1, 18, 0, 0))
107
+ zone = ZoneFull.new('America/Los_Angeles', time)
101
108
 
102
- period = @zone.tz.period_for_utc(Time.now)
103
- actual_now = Time.now.utc + period.utc_total_offset
109
+ expect( zone.now ).to eq ::Time.utc(2013, 6, 1, 11, 0, 0)
110
+ end
111
+ end
104
112
 
105
- now = @zone.today
106
- now.year.should == actual_now.year
107
- now.month.should == actual_now.month
108
- now.day.should == actual_now.day
113
+ describe '#to_s' do
114
+ it 'returns the input zone' do
115
+ expect( ZoneFull.new('Europe/Paris').to_s ).to eq 'Europe/Paris'
109
116
  end
117
+ end
110
118
 
111
- it "converts local_time to utc" do
112
- local_time = Time.now.utc
113
- utc_time = @zone.local_to_utc(local_time)
119
+ describe '#local_to_utc' do
120
+ it 'converts a time in the local time zone to UTC' do
121
+ zone = ZoneFull.new('America/Los_Angeles')
122
+ local_time = ::Time.now.utc
114
123
 
115
- offset = @zone.tz.period_for_utc(local_time).utc_total_offset
116
- utc_time.should == (local_time - offset)
124
+ expect( zone.local_to_utc(local_time).to_i ).to eq((local_time - zone.offset).to_i)
117
125
  end
126
+ end
118
127
 
119
- it "converts utc to local_time" do
120
- utc_time = Time.now.utc
121
- local_time = @zone.utc_to_local(utc_time)
128
+ describe '#utc_to_local' do
129
+ it 'converts a time in the local time zone to UTC' do
130
+ zone = ZoneFull.new('America/Los_Angeles')
131
+ utc_time = ::Time.now.utc
122
132
 
123
- offset = @zone.tz.period_for_utc(local_time).utc_total_offset
124
- utc_time.should == (local_time - offset)
133
+ expect( zone.utc_to_local(utc_time).to_i ).to eq((utc_time + zone.offset).to_i)
125
134
  end
126
-
127
135
  end
136
+ end
128
137
 
129
- describe "with a zone code" do
138
+ describe ZoneOffset do
139
+ def stub_time(utc_now)
140
+ now = double(:now, utc: utc_now)
141
+ double(:time_class, now: now)
142
+ end
130
143
 
131
- before(:each) do
132
- @utc = Time.now.utc
133
- @timezone = "EAST"
134
- @zone = Data::Zone.new(@timezone)
144
+ describe '.detect?' do
145
+ it 'returns false when given a full timezone' do
146
+ expect( ZoneOffset.detect?('America/Los_Angeles') ).to be_false
135
147
  end
136
148
 
137
- it "responds to zone_code" do
138
- @zone.zone_code.should_not be_nil
139
- @zone.zone_code.should == @timezone
149
+ it 'returns false when given a timezone code' do
150
+ expect( ZoneOffset.detect?('PST') ).to be_false
140
151
  end
141
152
 
142
- it "responds to zone_full" do
143
- @zone.zone_full.should be_nil
153
+ it 'returns true when given an offset' do
154
+ expect( ZoneOffset.detect?(10) ).to be_true
144
155
  end
145
156
 
146
- it "responds to zone_offset" do
147
- @zone.zone_offset.should be_nil
157
+ it 'returns false when given an offset out of range' do
158
+ expect( ZoneOffset.detect?(15) ).to be_false
148
159
  end
149
160
 
150
- it "responds to tz" do
151
- zone = Data::Zone.new(@timezone)
152
- zone.tz.should be_nil
161
+ it 'returns false when given nothing' do
162
+ expect( ZoneOffset.detect?('') ).to be_false
163
+ expect( ZoneOffset.detect?(nil) ).to be_false
153
164
  end
165
+ end
154
166
 
155
- it "responds to code" do
156
- @zone.respond_to?("code").should be_true
157
- zone = Data::Zone.new(@timezone)
158
- zone.tz = nil
159
- zone.tz.should be_nil
160
- zone.code.should == @timezone
161
-
162
- zone = Data::Zone.new(@timezone)
163
- zone.code.should == @timezone
167
+ describe '#code' do
168
+ it 'returns nil' do
169
+ expect( ZoneOffset.new(10).code ).to be_nil
164
170
  end
171
+ end
165
172
 
166
- it "responds to full" do
167
- @zone.respond_to?("full").should be_true
168
- zone = Data::Zone.new(@timezone)
169
- zone.tz = nil
170
- zone.tz.should be_nil
171
- zone.full.should be_nil
172
-
173
- zone = Data::Zone.new(@timezone)
174
- zone.full.should be_nil
173
+ describe '#offset' do
174
+ it 'converts the input from hours to seconds' do
175
+ expect( ZoneOffset.new(5).offset ).to eq(5 * 60 * 60)
175
176
  end
177
+ end
176
178
 
177
- it "responds to now" do
178
- @zone.respond_to?("now").should be_true
179
- @zone.now.is_a?(Time).should be_true
180
-
181
- actual_now = Time.now.utc + (-6*60*60)
179
+ describe '#now' do
180
+ it 'returns the current local time' do
181
+ time = stub_time(::Time.utc(2013, 1, 1, 10, 0, 0))
182
+ zone = ZoneOffset.new(5, time)
182
183
 
183
- now = @zone.now
184
- now.hour.should == actual_now.hour
185
- now.min.should == actual_now.min
186
- now.sec.should == actual_now.sec
187
- now.year.should == actual_now.year
188
- now.month.should == actual_now.month
189
- now.day.should == actual_now.day
184
+ expect( zone.now ).to eq ::Time.utc(2013, 1, 1, 15, 0, 0)
190
185
  end
186
+ end
191
187
 
192
- it "responds to today" do
193
- @zone.respond_to?("today").should be_true
194
- @zone.today.is_a?(Date).should be_true
195
-
196
- actual_now = Time.now.utc + (-6*60*60)
197
-
198
- now = @zone.today
199
- now.year.should == actual_now.year
200
- now.month.should == actual_now.month
201
- now.day.should == actual_now.day
188
+ describe '#to_s' do
189
+ it 'returns the input zone' do
190
+ expect( ZoneOffset.new(5).to_s ).to eq '5'
202
191
  end
192
+ end
203
193
 
204
- it "converts local_time to utc" do
205
- local_time = Time.now.utc
206
- utc_time = @zone.local_to_utc(local_time)
194
+ describe '#local_to_utc' do
195
+ it 'converts a time in the local time zone to UTC' do
196
+ zone = ZoneOffset.new(5)
197
+ local_time = ::Time.now.utc
207
198
 
208
- utc_time.year.should == (local_time - @zone.offset).year
209
- utc_time.month.should == (local_time - @zone.offset).month
210
- utc_time.day.should == (local_time - @zone.offset).day
211
- utc_time.hour.should == (local_time - @zone.offset).hour
212
- utc_time.min.should == (local_time - @zone.offset).min
213
- utc_time.sec.should == (local_time - @zone.offset).sec
199
+ expect( zone.local_to_utc(local_time).to_i ).to eq((local_time - zone.offset).to_i)
214
200
  end
201
+ end
215
202
 
216
- it "converts utc to local_time" do
217
- utc_time = Time.now.utc
218
- local_time = @zone.utc_to_local(utc_time)
203
+ describe '#utc_to_local' do
204
+ it 'converts a time in the local time zone to UTC' do
205
+ zone = ZoneOffset.new(5)
206
+ utc_time = ::Time.now.utc
219
207
 
220
- local_time.year.should == (utc_time + @zone.offset).year
221
- local_time.month.should == (utc_time + @zone.offset).month
222
- local_time.day.should == (utc_time + @zone.offset).day
223
- local_time.hour.should == (utc_time + @zone.offset).hour
224
- local_time.min.should == (utc_time + @zone.offset).min
225
- local_time.sec.should == (utc_time + @zone.offset).sec
208
+ expect( zone.utc_to_local(utc_time).to_i ).to eq((utc_time + zone.offset).to_i)
226
209
  end
227
-
228
210
  end
211
+ end
229
212
 
230
- describe "with a zone offset" do
213
+ describe ZoneCode do
214
+ def stub_time(utc_now)
215
+ now = double(:now, utc: utc_now)
216
+ double(:time_class, now: now)
217
+ end
231
218
 
232
- before(:each) do
233
- @utc = Time.now.utc
234
- @timezone = 8.5
235
- @zone = Data::Zone.new(@timezone)
219
+ describe '.detect?' do
220
+ it 'returns false when given a full timezone' do
221
+ expect( ZoneCode.detect?('America/Los_Angeles') ).to be_false
236
222
  end
237
223
 
238
- it "responds to zone_offset" do
239
- @zone.zone_offset.should_not be_nil
240
- @zone.zone_offset.should == @timezone
224
+ it 'returns true when given a timezone code' do
225
+ expect( ZoneCode.detect?('PST') ).to be_true
241
226
  end
242
227
 
243
- it "responds to zone_full" do
244
- @zone.zone_full.should be_nil
228
+ it 'returns true when given an obscure timezone code' do
229
+ expect( ZoneCode.detect?('CEST') ).to be_true
245
230
  end
246
231
 
247
- it "responds to zone_code" do
248
- @zone.zone_code.should be_nil
232
+ it 'returns false when given an invalid timezone code' do
233
+ expect( ZoneCode.detect?('ABC') ).to be_false
249
234
  end
250
235
 
251
- it "responds to tz" do
252
- zone = Data::Zone.new(@timezone)
253
- zone.tz.should be_nil
236
+ it 'returns false when given an offset' do
237
+ expect( ZoneCode.detect?(10) ).to be_false
254
238
  end
255
239
 
256
- it "responds to offset" do
257
- @zone.respond_to?("offset").should be_true
258
- zone = Data::Zone.new(@timezone)
259
- zone.tz = nil
260
- zone.tz.should be_nil
261
- zone.offset.should == (@timezone * 60 * 60)
262
-
263
- zone = Data::Zone.new(@timezone)
264
- zone.offset.should == (@timezone * 60 * 60)
240
+ it 'returns false when given nothing' do
241
+ expect( ZoneCode.detect?('') ).to be_false
242
+ expect( ZoneCode.detect?(nil) ).to be_false
265
243
  end
244
+ end
266
245
 
267
- it "responds to full" do
268
- @zone.respond_to?("full").should be_true
269
- zone = Data::Zone.new(@timezone)
270
- zone.tz = nil
271
- zone.tz.should be_nil
272
- zone.full.should be_nil
273
-
274
- zone = Data::Zone.new(@timezone)
275
- zone.full.should be_nil
246
+ describe '#code' do
247
+ it 'returns the input code' do
248
+ expect( ZoneCode.new('PST').code ).to eq 'PST'
276
249
  end
250
+ end
277
251
 
278
- it "responds to code" do
279
- @zone.respond_to?("code").should be_true
280
- zone = Data::Zone.new(@timezone)
281
- zone.tz = nil
282
- zone.tz.should be_nil
283
- zone.code.should be_nil
284
-
285
- zone = Data::Zone.new(@timezone)
286
- zone.code.should be_nil
252
+ describe '#offset' do
253
+ it 'returns the offset in seconds' do
254
+ expect( ZoneCode.new('PST').offset ).to eq(-8 * 60 * 60)
287
255
  end
288
256
 
289
- it "responds to now" do
290
- @zone.respond_to?("now").should be_true
291
- @zone.now.is_a?(Time).should be_true
292
-
293
- actual_now = Time.now.utc + (@timezone.to_f*60*60)
294
-
295
- now = @zone.now
296
- now.hour.should == actual_now.hour
297
- now.min.should == actual_now.min
298
- now.sec.should == actual_now.sec
299
- now.year.should == actual_now.year
300
- now.month.should == actual_now.month
301
- now.day.should == actual_now.day
257
+ it 'returns the offset in seconds for an obscure input code' do
258
+ expect( ZoneCode.new('CEST').offset ).to eq(2 * 60 * 60)
302
259
  end
303
260
 
304
- it "responds to today" do
305
- @zone.respond_to?("today").should be_true
306
- @zone.today.is_a?(Date).should be_true
307
-
308
- actual_now = Time.now.utc + (@timezone.to_f*60*60)
309
-
310
- now = @zone.today
311
- now.year.should == actual_now.year
312
- now.month.should == actual_now.month
313
- now.day.should == actual_now.day
261
+ it 'returns 0 for unknown codes' do
262
+ expect( ZoneCode.new('ABC').offset ).to be_zero
314
263
  end
264
+ end
315
265
 
316
- it "converts local_time to utc" do
317
- local_time = Time.now.utc
318
- utc_time = @zone.local_to_utc(local_time)
266
+ describe '#now' do
267
+ it 'returns the current local time' do
268
+ time = stub_time(::Time.utc(2013, 1, 1, 10, 0, 0))
269
+ zone = ZoneCode.new('PST', time)
319
270
 
320
- utc_time.year.should == (local_time - @zone.offset).year
321
- utc_time.month.should == (local_time - @zone.offset).month
322
- utc_time.day.should == (local_time - @zone.offset).day
323
- utc_time.hour.should == (local_time - @zone.offset).hour
324
- utc_time.min.should == (local_time - @zone.offset).min
325
- utc_time.sec.should == (local_time - @zone.offset).sec
271
+ expect( zone.now ).to eq ::Time.utc(2013, 1, 1, 2, 0, 0)
326
272
  end
273
+ end
327
274
 
328
- it "converts utc to local_time" do
329
- utc_time = Time.now.utc
330
- local_time = @zone.utc_to_local(utc_time)
331
-
332
- local_time.year.should == (utc_time + @zone.offset).year
333
- local_time.month.should == (utc_time + @zone.offset).month
334
- local_time.day.should == (utc_time + @zone.offset).day
335
- local_time.hour.should == (utc_time + @zone.offset).hour
336
- local_time.min.should == (utc_time + @zone.offset).min
337
- local_time.sec.should == (utc_time + @zone.offset).sec
275
+ describe '#to_s' do
276
+ it 'returns the input zone' do
277
+ expect( ZoneCode.new('PST').to_s ).to eq 'PST'
338
278
  end
339
-
340
279
  end
341
280
 
342
- end
281
+ describe '#local_to_utc' do
282
+ it 'converts a time in the local time zone to UTC' do
283
+ zone = ZoneCode.new('PST')
284
+ local_time = ::Time.now.utc
343
285
 
344
- describe "when detecting zones" do
345
-
346
- it "recognozes a full time zone format" do
347
- Data::Zone.is_zone_full?("invalid").should be_false
348
- Data::Zone.is_zone_full?("America/New York").should be_true
286
+ expect( zone.local_to_utc(local_time).to_i ).to eq((local_time - zone.offset).to_i)
287
+ end
349
288
  end
350
289
 
351
- it "matches a zone offset" do
352
- Data::Zone.is_zone_offset?("invalid").should be_false
353
- Data::Zone.is_zone_offset?("MST").should be_false
354
- Data::Zone.is_zone_offset?("10").should be_false
355
- Data::Zone.is_zone_offset?(-10).should be_true
356
- end
290
+ describe '#utc_to_local' do
291
+ it 'converts a time in the local time zone to UTC' do
292
+ zone = ZoneCode.new('PST')
293
+ utc_time = ::Time.now.utc
357
294
 
358
- it "matches a zone code" do
359
- Data::Zone.is_zone_code?("invalid").should be_false
360
- Data::Zone.is_zone_code?("MST").should be_true
361
- Data::Zone.is_zone_code?("EAST").should be_true
295
+ expect( zone.utc_to_local(utc_time).to_i ).to eq((utc_time + zone.offset).to_i)
296
+ end
362
297
  end
363
-
364
298
  end
365
-
366
299
  end