From c17665c0cc064c8a14af812dedd645977d110388 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 27 Mar 2018 15:24:27 +0200 Subject: TomTom::Matrix: Serialize `BigDecimal` as float Rails serialises `BigDecimal`s as JSON strings to prevent loss of precision. The `latitude` and `longitude` columns in `StopArea` are stored as `BigDecimal`s. The trouble is that TomTom's API requires the latitude & longitude values to be JSON floats, not strings. Make a new JSON serialiser that converts the `BigDecimal` coordinates to float to allow the values to be correctly interpreted by the API. Refs #6222 --- lib/tom_tom/matrix.rb | 12 ++++++++---- lib/tom_tom/matrix/request_json_serializer.rb | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 lib/tom_tom/matrix/request_json_serializer.rb (limited to 'lib') 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 -- cgit v1.2.3