barometer 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.pelusa.yml +7 -0
- data/.travis.yml +6 -3
- data/Gemfile +11 -1
- data/README.md +89 -244
- data/Rakefile +1 -1
- data/barometer.gemspec +22 -24
- data/lib/barometer.rb +14 -65
- data/lib/barometer/base.rb +35 -65
- data/lib/barometer/data.rb +15 -20
- data/lib/barometer/data/attribute.rb +19 -0
- data/lib/barometer/data/attributes/distance.rb +11 -0
- data/lib/barometer/data/attributes/float.rb +11 -0
- data/lib/barometer/data/attributes/integer.rb +11 -0
- data/lib/barometer/data/attributes/location.rb +11 -0
- data/lib/barometer/data/attributes/pressure.rb +11 -0
- data/lib/barometer/data/attributes/sun.rb +15 -0
- data/lib/barometer/data/attributes/temperature.rb +11 -0
- data/lib/barometer/data/attributes/time.rb +11 -0
- data/lib/barometer/data/attributes/vector.rb +11 -0
- data/lib/barometer/data/attributes/zone.rb +11 -0
- data/lib/barometer/data/convertable_units.rb +145 -0
- data/lib/barometer/data/coordinates.rb +16 -0
- data/lib/barometer/data/distance.rb +14 -133
- data/lib/barometer/data/geo.rb +22 -59
- data/lib/barometer/data/location.rb +14 -20
- data/lib/barometer/data/pressure.rb +14 -135
- data/lib/barometer/data/sun.rb +24 -24
- data/lib/barometer/data/temperature.rb +14 -169
- data/lib/barometer/data/vector.rb +44 -0
- data/lib/barometer/data/zone.rb +109 -112
- data/lib/barometer/query.rb +23 -161
- data/lib/barometer/query/base.rb +88 -0
- data/lib/barometer/query/converter.rb +64 -0
- data/lib/barometer/query/converters/from_coordinates_to_noaa_station_id.rb +30 -0
- data/lib/barometer/query/converters/from_geocode_to_coordinates.rb +28 -0
- data/lib/barometer/query/converters/from_short_zipcode_to_zipcode.rb +28 -0
- data/lib/barometer/query/converters/from_weather_id_to_geocode.rb +30 -0
- data/lib/barometer/query/converters/from_woe_id_or_ipv4_to_geocode.rb +32 -0
- data/lib/barometer/query/converters/to_geocode.rb +30 -0
- data/lib/barometer/query/converters/to_weather_id.rb +30 -0
- data/lib/barometer/query/converters/to_woe_id.rb +30 -0
- data/lib/barometer/query/format.rb +59 -0
- data/lib/barometer/query/formats/base.rb +22 -0
- data/lib/barometer/query/formats/coordinates.rb +14 -0
- data/lib/barometer/query/formats/geocode.rb +15 -0
- data/lib/barometer/query/formats/icao.rb +35 -0
- data/lib/barometer/query/formats/ipv4_address.rb +19 -0
- data/lib/barometer/query/formats/noaa_station_id.rb +15 -0
- data/lib/barometer/query/formats/postalcode.rb +20 -0
- data/lib/barometer/query/formats/short_zipcode.rb +15 -0
- data/lib/barometer/{translations → query/formats/translations}/icao_country_codes.yml +0 -0
- data/lib/barometer/{translations → query/formats/translations}/weather_country_codes.yml +0 -0
- data/lib/barometer/query/formats/unknown.rb +14 -0
- data/lib/barometer/query/formats/weather_id.rb +33 -0
- data/lib/barometer/query/formats/woe_id.rb +28 -0
- data/lib/barometer/query/formats/zipcode.rb +15 -0
- data/lib/barometer/query/service.rb +13 -0
- data/lib/barometer/query/services/apis/geonames_timezone.rb +26 -0
- data/lib/barometer/query/services/apis/google_geocode.rb +35 -0
- data/lib/barometer/query/services/apis/noaa_station.rb +31 -0
- data/lib/barometer/query/services/apis/weather_id.rb +35 -0
- data/lib/barometer/query/services/apis/wunderground_timezone.rb +26 -0
- data/lib/barometer/query/services/apis/yahoo_placefinder.rb +35 -0
- data/lib/barometer/query/services/apis/yahoo_weather.rb +31 -0
- data/lib/barometer/query/services/from_weather_id.rb +64 -0
- data/lib/barometer/query/services/geonames_timezone.rb +18 -0
- data/lib/barometer/query/services/google_geocode.rb +106 -0
- data/lib/barometer/query/services/noaa_station_id.rb +28 -0
- data/lib/barometer/query/services/to_weather_id.rb +25 -0
- data/lib/barometer/query/services/to_woe_id.rb +29 -0
- data/lib/barometer/query/services/wunderground_timezone.rb +18 -0
- data/lib/barometer/query/services/yahoo_geocode.rb +69 -0
- data/lib/barometer/response.rb +12 -0
- data/lib/barometer/response/base.rb +57 -0
- data/lib/barometer/response/current.rb +27 -0
- data/lib/barometer/response/prediction.rb +41 -0
- data/lib/barometer/response/prediction_collection.rb +48 -0
- data/lib/barometer/utils.rb +17 -0
- data/lib/barometer/utils/address.rb +33 -0
- data/lib/barometer/utils/api.rb +30 -0
- data/lib/barometer/utils/config_reader.rb +40 -0
- data/lib/barometer/utils/get.rb +17 -0
- data/lib/barometer/utils/json_reader.rb +22 -0
- data/lib/barometer/utils/payload.rb +100 -0
- data/lib/barometer/utils/payload_request.rb +37 -0
- data/lib/barometer/utils/post.rb +24 -0
- data/lib/barometer/utils/time.rb +78 -0
- data/lib/barometer/{translations → utils/translations}/zone_codes.yml +0 -0
- data/lib/barometer/utils/versioned_registration.rb +70 -0
- data/lib/barometer/utils/xml_reader.rb +27 -0
- data/lib/barometer/utils/zone_code_lookup.rb +30 -0
- data/lib/barometer/version.rb +1 -1
- data/lib/barometer/weather.rb +44 -173
- data/lib/barometer/weather_service.rb +41 -0
- data/lib/barometer/weather_services/base.rb +50 -0
- data/lib/barometer/weather_services/forecast_io.rb +36 -0
- data/lib/barometer/weather_services/forecast_io/api.rb +22 -0
- data/lib/barometer/weather_services/forecast_io/query.rb +38 -0
- data/lib/barometer/weather_services/forecast_io/response.rb +31 -0
- data/lib/barometer/weather_services/forecast_io/response/current_weather.rb +78 -0
- data/lib/barometer/weather_services/forecast_io/response/forecasted_weather.rb +74 -0
- data/lib/barometer/weather_services/forecast_io/response/location.rb +19 -0
- data/lib/barometer/weather_services/forecast_io/response/timezone.rb +25 -0
- data/lib/barometer/weather_services/noaa.rb +21 -305
- data/lib/barometer/weather_services/noaa/current_api.rb +25 -0
- data/lib/barometer/weather_services/noaa/current_query.rb +30 -0
- data/lib/barometer/weather_services/noaa/current_response.rb +29 -0
- data/lib/barometer/weather_services/noaa/forecast_api.rb +25 -0
- data/lib/barometer/weather_services/noaa/forecast_query.rb +39 -0
- data/lib/barometer/weather_services/noaa/forecast_response.rb +28 -0
- data/lib/barometer/weather_services/noaa/response/current_location.rb +42 -0
- data/lib/barometer/weather_services/noaa/response/current_station.rb +46 -0
- data/lib/barometer/weather_services/noaa/response/current_weather.rb +82 -0
- data/lib/barometer/weather_services/noaa/response/forecasted_weather.rb +90 -0
- data/lib/barometer/weather_services/noaa/response/location.rb +19 -0
- data/lib/barometer/weather_services/noaa/response/timezone.rb +15 -0
- data/lib/barometer/weather_services/response.rb +9 -0
- data/lib/barometer/weather_services/response/location.rb +42 -0
- data/lib/barometer/weather_services/response/time_zone.rb +19 -0
- data/lib/barometer/weather_services/weather_bug.rb +24 -280
- data/lib/barometer/weather_services/weather_bug/current_api.rb +26 -0
- data/lib/barometer/weather_services/weather_bug/current_response.rb +33 -0
- data/lib/barometer/weather_services/weather_bug/forecast_api.rb +26 -0
- data/lib/barometer/weather_services/weather_bug/forecast_response.rb +29 -0
- data/lib/barometer/weather_services/weather_bug/query.rb +42 -0
- data/lib/barometer/weather_services/weather_bug/response/current_weather.rb +82 -0
- data/lib/barometer/weather_services/weather_bug/response/forecasted_weather.rb +67 -0
- data/lib/barometer/weather_services/weather_bug/response/location.rb +23 -0
- data/lib/barometer/weather_services/weather_bug/response/station.rb +43 -0
- data/lib/barometer/weather_services/weather_bug/response/sun.rb +32 -0
- data/lib/barometer/weather_services/weather_bug/response/time_helper.rb +52 -0
- data/lib/barometer/weather_services/weather_bug/response/timezone.rb +15 -0
- data/lib/barometer/weather_services/wunderground_v1.rb +32 -0
- data/lib/barometer/weather_services/wunderground_v1/current_api.rb +21 -0
- data/lib/barometer/weather_services/wunderground_v1/current_response.rb +31 -0
- data/lib/barometer/weather_services/wunderground_v1/forecast_api.rb +21 -0
- data/lib/barometer/weather_services/wunderground_v1/forecast_response.rb +33 -0
- data/lib/barometer/weather_services/wunderground_v1/query.rb +30 -0
- data/lib/barometer/weather_services/wunderground_v1/response/current_weather.rb +92 -0
- data/lib/barometer/weather_services/wunderground_v1/response/forecasted_weather.rb +87 -0
- data/lib/barometer/weather_services/wunderground_v1/response/full_timezone.rb +22 -0
- data/lib/barometer/weather_services/wunderground_v1/response/location.rb +43 -0
- data/lib/barometer/weather_services/wunderground_v1/response/station.rb +39 -0
- data/lib/barometer/weather_services/wunderground_v1/response/sun.rb +53 -0
- data/lib/barometer/weather_services/wunderground_v1/response/timezone.rb +15 -0
- data/lib/barometer/weather_services/yahoo.rb +16 -198
- data/lib/barometer/weather_services/yahoo/api.rb +21 -0
- data/lib/barometer/weather_services/yahoo/query.rb +42 -0
- data/lib/barometer/weather_services/yahoo/response.rb +39 -0
- data/lib/barometer/weather_services/yahoo/response/current_weather.rb +86 -0
- data/lib/barometer/weather_services/yahoo/response/forecasted_weather.rb +71 -0
- data/lib/barometer/weather_services/yahoo/response/location.rb +47 -0
- data/lib/barometer/weather_services/yahoo/response/sun.rb +43 -0
- data/lib/barometer/weather_services/yahoo/response/timezone.rb +15 -0
- data/spec/barometer_spec.rb +18 -120
- data/spec/base_spec.rb +114 -0
- data/spec/cassettes/Converter_FromCoordinatesToNoaaStationId.json +1 -0
- data/spec/cassettes/Converter_FromWeatherIdToGeocode.json +1 -0
- data/spec/cassettes/Converter_FromWoeIdOrIpv4ToGeocode.json +1 -0
- data/spec/cassettes/Converter_ToGeocode.json +1 -0
- data/spec/cassettes/Converter_ToWeatherId.json +1 -0
- data/spec/cassettes/Converter_ToWoeId.json +1 -0
- data/spec/cassettes/Service_FromWeatherId.json +1 -0
- data/spec/cassettes/Service_GoogleGeocode.json +1 -0
- data/spec/cassettes/Service_NoaaStation.json +1 -0
- data/spec/cassettes/Service_ToWeatherId.json +1 -0
- data/spec/cassettes/Service_ToWoeId.json +1 -0
- data/spec/cassettes/Service_YahooGeocode.json +1 -0
- data/spec/cassettes/WeatherService_ForecastIo.json +1 -0
- data/spec/cassettes/WeatherService_Noaa.json +1 -1
- data/spec/cassettes/WeatherService_WeatherBug.json +1 -1
- data/spec/cassettes/WeatherService_WundergroundV1.json +1 -0
- data/spec/cassettes/WeatherService_Yahoo.json +1 -1
- data/spec/data/attributes/distance_spec.rb +60 -0
- data/spec/data/attributes/location_spec.rb +41 -0
- data/spec/data/attributes/pressure_spec.rb +60 -0
- data/spec/data/attributes/sun_spec.rb +33 -0
- data/spec/data/attributes/temperature_spec.rb +60 -0
- data/spec/data/attributes/time_spec.rb +58 -0
- data/spec/data/attributes/vector_spec.rb +43 -0
- data/spec/data/attributes/zone_spec.rb +34 -0
- data/spec/data/convertable_units_spec.rb +299 -0
- data/spec/data/coordinates_spec.rb +15 -0
- data/spec/data/distance_spec.rb +49 -333
- data/spec/data/geo_spec.rb +72 -71
- data/spec/data/location_spec.rb +70 -65
- data/spec/data/pressure_spec.rb +49 -333
- data/spec/data/sun_spec.rb +57 -81
- data/spec/data/temperature_spec.rb +49 -393
- data/spec/data/vector_spec.rb +100 -0
- data/spec/data/zone_spec.rb +199 -266
- data/spec/query/base_spec.rb +296 -0
- data/spec/query/converter_spec.rb +98 -0
- data/spec/query/converters/from_coordinates_to_noaa_station_id_spec.rb +35 -0
- data/spec/query/converters/from_geocode_to_coordinates_spec.rb +25 -0
- data/spec/query/converters/from_short_zipcode_to_zipcode_spec.rb +31 -0
- data/spec/query/converters/from_weather_id_to_geocode_spec.rb +40 -0
- data/spec/query/converters/from_woe_id_or_ipv4_to_geocode_spec.rb +51 -0
- data/spec/query/converters/to_geocode_spec.rb +99 -0
- data/spec/query/converters/to_weather_id_spec.rb +35 -0
- data/spec/query/converters/to_woe_id_spec.rb +74 -0
- data/spec/query/formats/base_spec.rb +15 -0
- data/spec/query/formats/coordinates_spec.rb +13 -0
- data/spec/query/formats/geocode_spec.rb +9 -0
- data/spec/query/formats/icao_spec.rb +20 -0
- data/spec/query/formats/ipv4_address_spec.rb +33 -0
- data/spec/query/formats/noaa_station_id_spec.rb +9 -0
- data/spec/query/formats/postalcode_spec.rb +18 -0
- data/spec/query/formats/short_zipcode_spec.rb +18 -0
- data/spec/query/formats/unknown_spec.rb +9 -0
- data/spec/query/formats/weather_id_spec.rb +31 -0
- data/spec/query/formats/woe_id_spec.rb +45 -0
- data/spec/query/formats/zipcode_spec.rb +18 -0
- data/spec/query/formats_spec.rb +55 -0
- data/spec/query/services/from_weather_id_spec.rb +54 -0
- data/spec/query/services/google_geocode_spec.rb +60 -0
- data/spec/query/services/noaa_station_id_spec.rb +23 -0
- data/spec/query/services/to_weather_id_spec.rb +23 -0
- data/spec/query/services/to_woe_id_spec.rb +51 -0
- data/spec/query/services/yahoo_geocode_spec.rb +72 -0
- data/spec/response/base_spec.rb +103 -0
- data/spec/response/current_spec.rb +33 -0
- data/spec/response/predicition_spec.rb +65 -0
- data/spec/response/prediction_collection_spec.rb +97 -0
- data/spec/spec_helper.rb +14 -12
- data/spec/support/key_file_parser.rb +22 -0
- data/spec/support/matchers/formats.rb +36 -0
- data/spec/support/matchers/have_data.rb +46 -0
- data/spec/support/matchers/have_field.rb +128 -0
- data/spec/support/matchers/have_forecast.rb +46 -0
- data/spec/support/matchers/path.rb +23 -0
- data/spec/support/query_factory.rb +20 -0
- data/spec/utils/address_spec.rb +67 -0
- data/spec/utils/config_reader_spec.rb +157 -0
- data/spec/utils/get_spec.rb +22 -0
- data/spec/utils/payload_request_spec.rb +69 -0
- data/spec/utils/payload_spec.rb +168 -0
- data/spec/utils/post_spec.rb +20 -0
- data/spec/utils/time_spec.rb +93 -0
- data/spec/utils/versioned_registration_spec.rb +105 -0
- data/spec/weather_services/base_spec.rb +116 -0
- data/spec/weather_services/forecast_io_spec.rb +71 -0
- data/spec/weather_services/noaa/current_response_spec.rb +22 -0
- data/spec/weather_services/noaa/forecast_response_spec.rb +33 -0
- data/spec/weather_services/noaa_spec.rb +68 -166
- data/spec/weather_services/weather_bug/current_response_spec.rb +64 -0
- data/spec/weather_services/weather_bug/forecast_response_spec.rb +23 -0
- data/spec/weather_services/weather_bug_spec.rb +62 -202
- data/spec/weather_services/wunderground_v1/current_response_spec.rb +19 -0
- data/spec/weather_services/wunderground_v1/forecast_response_spec.rb +62 -0
- data/spec/weather_services/wunderground_v1_spec.rb +78 -0
- data/spec/weather_services/yahoo/response_spec.rb +73 -0
- data/spec/weather_services/yahoo_spec.rb +52 -135
- data/spec/weather_services_spec.rb +103 -0
- data/spec/weather_spec.rb +167 -347
- metadata +342 -123
- data/.document +0 -5
- data/TODO +0 -60
- data/bin/barometer +0 -441
- data/lib/barometer/data/local_datetime.rb +0 -145
- data/lib/barometer/data/local_time.rb +0 -134
- data/lib/barometer/data/speed.rb +0 -158
- data/lib/barometer/data/units.rb +0 -49
- data/lib/barometer/formats.rb +0 -13
- data/lib/barometer/formats/coordinates.rb +0 -57
- data/lib/barometer/formats/format.rb +0 -64
- data/lib/barometer/formats/geocode.rb +0 -60
- data/lib/barometer/formats/icao.rb +0 -37
- data/lib/barometer/formats/postalcode.rb +0 -22
- data/lib/barometer/formats/short_zipcode.rb +0 -17
- data/lib/barometer/formats/weather_id.rb +0 -92
- data/lib/barometer/formats/woe_id.rb +0 -150
- data/lib/barometer/formats/zipcode.rb +0 -31
- data/lib/barometer/key_file_parser.rb +0 -20
- data/lib/barometer/measurements/measurement.rb +0 -202
- data/lib/barometer/measurements/result.rb +0 -207
- data/lib/barometer/measurements/result_array.rb +0 -75
- data/lib/barometer/services.rb +0 -19
- data/lib/barometer/weather_services/service.rb +0 -189
- data/lib/barometer/weather_services/wunderground.rb +0 -264
- data/lib/barometer/web_services/geocode.rb +0 -34
- data/lib/barometer/web_services/noaa_station_id.rb +0 -53
- data/lib/barometer/web_services/placemaker.rb +0 -95
- data/lib/barometer/web_services/timezone.rb +0 -38
- data/lib/barometer/web_services/weather_id.rb +0 -50
- data/lib/barometer/web_services/web_service.rb +0 -29
- data/spec/cassettes/Barometer.json +0 -1
- data/spec/cassettes/Query.json +0 -1
- data/spec/cassettes/Query_Format_Coordinates.json +0 -1
- data/spec/cassettes/Query_Format_Geocode.json +0 -1
- data/spec/cassettes/Query_Format_WeatherID.json +0 -1
- data/spec/cassettes/Query_Format_WoeID.json +0 -1
- data/spec/cassettes/WeatherService.json +0 -1
- data/spec/cassettes/WeatherService_Wunderground.json +0 -1
- data/spec/cassettes/WebService_Geocode.json +0 -1
- data/spec/cassettes/WebService_NoaaStation.json +0 -1
- data/spec/data/local_datetime_spec.rb +0 -274
- data/spec/data/local_time_spec.rb +0 -239
- data/spec/data/speed_spec.rb +0 -374
- data/spec/data/units_spec.rb +0 -101
- data/spec/formats/coordinates_spec.rb +0 -166
- data/spec/formats/format_spec.rb +0 -74
- data/spec/formats/geocode_spec.rb +0 -163
- data/spec/formats/icao_spec.rb +0 -55
- data/spec/formats/postalcode_spec.rb +0 -53
- data/spec/formats/short_zipcode_spec.rb +0 -47
- data/spec/formats/weather_id_spec.rb +0 -182
- data/spec/formats/woe_id_spec.rb +0 -211
- data/spec/formats/zipcode_spec.rb +0 -103
- data/spec/key_file_parser_spec.rb +0 -28
- data/spec/measurements/measurement_spec.rb +0 -381
- data/spec/measurements/result_array_spec.rb +0 -150
- data/spec/measurements/result_spec.rb +0 -632
- data/spec/query_spec.rb +0 -498
- data/spec/weather_services/services_spec.rb +0 -135
- data/spec/weather_services/wunderground_spec.rb +0 -179
- data/spec/web_services/geocode_spec.rb +0 -31
- data/spec/web_services/noaa_station_id_spec.rb +0 -33
- data/spec/web_services/placemaker_spec.rb +0 -41
- data/spec/web_services/web_services_spec.rb +0 -20
@@ -0,0 +1,296 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
module Barometer
|
4
|
+
describe Query::Base do
|
5
|
+
describe '.initialize' do
|
6
|
+
describe 'detecting the query format' do
|
7
|
+
it 'detects :short_zipcode' do
|
8
|
+
query = Query::Base.new('90210')
|
9
|
+
expect( query.format ).to eq :short_zipcode
|
10
|
+
expect( query.geo.country_code ).to eq 'US'
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'detects :zipcode' do
|
14
|
+
query = Query::Base.new('90210-5555')
|
15
|
+
expect( query.format ).to eq :zipcode
|
16
|
+
expect( query.geo.country_code ).to eq 'US'
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'detects :postalcode' do
|
20
|
+
query = Query::Base.new('T5B 4M9')
|
21
|
+
expect( query.format ).to eq :postalcode
|
22
|
+
expect( query.geo.country_code ).to eq 'CA'
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'detects :icao' do
|
26
|
+
query = Query::Base.new('KSFO')
|
27
|
+
expect( query.format ).to eq :icao
|
28
|
+
expect( query.geo.country_code ).to eq 'US'
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'detects :weather_id' do
|
32
|
+
query = Query::Base.new('USGA0028')
|
33
|
+
expect( query.format ).to eq :weather_id
|
34
|
+
expect( query.geo.country_code ).to eq 'US'
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'detects :coordinates' do
|
38
|
+
query = Query::Base.new('40.756054,-73.986951')
|
39
|
+
expect( query.format ).to eq :coordinates
|
40
|
+
expect( query.geo.country_code ).to be_nil
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'defaults to :unknown' do
|
44
|
+
query = Query::Base.new('New York, NY')
|
45
|
+
expect( query.format ).to eq :unknown
|
46
|
+
expect( query.geo.country_code ).to be_nil
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#metric?' do
|
52
|
+
it 'returns true when units are not specified' do
|
53
|
+
query = Query::Base.new('New York, NY')
|
54
|
+
expect( query ).to be_metric
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'returns true when units are set to metric' do
|
58
|
+
query = Query::Base.new('New York, NY', :metric)
|
59
|
+
expect( query ).to be_metric
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'returns false when units are set to imperial' do
|
63
|
+
query = Query::Base.new('New York, NY', :imperial)
|
64
|
+
expect( query ).not_to be_metric
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '#add_conversion' do
|
69
|
+
let(:query) { Query::Base.new('foo') }
|
70
|
+
|
71
|
+
it "adds a new conversion" do
|
72
|
+
query.add_conversion(:geocode, 'Paris')
|
73
|
+
expect( query.get_conversion(:geocode).q ).to eq 'Paris'
|
74
|
+
end
|
75
|
+
|
76
|
+
it "overrides an existing conversion" do
|
77
|
+
query.add_conversion(:geocode, 'Paris')
|
78
|
+
|
79
|
+
query.add_conversion(:geocode, 'Berlin')
|
80
|
+
expect( query.get_conversion(:geocode).q ).to eq 'Berlin'
|
81
|
+
end
|
82
|
+
|
83
|
+
it "does not add a nil conversion" do
|
84
|
+
query.add_conversion(:geocode, nil)
|
85
|
+
expect( query.get_conversion(:geocode) ).to be_nil
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe '#get_conversion' do
|
90
|
+
let(:query) { Query::Base.new('somewhere') }
|
91
|
+
|
92
|
+
context 'when the requested format is that of the query' do
|
93
|
+
it 'returns self instead of a conversion' do
|
94
|
+
query = Query::Base.new('90210')
|
95
|
+
|
96
|
+
query.add_conversion(:short_zipcode, '10001')
|
97
|
+
|
98
|
+
converted_query = query.get_conversion(:short_zipcode)
|
99
|
+
expect( converted_query.q ).to eq '90210'
|
100
|
+
expect( converted_query.format ).to eq :short_zipcode
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'returns a saved conversion' do
|
105
|
+
query.add_conversion(:geocode, 'Paris')
|
106
|
+
|
107
|
+
converted_query = query.get_conversion(:geocode)
|
108
|
+
expect( converted_query.q ).to eq 'Paris'
|
109
|
+
expect( converted_query.format ).to eq :geocode
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'returns one saved conversion, when asked for multiple' do
|
113
|
+
query.add_conversion(:geocode, 'Paris')
|
114
|
+
|
115
|
+
converted_query = query.get_conversion(:zipcode, :geocode)
|
116
|
+
expect( converted_query.format ).to eq :geocode
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'respects preference order' do
|
120
|
+
query.add_conversion(:zipcode, '12345')
|
121
|
+
query.add_conversion(:geocode, 'Paris')
|
122
|
+
query.add_conversion(:woe_id, '615702')
|
123
|
+
|
124
|
+
converted_query = query.get_conversion(:geocode, :woe_id, :zipcode)
|
125
|
+
expect( converted_query.format ).to eq :geocode
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'returns nil if nothing found' do
|
129
|
+
expect( query.get_conversion(:geocode) ).to be_nil
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'includes the current country code value' do
|
133
|
+
query.add_conversion(:geocode, 'Paris')
|
134
|
+
|
135
|
+
query.geo = Data::Geo.new(country_code: nil)
|
136
|
+
expect( query.get_conversion(:geocode, :woe_id).geo.country_code ).to be_nil
|
137
|
+
|
138
|
+
query.geo = Data::Geo.new(country_code: 'FR')
|
139
|
+
expect( query.get_conversion(:geocode, :woe_id).geo.country_code ).to eq 'FR'
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'includes the current geo value' do
|
143
|
+
query = Query::Base.new('34.1030032,-118.4104684')
|
144
|
+
query.add_conversion(:geocode, 'Paris')
|
145
|
+
|
146
|
+
geo = Data::Geo.new(locality: 'New York')
|
147
|
+
query.geo = geo
|
148
|
+
|
149
|
+
expect( query.get_conversion(:geocode, :woe_id).geo.to_s ).to eq geo.to_s
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'includes the current units value' do
|
153
|
+
query = Query::Base.new('34.1030032,-118.4104684', :imperial)
|
154
|
+
query.add_conversion(:geocode, 'Paris')
|
155
|
+
|
156
|
+
expect( query.get_conversion(:geocode, :woe_id).units ).to eq :imperial
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
describe '#convert!' do
|
161
|
+
context 'when the query can be converted directly to the requested format' do
|
162
|
+
it 'creates a conversion for the requested format' do
|
163
|
+
coordinates = ConvertedQuery.new('12.34,-56.78', :coordinates)
|
164
|
+
coordinates_converter = double(:converter_instance, call: coordinates)
|
165
|
+
coordinates_converter_klass = double(:coordinates_converter, new: coordinates_converter)
|
166
|
+
|
167
|
+
Query::Converter.stub(find_all: [{coordinates: coordinates_converter_klass}])
|
168
|
+
|
169
|
+
query = Query::Base.new('90210')
|
170
|
+
|
171
|
+
converted_query = query.convert!(:coordinates)
|
172
|
+
expect( converted_query.q ).to eq '12.34,-56.78'
|
173
|
+
expect( converted_query.format ).to eq :coordinates
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
context 'when the query has already been converted to the requested format' do
|
178
|
+
it 'uses the existing conversion' do
|
179
|
+
query = Query::Base.new('10001')
|
180
|
+
query.add_conversion(:coordinates, '12.34,-56.78')
|
181
|
+
|
182
|
+
converted_query = query.convert!(:coordinates)
|
183
|
+
expect( converted_query.q ).to eq '12.34,-56.78'
|
184
|
+
expect( converted_query.format ).to eq :coordinates
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
context 'when the query can be converted via geocoding to the requested format' do
|
189
|
+
it 'creates a conversion for the requested format' do
|
190
|
+
coordinates = ConvertedQuery.new('12.34,-56.78', :coordinates)
|
191
|
+
coordinates_converter = double(:converter_instance, call: coordinates)
|
192
|
+
coordinates_converter_klass = double(:coordinates_converter, new: coordinates_converter)
|
193
|
+
|
194
|
+
geocode = ConvertedQuery.new('Foo Bar', :geocode)
|
195
|
+
geocode_converter = double(:geocode_converter_instance, call: geocode)
|
196
|
+
geocode_converter_klass = double(:geocode_converter, new: geocode_converter)
|
197
|
+
|
198
|
+
Query::Converter.stub(find_all: [
|
199
|
+
{geocode: geocode_converter_klass},
|
200
|
+
{coordinates: coordinates_converter_klass}
|
201
|
+
])
|
202
|
+
|
203
|
+
query = Query::Base.new('90210')
|
204
|
+
|
205
|
+
converted_query = query.convert!(:coordinates)
|
206
|
+
expect( converted_query.q ).to eq '12.34,-56.78'
|
207
|
+
expect( converted_query.format ).to eq :coordinates
|
208
|
+
end
|
209
|
+
|
210
|
+
it 'uses any exisiting intermediate conversions' do
|
211
|
+
coordinates = ConvertedQuery.new('12.34,-56.78', :coordinates)
|
212
|
+
coordinates_converter = double(:converter_instance, call: coordinates)
|
213
|
+
coordinates_converter_klass = double(:coordinates_converter, new: coordinates_converter, from: [:geocode])
|
214
|
+
|
215
|
+
geocode_converter_klass = double(:geocode_converter)
|
216
|
+
|
217
|
+
Query::Converter.stub(find_all: [
|
218
|
+
{geocode: geocode_converter_klass},
|
219
|
+
{coordinates: coordinates_converter_klass}
|
220
|
+
])
|
221
|
+
|
222
|
+
query = Query::Base.new('90210')
|
223
|
+
query.add_conversion(:geocode, 'Foo Bar')
|
224
|
+
query.geo = Data::Geo.new(latitude: 12.34, longitude: -56.78)
|
225
|
+
|
226
|
+
converted_query = query.convert!(:coordinates)
|
227
|
+
expect( converted_query.q ).to eq '12.34,-56.78'
|
228
|
+
expect( converted_query.format ).to eq :coordinates
|
229
|
+
end
|
230
|
+
|
231
|
+
it 'returns a new intermediate conversion if preferred' do
|
232
|
+
class FakeGeocodeConverter
|
233
|
+
def initialize(query); @query = query; end
|
234
|
+
def call
|
235
|
+
@query.add_conversion(:woe_id, '12345678')
|
236
|
+
@query.add_conversion(:geocode, 'Foo Bar')
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
class FakeCoordinatesConverter
|
241
|
+
def initialize(query); @query = query; end
|
242
|
+
def call
|
243
|
+
@query.add_conversion(:coordinates, '12.34,-56.78')
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
Query::Converter.stub(find_all: [
|
248
|
+
{geocode: FakeGeocodeConverter},
|
249
|
+
{coordinates: FakeCoordinatesConverter}
|
250
|
+
])
|
251
|
+
|
252
|
+
query = Query::Base.new('90210')
|
253
|
+
|
254
|
+
converted_query = query.convert!(:woe_id, :coordinates)
|
255
|
+
expect( converted_query.q ).to eq '12345678'
|
256
|
+
expect( converted_query.format ).to eq :woe_id
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
context 'when the query cannot be converted to the requested format' do
|
261
|
+
it 'raises ConversionNotPossible' do
|
262
|
+
query = Query::Base.new('90210')
|
263
|
+
|
264
|
+
Query::Converter.stub(find_all: [])
|
265
|
+
|
266
|
+
expect {
|
267
|
+
query.convert!(:zipcode)
|
268
|
+
}.to raise_error{ Query::ConversionNotPossible }
|
269
|
+
end
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
describe '#geo=' do
|
274
|
+
it 'updates the current geo values' do
|
275
|
+
query = Query::Base.new('90210')
|
276
|
+
query.geo = Data::Geo.new(locality: 'foo', region: 'bar')
|
277
|
+
|
278
|
+
geo = Data::Geo.new(latitude: 12.34, longitude: -56.78, country_code: 'FB')
|
279
|
+
|
280
|
+
query.geo = geo
|
281
|
+
expect( query.geo.locality ).to eq 'foo'
|
282
|
+
expect( query.geo.region ).to eq 'bar'
|
283
|
+
expect( query.geo.country_code ).to eq 'US'
|
284
|
+
expect( query.geo.latitude ).to eq 12.34
|
285
|
+
expect( query.geo.longitude ).to eq -56.78
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
describe '#to_s' do
|
290
|
+
it 'returns the query q value' do
|
291
|
+
query = Query::Base.new('90210')
|
292
|
+
expect( query.to_s ).to eq '90210'
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
describe Barometer::Query::Converter do
|
4
|
+
def clear_converters
|
5
|
+
@converters_cache = Barometer::Query::Converter.converters
|
6
|
+
Barometer::Query::Converter.converters = {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def reset_conveters
|
10
|
+
Barometer::Query::Converter.converters = @converters_cache
|
11
|
+
end
|
12
|
+
|
13
|
+
describe ".register" do
|
14
|
+
before { clear_converters }
|
15
|
+
after { reset_conveters }
|
16
|
+
|
17
|
+
context "when a 1:1 converter is registered" do
|
18
|
+
it "makes the converter available" do
|
19
|
+
zipcode_converter = double(:converter, from: [:short_zipcode])
|
20
|
+
Barometer::Query::Converter.register(:zipcode, zipcode_converter)
|
21
|
+
Barometer::Query::Converter.find(:short_zipcode, :zipcode).should == {zipcode: zipcode_converter}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "when a 2:1 converter is registered" do
|
26
|
+
it "makes the converter available to each format" do
|
27
|
+
coordinates_converter = double(:converter, from: [:short_zipcode, :zipcode])
|
28
|
+
Barometer::Query::Converter.register(:coordinates, coordinates_converter)
|
29
|
+
Barometer::Query::Converter.find(:short_zipcode, :coordinates).should == {coordinates: coordinates_converter}
|
30
|
+
Barometer::Query::Converter.find(:zipcode, :coordinates).should == {coordinates: coordinates_converter}
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe ".find_all" do
|
36
|
+
before { clear_converters }
|
37
|
+
after { reset_conveters }
|
38
|
+
|
39
|
+
context "when the conversion can be made directly" do
|
40
|
+
it "returns the one converter" do
|
41
|
+
coordinates_converter = double(:coordinates_converter, from: [:short_zipcode])
|
42
|
+
Barometer::Query::Converter.register(:coordinates, coordinates_converter)
|
43
|
+
|
44
|
+
converters = Barometer::Query::Converter.find_all(:short_zipcode, :coordinates)
|
45
|
+
converters.should == [{coordinates: coordinates_converter}]
|
46
|
+
end
|
47
|
+
|
48
|
+
it "respects preference" do
|
49
|
+
zipcode_converter = double(:zipcode_converter, from: [:short_zipcode])
|
50
|
+
Barometer::Query::Converter.register(:zipcode, zipcode_converter)
|
51
|
+
|
52
|
+
coordinates_converter = double(:coordinates_converter, from: [:short_zipcode])
|
53
|
+
Barometer::Query::Converter.register(:coordinates, coordinates_converter)
|
54
|
+
|
55
|
+
converters = Barometer::Query::Converter.find_all(:short_zipcode, [:foo, :coordinates, :zipcode])
|
56
|
+
converters.should == [{coordinates: coordinates_converter}]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context "when the conversion can only be made indirecty" do
|
61
|
+
it "returns multiple converters" do
|
62
|
+
coordinates_converter = double(:coordinates_converter, from: [:geocode])
|
63
|
+
Barometer::Query::Converter.register(:coordinates, coordinates_converter)
|
64
|
+
|
65
|
+
geocode_converter = double(:geocode_converter, from: [:short_zipcode])
|
66
|
+
Barometer::Query::Converter.register(:geocode, geocode_converter)
|
67
|
+
|
68
|
+
converters = Barometer::Query::Converter.find_all(:short_zipcode, :coordinates)
|
69
|
+
converters.should == [
|
70
|
+
{geocode: geocode_converter}, {coordinates: coordinates_converter}
|
71
|
+
]
|
72
|
+
end
|
73
|
+
|
74
|
+
it "respects preference" do
|
75
|
+
zipcode_converter = double(:zipcode_converter, from: [:geocode])
|
76
|
+
Barometer::Query::Converter.register(:zipcode, zipcode_converter)
|
77
|
+
|
78
|
+
coordinates_converter = double(:coordinates_converter, from: [:geocode])
|
79
|
+
Barometer::Query::Converter.register(:coordinates, coordinates_converter)
|
80
|
+
|
81
|
+
geocode_converter = double(:geocode_converter, from: [:short_zipcode])
|
82
|
+
Barometer::Query::Converter.register(:geocode, geocode_converter)
|
83
|
+
|
84
|
+
converters = Barometer::Query::Converter.find_all(:short_zipcode, [:foo, :coordinates, :zipcode])
|
85
|
+
converters.should == [
|
86
|
+
{geocode: geocode_converter}, {coordinates: coordinates_converter}
|
87
|
+
]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context "when the conversion cannot be made" do
|
92
|
+
it "returns nil" do
|
93
|
+
converters = Barometer::Query::Converter.find_all(:short_zipcode, :foo)
|
94
|
+
converters.should be_empty
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
describe Barometer::Query::Converter::FromCoordinatesToNoaaStationId, vcr: {
|
4
|
+
match_requests_on: [:method, :uri],
|
5
|
+
cassette_name: "Converter::FromCoordinatesToNoaaStationId"
|
6
|
+
} do
|
7
|
+
|
8
|
+
it "converts :coordinates -> :noaa_station_id" do
|
9
|
+
query = Barometer::Query.new('34.10,-118.41')
|
10
|
+
|
11
|
+
converter = Barometer::Query::Converter::FromCoordinatesToNoaaStationId.new(query)
|
12
|
+
converted_query = converter.call
|
13
|
+
|
14
|
+
converted_query.q.should == 'KSMO'
|
15
|
+
converted_query.format.should == :noaa_station_id
|
16
|
+
end
|
17
|
+
|
18
|
+
it "uses a previous coversion (if needed) on the query" do
|
19
|
+
query = Barometer::Query.new('90210')
|
20
|
+
query.add_conversion(:coordinates, '34.10,-118.41')
|
21
|
+
|
22
|
+
converter = Barometer::Query::Converter::FromCoordinatesToNoaaStationId.new(query)
|
23
|
+
converted_query = converter.call
|
24
|
+
|
25
|
+
converted_query.q.should == 'KSMO'
|
26
|
+
converted_query.format.should == :noaa_station_id
|
27
|
+
end
|
28
|
+
|
29
|
+
it "does not convert any other format" do
|
30
|
+
query = Barometer::Query.new('KJFK')
|
31
|
+
|
32
|
+
converter = Barometer::Query::Converter::FromCoordinatesToNoaaStationId.new(query)
|
33
|
+
converter.call.should be_nil
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
describe Barometer::Query::Converter::FromGeocodeToCoordinates do
|
4
|
+
it "converts :geocode -> :coordinates" do
|
5
|
+
query = Barometer::Query.new('USNY0996')
|
6
|
+
query.add_conversion(:geocode, 'New York, NY')
|
7
|
+
|
8
|
+
query.geo = Barometer::Data::Geo.new(latitude: 40.7143528, longitude: -74.0059731, country_code: 'US')
|
9
|
+
|
10
|
+
converter = Barometer::Query::Converter::FromGeocodeToCoordinates.new(query)
|
11
|
+
converted_query = converter.call
|
12
|
+
|
13
|
+
converted_query.q.should == '40.7143528,-74.0059731'
|
14
|
+
converted_query.format.should == :coordinates
|
15
|
+
converted_query.geo.country_code.should == 'US'
|
16
|
+
converted_query.geo.should_not be_nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "does not convert other formats" do
|
20
|
+
query = Barometer::Query.new('USGA0028')
|
21
|
+
|
22
|
+
converter = Barometer::Query::Converter::FromGeocodeToCoordinates.new(query)
|
23
|
+
converter.call.should be_nil
|
24
|
+
end
|
25
|
+
end
|