From 93151a3b0caffd2ae6d6c77675466892889e8f3f Mon Sep 17 00:00:00 2001 From: J Guthrie Date: Sat, 3 Jun 2017 18:40:47 +0100 Subject: [PATCH 1/2] Add GoogleMapsService::Error::NotFoundError - When doing a directions search, if you enter an invalid address (i.e. 'dsadsfdfdscfrcerf, Tanzania'), a NotFoundError is raised --- lib/google_maps_service/apis/directions.rb | 2 +- lib/google_maps_service/client.rb | 4 +++- lib/google_maps_service/errors.rb | 6 +++++- spec/google_maps_service/apis/directions_spec.rb | 14 +++++++++++++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/google_maps_service/apis/directions.rb b/lib/google_maps_service/apis/directions.rb index 6ae9fa2..87ee3af 100644 --- a/lib/google_maps_service/apis/directions.rb +++ b/lib/google_maps_service/apis/directions.rb @@ -98,4 +98,4 @@ def directions(origin, destination, return get('/maps/api/directions/json', params)[:routes] end end -end \ No newline at end of file +end diff --git a/lib/google_maps_service/client.rb b/lib/google_maps_service/client.rb index 4c98c89..efecaa9 100644 --- a/lib/google_maps_service/client.rb +++ b/lib/google_maps_service/client.rb @@ -281,9 +281,11 @@ def check_body_error(response, body) raise GoogleMapsService::Error::RequestDeniedError.new(response), body[:error_message] when 'INVALID_REQUEST' raise GoogleMapsService::Error::InvalidRequestError.new(response), body[:error_message] + when 'NOT_FOUND' + raise GoogleMapsService::Error::NotFoundError.new(response), (body[:error_message] || 'ADDRESS NOT FOUND') else raise GoogleMapsService::Error::ApiError.new(response), body[:error_message] end end end -end \ No newline at end of file +end diff --git a/lib/google_maps_service/errors.rb b/lib/google_maps_service/errors.rb index 642ba0b..582bb7e 100644 --- a/lib/google_maps_service/errors.rb +++ b/lib/google_maps_service/errors.rb @@ -48,5 +48,9 @@ class RateLimitError < ApiError # An unathorized error occurred. It might be caused by invalid key/secret or invalid access. class RequestDeniedError < ApiError end + + # When an Address is not found. i.e. An address string could not be geocoded + class NotFoundError < ApiError + end end -end \ No newline at end of file +end diff --git a/spec/google_maps_service/apis/directions_spec.rb b/spec/google_maps_service/apis/directions_spec.rb index 7cebeb0..c45f9f7 100644 --- a/spec/google_maps_service/apis/directions_spec.rb +++ b/spec/google_maps_service/apis/directions_spec.rb @@ -71,6 +71,18 @@ end end + context 'unparseable / non-existing address' do + before(:example) do + stub_request(:get, /https:\/\/maps.googleapis.com\/maps\/api\/directions\/.*/) + .to_return(:status => 200, headers: { 'Content-Type' => 'application/json' }, body: '{"results": [], "status": "NOT_FOUND"}') + end + + it 'should raise GoogleMapsService::Error::NotFoundError' do + expect { client.directions('dsfdsfdsfdsfdsfadsfdrcgtzvhstrvzstrhstrgcrscgtr', 'Sydney Town Hall', + mode: 'driving') }.to raise_error GoogleMapsService::Error::NotFoundError + end + end + context 'travel mode round trip' do it 'should call Google Maps Web Service' do routes = client.directions('Town Hall, Sydney', 'Parramatta, NSW', @@ -146,4 +158,4 @@ expect(a_request(:get, 'https://maps.googleapis.com/maps/api/directions/json?origin=Sydney+Town+Hall&destination=Parramatta+Town+Hall&alternatives=true&key=%s' % api_key)).to have_been_made end end -end \ No newline at end of file +end From a86563b02954803a7aeec97f4d6f810a51a51e16 Mon Sep 17 00:00:00 2001 From: J Guthrie Date: Sat, 3 Jun 2017 20:44:50 +0100 Subject: [PATCH 2/2] Add an option that changes the directions return value (Issue #6) Added a feature that allows 'return_type: :all' to be passed into the client.directions method, which returns the full Hash output, rather than just the [:routes] entry Can change the argument to be 'return_type: :status' if you just want the status, or leave it blank to get the [:routes] value (i.e. the same output as is the case without this change) --- lib/google_maps_service/apis/directions.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/google_maps_service/apis/directions.rb b/lib/google_maps_service/apis/directions.rb index 87ee3af..dd7e8c0 100644 --- a/lib/google_maps_service/apis/directions.rb +++ b/lib/google_maps_service/apis/directions.rb @@ -63,7 +63,7 @@ def directions(origin, destination, mode: nil, waypoints: nil, alternatives: false, avoid: nil, language: nil, units: nil, region: nil, departure_time: nil, arrival_time: nil, optimize_waypoints: false, transit_mode: nil, - transit_routing_preference: nil) + transit_routing_preference: nil, return_type: "routes") params = { origin: GoogleMapsService::Convert.waypoint(origin), @@ -95,7 +95,11 @@ def directions(origin, destination, params[:transit_mode] = GoogleMapsService::Convert.join_list("|", transit_mode) if transit_mode params[:transit_routing_preference] = transit_routing_preference if transit_routing_preference - return get('/maps/api/directions/json', params)[:routes] + if return_type.to_sym == :all + return get('/maps/api/directions/json', params) + else + return get('/maps/api/directions/json', params)[return_type.to_sym] + end end end end