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,299 @@
1
+ require_relative '../spec_helper'
2
+
3
+ module Barometer::Data
4
+ class TestData < ConvertableUnits
5
+ private
6
+
7
+ def convert_imperial_to_metric(imperial_value)
8
+ imperial_value*200
9
+ end
10
+
11
+ def convert_metric_to_imperial(metric_value)
12
+ metric_value/200
13
+ end
14
+ end
15
+
16
+ describe ConvertableUnits do
17
+ describe '.initialize' do
18
+ it 'sets metric value' do
19
+ test_data = TestData.new(200.0, nil)
20
+ expect( test_data.metric ).to eq 200.0
21
+ end
22
+
23
+ it 'sets imperial value' do
24
+ test_data = TestData.new(nil, 1.0)
25
+ expect( test_data.imperial ).to eq 1.0
26
+ end
27
+
28
+ it 'defaults to metric' do
29
+ test_data = TestData.new(200)
30
+ expect( test_data ).to be_metric
31
+ expect( test_data.metric ).to eq 200.0
32
+ end
33
+
34
+ context 'when setting the unit type' do
35
+ it 'sets metric via boolean' do
36
+ test_data = TestData.new(true, 200, nil)
37
+ expect( test_data.to_s ).to eq '200 METRIC'
38
+ end
39
+
40
+ it 'sets metric via symbol' do
41
+ test_data = TestData.new(:metric, 200, nil)
42
+ expect( test_data.to_s ).to eq '200 METRIC'
43
+ end
44
+
45
+ it 'sets imperial via boolean' do
46
+ test_data = TestData.new(false, nil, 1.0)
47
+ expect( test_data.to_s ).to eq '1.0 IMPERIAL'
48
+ end
49
+
50
+ it 'sets imperial via symbol' do
51
+ test_data = TestData.new(:imperial, nil, 1.0)
52
+ expect( test_data.to_s ).to eq '1.0 IMPERIAL'
53
+ end
54
+
55
+ context 'and only provided one magnitude' do
56
+ context 'and metric is set' do
57
+ it 'interprets magnitude as metric value' do
58
+ test_data = TestData.new(:metric, 200)
59
+ expect( test_data.to_s ).to eq '200 METRIC'
60
+ end
61
+ end
62
+
63
+ context 'and imperial is set' do
64
+ it 'interprets magnitude as imperial value' do
65
+ test_data = TestData.new(:imperial, 1)
66
+ expect( test_data.to_s ).to eq '1 IMPERIAL'
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ describe '#imperial' do
74
+ it 'calculates imperial value from metric value' do
75
+ test_data = TestData.new(:metric, 200.0)
76
+ expect( test_data.imperial ).to eq 1.0
77
+ end
78
+
79
+ it 'returns nil if imperial value is nil' do
80
+ test_data = TestData.new(:imperial, nil)
81
+ expect( test_data.imperial ).to be_nil
82
+ end
83
+
84
+ it 'returns known value as imperial value' do
85
+ test_data = TestData.new(:imperial, 1)
86
+ expect( test_data.imperial ).to eq 1
87
+ end
88
+
89
+ # it 'keeps integer resolution' do
90
+ # test_data = TestData.new(:imperial, 10)
91
+ # expect( test_data.metric ).to eq 16
92
+ # end
93
+
94
+ # it 'keeps float resolution' do
95
+ # test_data = TestData.new(:imperial, 10.0)
96
+ # expect( test_data.metric ).to eq 16.1
97
+ # end
98
+ end
99
+
100
+ describe '#metric' do
101
+ it 'calculates metric value from imperial value' do
102
+ test_data = TestData.new(:imperial, 1.0)
103
+ expect( test_data.metric ).to eq 200.0
104
+ end
105
+
106
+ it 'returns nil if metric value is nil' do
107
+ test_data = TestData.new(:metric, nil)
108
+ expect( test_data.metric ).to be_nil
109
+ end
110
+
111
+ it 'returns known value as metric value' do
112
+ test_data = TestData.new(:metric, 200)
113
+ expect( test_data.metric ).to eq 200
114
+ end
115
+
116
+ # it 'keeps integer resolution' do
117
+ # test_data = TestData.new(:imperial, 10)
118
+ # expect( test_data.metric ).to eq 16
119
+ # end
120
+
121
+ # it 'keeps float resolution' do
122
+ # test_data = TestData.new(:imperial, 10.0)
123
+ # expect( test_data.metric ).to eq 16.1
124
+ # end
125
+ end
126
+
127
+ describe '#<=>' do
128
+ context 'when comparing two metric test_datas' do
129
+ it 'returns > correctly' do
130
+ test_data1 = TestData.new(:metric, 200)
131
+ test_data2 = TestData.new(:metric, 200.2)
132
+
133
+ expect( test_data2 ).to be > test_data1
134
+ end
135
+
136
+ it 'returns == correctly' do
137
+ test_data1 = TestData.new(:metric, 200)
138
+ test_data2 = TestData.new(:metric, 200.0)
139
+
140
+ expect( test_data2 ).to eq test_data1
141
+ end
142
+
143
+ it 'returns < correctly' do
144
+ test_data1 = TestData.new(:metric, 200)
145
+ test_data2 = TestData.new(:metric, 200.2)
146
+
147
+ expect( test_data1 ).to be < test_data2
148
+ end
149
+ end
150
+
151
+ context 'when comparing two imperial test_datas' do
152
+ it 'returns > correctly' do
153
+ test_data1 = TestData.new(:imperial, 1)
154
+ test_data2 = TestData.new(:imperial, 1.2)
155
+
156
+ expect( test_data2 ).to be > test_data1
157
+ end
158
+
159
+ it 'returns == correctly' do
160
+ test_data1 = TestData.new(:imperial, 1)
161
+ test_data2 = TestData.new(:imperial, 1.0)
162
+
163
+ expect( test_data2 ).to eq test_data1
164
+ end
165
+
166
+ it 'returns < correctly' do
167
+ test_data1 = TestData.new(:imperial, 1)
168
+ test_data2 = TestData.new(:imperial, 1.2)
169
+
170
+ expect( test_data1 ).to be < test_data2
171
+ end
172
+ end
173
+
174
+ context 'when comparing a mtric and an imperial test_data' do
175
+ it 'returns > correctly' do
176
+ test_data1 = TestData.new(:metric, 200.0)
177
+ test_data2 = TestData.new(:imperial, 1.2)
178
+
179
+ expect( test_data2 ).to be > test_data1
180
+ end
181
+
182
+ it 'returns == correctly' do
183
+ test_data1 = TestData.new(:metric, 200.0)
184
+ test_data2 = TestData.new(:imperial, 1.0)
185
+
186
+ expect( test_data2 ).to eq test_data1
187
+ end
188
+
189
+ it 'returns < correctly' do
190
+ test_data1 = TestData.new(:metric, 200.3)
191
+ test_data2 = TestData.new(:imperial, 1.0)
192
+
193
+ expect( test_data2 ).to be < test_data1
194
+ end
195
+ end
196
+ end
197
+
198
+ describe '#units' do
199
+ context 'when test_data is metric' do
200
+ it 'returns metric value' do
201
+ test_data = TestData.new(:metric, 200.0)
202
+ expect( test_data.units ).to eq 'METRIC'
203
+ end
204
+ end
205
+
206
+ context 'when test_data is imperial' do
207
+ it 'returns imperial value' do
208
+ test_data = TestData.new(:imperial, 1.0)
209
+ expect( test_data.units ).to eq 'IMPERIAL'
210
+ end
211
+ end
212
+ end
213
+
214
+ describe '#to_i' do
215
+ context 'when test_data is metric' do
216
+ it 'returns the value of metric value' do
217
+ test_data = TestData.new(:metric, 200.0, 1.0)
218
+ expect( test_data.to_i ).to eq 200
219
+ end
220
+
221
+ it 'returns 0 if nothing is set' do
222
+ test_data = TestData.new(:metric, nil)
223
+ expect( test_data.to_i ).to eq 0
224
+ end
225
+ end
226
+
227
+ context 'when test_data is imperial' do
228
+ it 'returns the value of imperial value' do
229
+ test_data = TestData.new(:imperial, 200.0, 1.0)
230
+ expect( test_data.to_i ).to eq 1
231
+ end
232
+
233
+ it 'returns 0 if nothing is set' do
234
+ test_data = TestData.new(:imperial, nil)
235
+ expect( test_data.to_i ).to eq 0
236
+ end
237
+ end
238
+ end
239
+
240
+ describe '#to_f' do
241
+ context 'when test_data is metric' do
242
+ it 'returns the value of metric value' do
243
+ test_data = TestData.new(:metric, 200, 1)
244
+ expect( test_data.to_f ).to eq 200.0
245
+ end
246
+
247
+ it 'returns 0 if nothing is set' do
248
+ test_data = TestData.new(:metric, nil)
249
+ expect( test_data.to_f ).to eq 0.0
250
+ end
251
+ end
252
+
253
+ context 'when test_data is imperial' do
254
+ it 'returns the value of imperial value' do
255
+ test_data = TestData.new(:imperial, 200, 1)
256
+ expect( test_data.to_f ).to eq 1.0
257
+ end
258
+
259
+ it 'returns 0 if nothing is set' do
260
+ test_data = TestData.new(:imperial, nil)
261
+ expect( test_data.to_f ).to eq 0.0
262
+ end
263
+ end
264
+ end
265
+
266
+ describe '#to_s' do
267
+ context 'when test_data is metric' do
268
+ it 'returns metric value' do
269
+ test_data = TestData.new(:metric, 200)
270
+ expect( test_data.to_s ).to eq '200 METRIC'
271
+ end
272
+ end
273
+
274
+ context 'when test_data is imperial' do
275
+ it 'returns imperial value' do
276
+ test_data = TestData.new(:imperial, 1)
277
+ expect( test_data.to_s ).to eq '1 IMPERIAL'
278
+ end
279
+ end
280
+ end
281
+
282
+ describe '#nil?' do
283
+ it 'returns true if nothing is set' do
284
+ test_data = TestData.new(nil, nil)
285
+ expect( test_data ).to be_nil
286
+ end
287
+
288
+ it 'returns false if only metric value set' do
289
+ test_data = TestData.new(200, nil)
290
+ expect( test_data ).to_not be_nil
291
+ end
292
+
293
+ it 'returns false if only imperial value set' do
294
+ test_data = TestData.new(nil, 1)
295
+ expect( test_data ).to_not be_nil
296
+ end
297
+ end
298
+ end
299
+ end
@@ -0,0 +1,15 @@
1
+ require_relative '../spec_helper'
2
+
3
+ module Barometer::Data
4
+ describe Coordinates do
5
+ describe '#coordinates' do
6
+ it 'joins latitude and longitude' do
7
+ coordinates = Coordinates.new(
8
+ longitude: '99.99',
9
+ latitude: '88.88'
10
+ )
11
+ expect( coordinates.coordinates ).to eq '88.88,99.99'
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,336 +1,52 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe "Data::Distance" do
4
-
5
- describe "when initialized" do
6
-
7
- it "defines METRIC_UNITS" do
8
- Data::Distance.const_defined?("METRIC_UNITS").should be_true
9
- Data::Distance::METRIC_UNITS.should == "km"
10
- end
11
-
12
- it "defines IMPERIAL_UNITS" do
13
- Data::Distance.const_defined?("IMPERIAL_UNITS").should be_true
14
- Data::Distance::IMPERIAL_UNITS.should == "m"
15
- end
16
-
17
- before(:each) do
18
- @distance = Data::Distance.new
19
- end
20
-
21
- it "responds to kilometers" do
22
- @distance.kilometers.should be_nil
23
- end
24
-
25
- it "responds to miles" do
26
- @distance.miles.should be_nil
27
- end
28
-
29
- it "responds to metric_default" do
30
- lambda { @distance.metric_default = 5 }.should_not raise_error(NotImplementedError)
31
- end
32
-
33
- it "responds to imperial_default" do
34
- lambda { @distance.imperial_default = 5 }.should_not raise_error(NotImplementedError)
35
- end
36
-
37
- it "responds to nil?" do
38
- @distance.nil?.should be_true
39
- @distance.km = 5
40
- @distance.nil?.should be_false
41
- end
42
-
43
- end
44
-
45
- describe "conversion" do
46
-
47
- # For all conversions
48
- # 26.2 M = 42.2 KM
49
- before(:each) do
50
- @m = 26.2
51
- @km = 42.2
52
- end
53
-
54
- it "requires a value, that is either Integer or Float" do
55
- Data::Distance.km_to_m(nil).should be_nil
56
- Data::Distance.m_to_km(nil).should be_nil
57
-
58
- not_float_or_integer = "string"
59
- Data::Distance.km_to_m(not_float_or_integer).should be_nil
60
- Data::Distance.m_to_km(not_float_or_integer).should be_nil
61
- end
62
-
63
- it "converts KM to M" do
64
- ((Data::Distance.km_to_m(@km)*10).round/10.0).should == @m
65
- end
66
-
67
- it "converts M to KM" do
68
- ((Data::Distance.m_to_km(@m)*10).round/10.0).should == @km
69
- end
70
-
71
- end
72
-
73
- describe "updating" do
74
-
75
- before(:each) do
76
- @distance = Data::Distance.new
77
- @m = 26.2
78
- @km = 42.2
79
- end
80
-
81
- it "nils M if new KM converts to a M that changes more then 1 unit" do
82
- @distance.miles = (@m + 1.1)
83
- @distance.update_miles(@km)
84
- @distance.miles.should be_nil
85
- end
86
-
87
- it "doesn't update M if new KM converts to a M that does not change more then 1 unit" do
88
- @distance.miles = (@m + 0.9)
89
- @distance.update_miles(@km)
90
- @distance.miles.should == (@m + 0.9)
91
- end
92
-
93
- it "doesn't set M if not already set" do
94
- @distance.miles.should be_nil
95
- @distance.kilometers.should be_nil
96
- @distance.update_miles(@km)
97
- @distance.miles.should be_nil
98
- @distance.kilometers.should be_nil
99
- end
100
-
101
- it "nils KM if new M converts to a KM that changes more then 1 unit" do
102
- @distance.kilometers = (@km + 1.1)
103
- @distance.update_kilometers(@m)
104
- @distance.kilometers.should be_nil
105
- end
106
-
107
- it "doesn't update KM if new M converts to a KM that does not change more then 1 unit" do
108
- @distance.kilometers = (@km + 0.9)
109
- @distance.update_kilometers(@m)
110
- @distance.kilometers.should == (@km + 0.9)
111
- end
112
-
113
- it "doesn't set KM if not already set" do
114
- @distance.miles.should be_nil
115
- @distance.kilometers.should be_nil
116
- @distance.update_kilometers(@m)
117
- @distance.miles.should be_nil
118
- @distance.kilometers.should be_nil
119
- end
120
-
121
- end
122
-
123
- describe "storing" do
124
-
125
- before(:each) do
126
- @distance = Data::Distance.new
127
- @m = 26.2
128
- @km = 42.2
129
- end
130
-
131
- it "doesn't update KM if nil value (or equivalent)" do
132
- @distance.kilometers.should be_nil
133
- @distance.km = nil
134
- @distance.kilometers.should be_nil
135
- @distance.km = "na"
136
- @distance.kilometers.should be_nil
137
- end
138
-
139
- it "stores KM and resets M" do
140
- @distance.kilometers.should be_nil
141
- @distance.miles = (@m + 1.1)
142
- @distance.km = @km
143
- @distance.kilometers.should == @km
144
- @distance.miles.should be_nil
145
- end
146
-
147
- it "doesn't update M if nil value (or equivalent)" do
148
- @distance.miles.should be_nil
149
- @distance.m = nil
150
- @distance.miles.should be_nil
151
- @distance.m = "na"
152
- @distance.miles.should be_nil
153
- end
154
-
155
- it "stores M and resets KM" do
156
- @distance.miles.should be_nil
157
- @distance.kilometers = (@km + 1.1)
158
- @distance.m = @m
159
- @distance.miles.should == @m
160
- @distance.kilometers.should be_nil
161
- end
162
-
163
- end
164
-
165
- describe "retrieving" do
166
-
167
- before(:each) do
168
- @distance = Data::Distance.new
169
- @m = 26.2
170
- @km = 42.16
171
- end
172
-
173
- it "returns KM if it exists" do
174
- @distance.km = @km
175
- @distance.kilometers.should == @km
176
- @distance.km(false).should == @km
177
- end
178
-
179
- it "auto converts from M if KM is nil and M exists" do
180
- @distance.m = @m
181
- @distance.miles.should == @m
182
- @distance.kilometers.should be_nil
183
- @distance.km(false).should == @km
184
- end
185
-
186
- it "allows a float to be returned for KM" do
187
- km = 42.12
188
- @distance.km = km
189
- @distance.kilometers.should == km
190
- @distance.km(true).should == km.to_i
191
- @distance.km(false).should == km.to_f
192
- end
193
-
194
- it "allows only 2 decimal precision for KM" do
195
- km = 42.1234
196
- @distance.km = km
197
- @distance.kilometers.should == km
198
- @distance.km(false).should == 42.12
199
- end
200
-
201
- it "returns M if it exists" do
202
- @distance.m = @m
203
- @distance.miles.should == @m
204
- @distance.m(false).should == @m
205
- end
206
-
207
- it "auto converts from KM if M is nil and KM exists" do
208
- @distance.km = @km
209
- @distance.kilometers.should == @km
210
- @distance.miles.should be_nil
211
- ((@distance.m(false)*10).round/10.0).should == @m
212
- end
213
-
214
- it "allows a float to be returned for M" do
215
- m = 26.12
216
- @distance.m = m
217
- @distance.miles.should == m
218
- @distance.m(true).should == m.to_i
219
- @distance.m(false).should == m.to_f
220
- end
221
-
222
- it "allows only 2 decimal precision for M" do
223
- m = 26.1234
224
- @distance.m = m
225
- @distance.miles.should == m
226
- @distance.m(false).should == 26.12
227
- end
228
-
229
- end
230
-
231
- describe "operators" do
232
-
233
- before(:each) do
234
- @m = 26.2
235
- @km = 42.16
236
- @distance_low = Data::Distance.new
237
- @distance_low.km = (@m - 1.0)
238
- @distance_high = Data::Distance.new
239
- @distance_high.km = (@km + 1.0)
240
- @distance = Data::Distance.new
241
- @distance.km = @km
242
- @distance_same = Data::Distance.new
243
- @distance_same.km = @km
244
- end
245
-
246
- it "defines <=>" do
247
- Data::Distance.method_defined?("<=>").should be_true
248
- (@distance_low <=> @distance_high).should == -1
249
- (@distance_high <=> @distance_low).should == 1
250
- (@distance <=> @distance_same).should == 0
1
+ require_relative '../spec_helper'
2
+
3
+ module Barometer::Data
4
+ describe Distance do
5
+ describe '.initialize' do
6
+ it 'sets km' do
7
+ distance = Distance.new(42.2, nil)
8
+ expect( distance.km ).to eq 42.2
9
+ end
10
+
11
+ it 'sets m' do
12
+ distance = Distance.new(nil, 26.2)
13
+ expect( distance.m ).to eq 26.2
14
+ end
15
+
16
+ it 'defaults to metric' do
17
+ distance = Distance.new(42)
18
+ expect( distance ).to be_metric
19
+ end
20
+ end
21
+
22
+ describe '#m' do
23
+ it 'returns known value as m' do
24
+ distance = Distance.new(:imperial, 26)
25
+ expect( distance.m ).to eq 26
26
+ end
27
+ end
28
+
29
+ describe '#km' do
30
+ it 'returns known value as km' do
31
+ distance = Distance.new(:metric, 42)
32
+ expect( distance.km ).to eq 42
33
+ end
34
+ end
35
+
36
+ describe '#units' do
37
+ context 'when distance is metric' do
38
+ it 'returns km' do
39
+ distance = Distance.new(:metric, 42.0)
40
+ expect( distance.units ).to eq 'km'
41
+ end
42
+ end
43
+
44
+ context 'when distance is imperial' do
45
+ it 'returns m' do
46
+ distance = Distance.new(:imperial, 26.0)
47
+ expect( distance.units ).to eq 'm'
48
+ end
49
+ end
251
50
  end
