diff options
| -rw-r--r-- | lib/tom_tom/matrix.rb | 29 | ||||
| -rw-r--r-- | lib/tom_tom/matrix/point.rb | 6 | ||||
| -rw-r--r-- | spec/lib/tom_tom/matrix_spec.rb | 8 |
3 files changed, 22 insertions, 21 deletions
diff --git a/lib/tom_tom/matrix.rb b/lib/tom_tom/matrix.rb index 55568ab66..8e0331cca 100644 --- a/lib/tom_tom/matrix.rb +++ b/lib/tom_tom/matrix.rb @@ -29,23 +29,30 @@ module TomTom end def points_from_way_costs(way_costs) - points = Set.new + points = [] way_costs.each do |way_cost| departure_id, arrival_id = way_cost.id.split('-') - points.add( - TomTom::Matrix::Point.new( - way_cost.departure, - departure_id - ) + departure = TomTom::Matrix::Point.new( + way_cost.departure, + departure_id ) - points.add( - TomTom::Matrix::Point.new( - way_cost.arrival, - arrival_id - ) + arrival = TomTom::Matrix::Point.new( + way_cost.arrival, + arrival_id ) + + # Don't add duplicate coordinates. This assumes that + # `way_costs` consists of an ordered route of points where + # each departure coordinate is the same as the preceding + # arrival coordinate. + if points.empty? || + points.last.coordinates != departure.coordinates + points << departure + end + + points << arrival end points diff --git a/lib/tom_tom/matrix/point.rb b/lib/tom_tom/matrix/point.rb index 4e9d11e68..435b4d4b0 100644 --- a/lib/tom_tom/matrix/point.rb +++ b/lib/tom_tom/matrix/point.rb @@ -13,12 +13,6 @@ module TomTom @coordinates == other.coordinates && @id == other.id end - - alias :eql? :== - - def hash - @coordinates.hash + @id.hash - end end end end diff --git a/spec/lib/tom_tom/matrix_spec.rb b/spec/lib/tom_tom/matrix_spec.rb index e67e539b7..b63ebc70b 100644 --- a/spec/lib/tom_tom/matrix_spec.rb +++ b/spec/lib/tom_tom/matrix_spec.rb @@ -18,7 +18,7 @@ RSpec.describe TomTom::Matrix do expect( matrix.points_from_way_costs(way_costs) - ).to eq(Set.new([ + ).to eq([ TomTom::Matrix::Point.new( Geokit::LatLng.new(48.85086, 2.36143), '44' @@ -31,13 +31,13 @@ RSpec.describe TomTom::Matrix do Geokit::LatLng.new(52.50867, 13.42879), '88' ) - ])) + ]) end end describe "#points_as_params" do it "transforms a set of LatLng points into a hash for use by TomTom Matrix" do - points = Set.new([ + points = [ TomTom::Matrix::Point.new( Geokit::LatLng.new(48.85086, 2.36143), '44' @@ -50,7 +50,7 @@ RSpec.describe TomTom::Matrix do Geokit::LatLng.new(52.50867, 13.42879), '88' ) - ]) + ] expect( matrix.points_as_params(points) |
