From 86a1c5d6b02a9c287fa8f5a8313ba06845697c27 Mon Sep 17 00:00:00 2001 From: Adrian Stoica Date: Fri, 5 Jan 2024 16:46:11 +0200 Subject: [PATCH] Add place_id as a result attribute fro amazon_location_service --- lib/geocoder/lookups/amazon_location_service.rb | 2 +- lib/geocoder/results/amazon_location_service.rb | 7 ++++++- .../amazon_location_service_madison_square_garden | 1 + test/test_helper.rb | 11 +++++------ test/unit/result_test.rb | 5 +++++ 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/geocoder/lookups/amazon_location_service.rb b/lib/geocoder/lookups/amazon_location_service.rb index e60d3df4e..7179430e2 100644 --- a/lib/geocoder/lookups/amazon_location_service.rb +++ b/lib/geocoder/lookups/amazon_location_service.rb @@ -19,7 +19,7 @@ def results(query) client.search_place_index_for_text(params.merge(text: query.text)) end - resp.results.map(&:place) + resp.results end private diff --git a/lib/geocoder/results/amazon_location_service.rb b/lib/geocoder/results/amazon_location_service.rb index 6cc175367..fc3ee693a 100644 --- a/lib/geocoder/results/amazon_location_service.rb +++ b/lib/geocoder/results/amazon_location_service.rb @@ -3,7 +3,8 @@ module Geocoder::Result class AmazonLocationService < Base def initialize(result) - @place = result + @place = result.place + super end def coordinates @@ -53,5 +54,9 @@ def country def country_code @place.country end + + def place_id + data.place_id if data.respond_to?(:place_id) + end end end diff --git a/test/fixtures/amazon_location_service_madison_square_garden b/test/fixtures/amazon_location_service_madison_square_garden index 48dbce46b..538d97493 100644 --- a/test/fixtures/amazon_location_service_madison_square_garden +++ b/test/fixtures/amazon_location_service_madison_square_garden @@ -1,4 +1,5 @@ [ + "AQABAFMAf5Cj6ApTW9YHMA3COqc3G1_dNHr1qtFl-W36hTyQd_Jw0f-t-7oaBxmbWd9vNecT3rIiH6O6DJ36qPk7seUIuIp8tOOZDoQnuweFUE5fHjvl15sTbn1PREQwp_66LjvkkubhQ3seXgBrCMZT3rt_dBzubg", nil, "USA", MockAWSPlaceGeometry.new([-74.15434739412053, 40.61681535865544]), diff --git a/test/test_helper.rb b/test/test_helper.rb index 83d049a68..b3e4301e4 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -553,11 +553,7 @@ def default_fixture_filename MockAWSPlace = Struct.new(*%i[ address_number country geometry label municipality neighborhood postal_code region street sub_region ]) - class MockAWSPlace - def place - self - end - end + MockAWSResult = Struct.new(:place_id, :place) class MockAmazonLocationServiceClient def search_place_index_for_position(params = {}, options = {}) @@ -578,7 +574,10 @@ def fixture end def mock_results - MockResults.new([MockAWSPlace.new(*fixture)]) + fixture_copy = fixture.dup + place_id = fixture_copy.shift + place = MockAWSPlace.new(*fixture_copy) + MockResults.new([MockAWSResult.new(place_id, place)]) end def mock_no_results diff --git a/test/unit/result_test.rb b/test/unit/result_test.rb index a37411c55..78097abe2 100644 --- a/test/unit/result_test.rb +++ b/test/unit/result_test.rb @@ -16,6 +16,7 @@ def test_forward_geocoding_result_has_required_attributes set_api_key!(l) result = Geocoder.search("Madison Square Garden").first assert_result_has_required_attributes(result) + assert_aws_result_supports_place_id(result) if l == :amazon_location_service end end @@ -56,4 +57,8 @@ def assert_result_has_required_attributes(result) assert result.country_code.is_a?(String), m % "country_code" assert_not_nil result.address, m % "address" end + + def assert_aws_result_supports_place_id(result) + assert result.place_id.is_a?(String) + end end