252
-
253
- it "defines <" do
254
- Data::Distance.method_defined?("<").should be_true
255
- @distance_low.should < @distance_high
256
- @distance_high.should_not < @distance_low
257
- @distance.should_not < @distance_same
258
- end
259
-
260
- it "defines >" do
261
- Data::Distance.method_defined?(">").should be_true
262
- @distance_low.should_not > @distance_high
263
- @distance_high.should > @distance_low
264
- @distance.should_not > @distance_same
265
- end
266
-
267
- it "defines ==" do
268
- Data::Distance.method_defined?("==").should be_true
269
- @distance_low.should_not == @distance_high
270
- @distance.should == @distance_same
271
- end
272
-
273
- it "defines <=" do
274
- Data::Distance.method_defined?("<=").should be_true
275
- @distance_low.should <= @distance_high
276
- @distance_high.should_not <= @distance_low
277
- @distance.should <= @distance_same
278
- end
279
-
280
- it "defines >=" do
281
- Data::Distance.method_defined?(">=").should be_true
282
- @distance_low.should_not >= @distance_high
283
- @distance_high.should >= @distance_low
284
- @distance.should >= @distance_same
285
- end
286
-
287
51
  end
288
-
289
- describe "changing units" do
290
-
291
- before(:each) do
292
- @m = 26.2
293
- @km = 42.16
294
- @distance = Data::Distance.new
295
- @distance.km = @km
296
- end
297
-
298
- it "returns just the integer value (no units)" do
299
- @distance.metric?.should be_true
300
- @distance.to_i.should == @km.to_i
301
-
302
- @distance.imperial!
303
- @distance.metric?.should be_false
304
- @distance.to_i.should == @m.to_i
305
- end
306
-
307
- it "returns just the float value (no units)" do
308
- @distance.metric?.should be_true
309
- @distance.to_f.should == @km.to_f
310
-
311
- @distance.imperial!
312
- @distance.metric?.should be_false
313
- ((@distance.to_f*10).round/10.0).should == @m.to_f
314
- end
315
-
316
- it "returns just the integer value with units" do
317
- @distance.metric?.should be_true
318
- @distance.to_s.should == "#{@km.to_i} #{Data::Distance::METRIC_UNITS}"
319
-
320
- @distance.imperial!
321
- @distance.metric?.should be_false
322
- @distance.to_s.should == "#{@m.to_i} #{Data::Distance::IMPERIAL_UNITS}"
323
- end
324
-
325
- it "returns just the units" do
326
- @distance.metric?.should be_true
327
- @distance.units.should == Data::Distance::METRIC_UNITS
328
-
329
- @distance.imperial!
330
- @distance.metric?.should be_false
331
- @distance.units.should == Data::Distance::IMPERIAL_UNITS
332
- end
333
-
334
- end
335
-
336
52
  end