diff options
| -rw-r--r-- | lib/tom_tom/matrix.rb | 12 | ||||
| -rw-r--r-- | lib/tom_tom/matrix/request_json_serializer.rb | 25 | ||||
| -rw-r--r-- | spec/lib/tom_tom/matrix/request_json_serializer_spec.rb | 39 | ||||
| -rw-r--r-- | spec/lib/tom_tom/matrix_spec.rb | 72 |
4 files changed, 144 insertions, 4 deletions
diff --git a/lib/tom_tom/matrix.rb b/lib/tom_tom/matrix.rb index 49c000148..71eb6c75d 100644 --- a/lib/tom_tom/matrix.rb +++ b/lib/tom_tom/matrix.rb @@ -15,10 +15,7 @@ module TomTom req.params[:routeType] = 'shortest' req.params[:travelMode] = 'bus' - req.body = { - origins: points, - destinations: points - }.to_json + req.body = build_request_body(points) end extract_costs_to_way_costs!( @@ -69,6 +66,13 @@ module TomTom end end + def build_request_body(points) + RequestJSONSerializer.dump({ + origins: points, + destinations: points + }) + end + def extract_costs_to_way_costs!(way_costs, points, matrix_json) way_costs = [] diff --git a/lib/tom_tom/matrix/request_json_serializer.rb b/lib/tom_tom/matrix/request_json_serializer.rb new file mode 100644 index 000000000..f4d12e482 --- /dev/null +++ b/lib/tom_tom/matrix/request_json_serializer.rb @@ -0,0 +1,25 @@ +module TomTom + class Matrix + class RequestJSONSerializer + def self.dump(hash) + hash[:origins].map! do |point| + point_to_f(point) + end + hash[:destinations].map! do |point| + point_to_f(point) + end + + JSON.dump(hash) + end + + private + + def self.point_to_f(point) + point[:point][:latitude] = point[:point][:latitude].to_f + point[:point][:longitude] = point[:point][:longitude].to_f + + point + end + end + end +end diff --git a/spec/lib/tom_tom/matrix/request_json_serializer_spec.rb b/spec/lib/tom_tom/matrix/request_json_serializer_spec.rb new file mode 100644 index 000000000..1fafad302 --- /dev/null +++ b/spec/lib/tom_tom/matrix/request_json_serializer_spec.rb @@ -0,0 +1,39 @@ +RSpec.describe TomTom::Matrix::RequestJSONSerializer do + describe ".dump" do + it "serializes BigDecimal values to floats" do + points = [{ + point: { + latitude: 52.50867.to_d, + longitude: 13.42879.to_d + }, + }] + data = { + origins: points, + destinations: points + } + + expect( + TomTom::Matrix::RequestJSONSerializer.dump(data) + ).to eq(<<-JSON.delete(" \n")) + { + "origins": [ + { + "point": { + "latitude": 52.50867, + "longitude": 13.42879 + } + } + ], + "destinations": [ + { + "point": { + "latitude": 52.50867, + "longitude": 13.42879 + } + } + ] + } + JSON + end + end +end diff --git a/spec/lib/tom_tom/matrix_spec.rb b/spec/lib/tom_tom/matrix_spec.rb index b63ebc70b..605f1d254 100644 --- a/spec/lib/tom_tom/matrix_spec.rb +++ b/spec/lib/tom_tom/matrix_spec.rb @@ -77,6 +77,78 @@ RSpec.describe TomTom::Matrix do end end + describe "#build_request_body" do + it "serializes BigDecimal coordinates to floats" do + points = [ + { + point: { + latitude: 48.85086.to_d, + longitude: 2.36143.to_d + }, + }, + { + point: { + latitude: 47.91231.to_d, + longitude: 1.87606.to_d + }, + }, + { + point: { + latitude: 52.50867.to_d, + longitude: 13.42879.to_d + }, + } + ] + + expect( + matrix.build_request_body(points) + ).to eq(<<-JSON.delete(" \n")) + { + "origins": [ + { + "point": { + "latitude": 48.85086, + "longitude": 2.36143 + } + }, + { + "point": { + "latitude": 47.91231, + "longitude": 1.87606 + } + }, + { + "point": { + "latitude": 52.50867, + "longitude": 13.42879 + } + } + ], + "destinations": [ + { + "point": { + "latitude": 48.85086, + "longitude": 2.36143 + } + }, + { + "point": { + "latitude": 47.91231, + "longitude": 1.87606 + } + }, + { + "point": { + "latitude": 52.50867, + "longitude": 13.42879 + } + } + ] + } + JSON + end + end + describe "#extract_costs_to_way_costs!" do it "puts distance & time costs in way_costs" do way_costs = [ |
