From 55cea45757ea5cec882dfc571a641c96447a786a Mon Sep 17 00:00:00 2001 From: Ryan Boland <bolandryanm@gmail.com> Date: Mon, 5 Sep 2016 11:32:10 -0400 Subject: [PATCH] support canada for geocodio --- lib/geocoder/results/geocodio.rb | 11 +++++++++-- test/fixtures/geocodio_reverse | 1 + test/unit/lookups/geocodio_test.rb | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/geocodio_reverse diff --git a/lib/geocoder/results/geocodio.rb b/lib/geocoder/results/geocodio.rb index b240b175..dbe5605f 100644 --- a/lib/geocoder/results/geocodio.rb +++ b/lib/geocoder/results/geocodio.rb @@ -29,11 +29,18 @@ module Geocoder::Result 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'] || "US" end def city diff --git a/test/fixtures/geocodio_reverse b/test/fixtures/geocodio_reverse new file mode 100644 index 00000000..9930dfab --- /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 6b316be9..250ce410 100644 --- a/test/unit/lookups/geocodio_test.rb +++ b/test/unit/lookups/geocodio_test.rb @@ -18,10 +18,24 @@ 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) 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 results = Geocoder.search("no results") assert_equal 0, results.length -- GitLab