diff --git a/lib/google_maps_service/apis/directions.rb b/lib/google_maps_service/apis/directions.rb index 6ae9fa2..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 \ 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