diff --git a/lib/geocoder/results/geocodio.rb b/lib/geocoder/results/geocodio.rb index b240b1750fc6d9058b6e8c17f2df007366c8a9ed..49dc2a07715ac7fdaa0d1512315dcca498994ab9 100644 --- a/lib/geocoder/results/geocodio.rb +++ b/lib/geocoder/results/geocodio.rb @@ -24,16 +24,24 @@ module Geocoder::Result alias_method :state_code, :state def zip - address_components["zip"] + # Postal code is not returned for Canada geocode results + address_components["zip"] || "" end alias_method :postal_code, :zip def country - "United States" # Geocodio only supports the US + # Geocodio supports US and Canada, however they don't return the full + # country name. + + if country_code == "CA" + "Canada" + else + "United States" + end end def country_code - "US" # Geocodio only supports the US + address_components['country'] end def city diff --git a/test/fixtures/geocodio_1101_pennsylvania_ave b/test/fixtures/geocodio_1101_pennsylvania_ave index c907deeeb1298bd2f9171e19502e776b33d85dcd..4b6a0627a1d1a5376831c9cb233cf8697efc8c55 100644 --- a/test/fixtures/geocodio_1101_pennsylvania_ave +++ b/test/fixtures/geocodio_1101_pennsylvania_ave @@ -1 +1 @@ -{"input":{"address_components":{"number":"1101","street":"Pennsylvania","suffix":"Ave","postdirectional":"NW","formatted_street":"Pennsylvania Ave NW","city":"Washington","state":"DC"},"formatted_address":"1101 Pennsylvania Ave NW, Washington, DC"},"results":[{"address_components":{"number":"1101","street":"Pennsylvania","suffix":"Ave","postdirectional":"NW","formatted_street":"Pennsylvania Ave NW","city":"Washington","county":"District of Columbia","state":"DC","zip":"20004"},"formatted_address":"1101 Pennsylvania Ave NW, Washington, DC 20004","location":{"lat":38.895019,"lng":-77.028095},"accuracy":1,"accuracy_type":"range_interpolation"},{"address_components":{"number":"1101","street":"Pennsylvania","suffix":"Ave","postdirectional":"NW","formatted_street":"Pennsylvania Ave NW","city":"Washington","county":"District of Columbia","state":"DC","zip":"20004"},"formatted_address":"1101 Pennsylvania Ave NW, Washington, DC 20004","location":{"lat":38.895016122449,"lng":-77.028084377551},"accuracy":0.8,"accuracy_type":"range_interpolation"}]} \ No newline at end of file +{"input":{"address_components":{"number":"1101","street":"Pennsylvania","suffix":"Ave","postdirectional":"NW","formatted_street":"Pennsylvania Ave NW","city":"Washington","state":"DC","zip":"20001","country":"US"},"formatted_address":"1101 Pennsylvania Ave NW, Washington, DC 20001"},"results":[{"address_components":{"number":"1101","street":"Pennsylvania","suffix":"Ave","postdirectional":"NW","formatted_street":"Pennsylvania Ave NW","city":"Washington","county":"District of Columbia","state":"DC","zip":"20004","country":"US"},"formatted_address":"1101 Pennsylvania Ave NW, Washington, DC 20004","location":{"lat":38.895156,"lng":-77.027405},"accuracy":1,"accuracy_type":"rooftop","source":"DC Geographic Information Systems Program (DC GIS)"}]} diff --git a/test/fixtures/geocodio_reverse b/test/fixtures/geocodio_reverse new file mode 100644 index 0000000000000000000000000000000000000000..9930dfab2725ac76ffc8497690cce532004dc7fe --- /dev/null +++ b/test/fixtures/geocodio_reverse @@ -0,0 +1 @@ +{"results":[{"address_components":{"number":"483","street":"Bay","suffix":"St","formatted_street":"Bay St","city":"Toronto","state":"ON","country":"CA"},"formatted_address":"483 Bay St, Toronto, ON","location":{"lat":43.652961,"lng":-79.382624},"accuracy":1,"accuracy_type":"nearest_street","source":"CanVec+ by Natural Resources Canada"},{"address_components":{"number":"20","street":"Albert","suffix":"St","formatted_street":"Albert St","city":"Toronto","state":"ON","country":"CA"},"formatted_address":"20 Albert St, Toronto, ON","location":{"lat":43.652961,"lng":-79.382624},"accuracy":0.16,"accuracy_type":"nearest_street","source":"CanVec+ by Natural Resources Canada"}]} diff --git a/test/unit/lookups/geocodio_test.rb b/test/unit/lookups/geocodio_test.rb index 6b316be9825d9e726e87d9f7f9dd28664bb50082..20db0f9ffd64994170dc9563dda13c50320146df 100644 --- a/test/unit/lookups/geocodio_test.rb +++ b/test/unit/lookups/geocodio_test.rb @@ -18,8 +18,22 @@ class GeocodioTest < GeocoderTestCase assert_equal "20004", result.zip assert_equal "NW", result.postdirectional assert_equal "Washington", result.city + assert_equal "US", result.country_code + assert_equal "United States", result.country assert_equal "1101 Pennsylvania Ave NW, Washington, DC 20004", result.formatted_address - assert_equal({ "lat" => 38.895019, "lng" => -77.028095 }, result.location) + assert_equal({ "lat" => 38.895156, "lng" => -77.027405 }, result.location) + end + + def test_reverse_canada_result + result = Geocoder.search([43.652961, -79.382624]).first + assert_equal 1.0, result.accuracy + assert_equal "483", result.number + assert_equal "Bay", result.street + assert_equal "St", result.suffix + assert_equal "ON", result.state + assert_equal "Toronto", result.city + assert_equal "CA", result.country_code + assert_equal "Canada", result.country end def test_no_results