diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/tom_tom/errors.rb | 4 | ||||
| -rw-r--r-- | lib/tom_tom/errors/matrix_remote_error.rb | 5 | ||||
| -rw-r--r-- | lib/tom_tom/matrix.rb | 20 | 
3 files changed, 29 insertions, 0 deletions
| diff --git a/lib/tom_tom/errors.rb b/lib/tom_tom/errors.rb new file mode 100644 index 000000000..da3f2239c --- /dev/null +++ b/lib/tom_tom/errors.rb @@ -0,0 +1,4 @@ +module TomTom +  module Errors +  end +end diff --git a/lib/tom_tom/errors/matrix_remote_error.rb b/lib/tom_tom/errors/matrix_remote_error.rb new file mode 100644 index 000000000..b13767847 --- /dev/null +++ b/lib/tom_tom/errors/matrix_remote_error.rb @@ -0,0 +1,5 @@ +module TomTom +  module Errors +    class MatrixRemoteError < RuntimeError; end +  end +end diff --git a/lib/tom_tom/matrix.rb b/lib/tom_tom/matrix.rb index c418cd516..e779abbf7 100644 --- a/lib/tom_tom/matrix.rb +++ b/lib/tom_tom/matrix.rb @@ -4,6 +4,10 @@ module TomTom        @connection = connection      end +    # Exceptions: +    # +    # * This raises a `TomTom::Errors::MatrixRemoteError` when the API responds +    #   with an error.      def matrix(way_costs)        points_with_ids = points_from_way_costs(way_costs)        points = points_as_params(points_with_ids) @@ -21,6 +25,8 @@ module TomTom          req.body = build_request_body(points)        end +      check_for_error_response(response) +        extract_costs_to_way_costs!(          way_costs,          points_with_ids, @@ -77,6 +83,20 @@ module TomTom        })      end +    def check_for_error_response(response) +      if response.status != 200 +        raise TomTom::Errors::MatrixRemoteError, +          "status: #{response.status}, body: #{response.body}" +      end + +      json = JSON.parse(response.body) + +      if json.has_key?('error') +        raise TomTom::Errors::MatrixRemoteError, +          "status: #{response.status}, message: #{json['error']['description']}" +      end +    end +      def extract_costs_to_way_costs!(way_costs, points, matrix_json)        way_costs = [] | 
