aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorTeddy Wing2018-03-15 17:09:49 +0100
committerLuc Donnet2018-03-23 17:16:43 +0100
commit02b2947632fbafc7bb637a21cc6ea089094fe31c (patch)
tree909c5cb1d6f72ff2dab6fc91b132224623822660 /lib
parent0d90fbf26eb588fffb13a6b51c5b3a5b8a173218 (diff)
downloadchouette-core-02b2947632fbafc7bb637a21cc6ea089094fe31c.tar.bz2
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
Diffstat (limited to 'lib')
-rw-r--r--lib/route_way_cost_json_serializer.rb22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/route_way_cost_json_serializer.rb b/lib/route_way_cost_json_serializer.rb
new file mode 100644
index 000000000..20b3d6ee2
--- /dev/null
+++ b/lib/route_way_cost_json_serializer.rb
@@ -0,0 +1,22 @@
+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