From c113edee0c5575d4078ccb8f0d349992860535d7 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 15 Mar 2018 17:18:09 +0100 Subject: Rename `RouteWayCostJSONSerializer` to `RouteWayCostUnitConverter` Because we need to pass a Ruby hash to Rabl instead of a JSON string, get rid of our serialiser and instead turn it into a function that just converts the distance & time units. Fix a bug in the test that had the `'1-2'` key as a symbol instead of a string which was caused by a copy-paste from JSON and not being thorough enough in search-and-replace. Refs #6203 --- app/views/routes/costs.rabl | 2 +- lib/route_way_cost_json_serializer.rb | 22 ------------------ lib/route_way_cost_unit_converter.rb | 20 ++++++++++++++++ spec/lib/route_way_cost_json_serializer_spec.rb | 31 ------------------------- spec/lib/route_way_cost_unit_converter_spec.rb | 29 +++++++++++++++++++++++ 5 files changed, 50 insertions(+), 54 deletions(-) delete mode 100644 lib/route_way_cost_json_serializer.rb create mode 100644 lib/route_way_cost_unit_converter.rb delete mode 100644 spec/lib/route_way_cost_json_serializer_spec.rb create mode 100644 spec/lib/route_way_cost_unit_converter_spec.rb diff --git a/app/views/routes/costs.rabl b/app/views/routes/costs.rabl index 2c336fb51..1a1b14898 100644 --- a/app/views/routes/costs.rabl +++ b/app/views/routes/costs.rabl @@ -1,4 +1,4 @@ object @route node :costs do - RouteWayCostJSONSerializer.dump(@route.costs) + RouteWayCostUnitConverter.convert(@route.costs) end diff --git a/lib/route_way_cost_json_serializer.rb b/lib/route_way_cost_json_serializer.rb deleted file mode 100644 index 20b3d6ee2..000000000 --- a/lib/route_way_cost_json_serializer.rb +++ /dev/null @@ -1,22 +0,0 @@ -class RouteWayCostJSONSerializer - def self.dump(way_costs) - return if way_costs.nil? - - way_costs.each do |_, costs| - costs['distance'] = self.meters_to_kilometers(costs['distance']) - costs['time'] = self.seconds_to_minutes(costs['time']) - end - - JSON.dump(way_costs) - end - - private - - def self.meters_to_kilometers(num) - num / 1000.0 - end - - def self.seconds_to_minutes(num) - num / 60 - end -end diff --git a/lib/route_way_cost_unit_converter.rb b/lib/route_way_cost_unit_converter.rb new file mode 100644 index 000000000..1aebbbb14 --- /dev/null +++ b/lib/route_way_cost_unit_converter.rb @@ -0,0 +1,20 @@ +class RouteWayCostUnitConverter + def self.convert(way_costs) + return if way_costs.nil? + + way_costs.each do |_, costs| + costs['distance'] = self.meters_to_kilometers(costs['distance']) + costs['time'] = self.seconds_to_minutes(costs['time']) + end + end + + private + + def self.meters_to_kilometers(num) + num / 1000.0 + end + + def self.seconds_to_minutes(num) + num / 60 + end +end diff --git a/spec/lib/route_way_cost_json_serializer_spec.rb b/spec/lib/route_way_cost_json_serializer_spec.rb deleted file mode 100644 index 52cb21afc..000000000 --- a/spec/lib/route_way_cost_json_serializer_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -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 diff --git a/spec/lib/route_way_cost_unit_converter_spec.rb b/spec/lib/route_way_cost_unit_converter_spec.rb new file mode 100644 index 000000000..33fb1d9aa --- /dev/null +++ b/spec/lib/route_way_cost_unit_converter_spec.rb @@ -0,0 +1,29 @@ +RSpec.describe RouteWayCostUnitConverter do + describe ".convert" 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( + RouteWayCostUnitConverter.convert(costs) + ).to eq({ + '1-2' => { + 'distance' => 35.223, + 'time' => 93 + }, + '94435-97513' => { + 'distance' => 35.919, + 'time' => 102 + } + }) + end + end +end -- cgit v1.2.3