From 7fb536325c19e837d10707e9adfc0d8bce5a3976 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 15 Mar 2018 17:09:49 +0100 Subject: Add `RouteWayCostJSONSerializer` This serialiser will take `Route#costs` and convert the distance and time fields from meters to kilometres and seconds to minutes respectively. We need this because the frontend uses kilometre and minute units while the TomTom API gives us the others (and we store the data we get from TomTom without treatment). Unfortunately, due to the way that Rabl works, this doesn't quite work just yet. The serializer returns a string, and Rabl just puts this string into the JSON output instead of a real JSON hash. Looks like I'm going to have to convert my serializer into a generic converter. Refs #6203 --- spec/lib/route_way_cost_json_serializer_spec.rb | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 spec/lib/route_way_cost_json_serializer_spec.rb (limited to 'spec') diff --git a/spec/lib/route_way_cost_json_serializer_spec.rb b/spec/lib/route_way_cost_json_serializer_spec.rb new file mode 100644 index 000000000..52cb21afc --- /dev/null +++ b/spec/lib/route_way_cost_json_serializer_spec.rb @@ -0,0 +1,31 @@ +RSpec.describe RouteWayCostJSONSerializer do + describe ".dump" do + it "converts distance from meters to km and time from seconds to minutes" do + costs = { + '1-2': { + 'distance' => 35223, + 'time' => 5604 + }, + '94435-97513' => { + 'distance' => 35919, + 'time' => 6174 + } + } + + expect( + RouteWayCostJSONSerializer.dump(costs) + ).to eq(<<-JSON.delete(" \n")) + { + "1-2": { + "distance": 35.223, + "time": 93 + }, + "94435-97513": { + "distance": 35.919, + "time": 102 + } + } + JSON + end + end +end -- cgit v1.2.3