diff options
| author | Teddy Wing | 2018-03-12 12:28:19 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2018-03-14 16:38:39 +0100 | 
| commit | 0d5e6f92f74a06807c67c9d0427dd5b5543a6400 (patch) | |
| tree | a0496938aed2517a9f113eb53fb2769fb24d9266 | |
| parent | ac2f38518f6c0aa81fcc622ce5f0ab3b3837dbe0 (diff) | |
| download | chouette-core-0d5e6f92f74a06807c67c9d0427dd5b5543a6400.tar.bz2 | |
TomTom#batch: Populate WayCosts with distance & time from API
Look through the API response from `/batch` and extract the distance &
time values given to us by TomTom. These are inserted in the `WayCost`s
given to use in the argument to `#batch`.
At the end, we get a list of `WayCost`s that are completely filled with
distance & time values.
We need to split up this code a bit to make it more testable, but
already this seems to function correctly.
Refs #6095
| -rw-r--r-- | lib/tom_tom.rb | 15 | ||||
| -rw-r--r-- | lib/way_cost.rb | 1 | 
2 files changed, 16 insertions, 0 deletions
| diff --git a/lib/tom_tom.rb b/lib/tom_tom.rb index 58604c347..415bba54b 100644 --- a/lib/tom_tom.rb +++ b/lib/tom_tom.rb @@ -32,6 +32,21 @@ class TomTom          batchItems: batch_items        }.to_json      end + +    response = JSON.parse(response.body) + +    calculated_routes = response['batchItems'] +    calculated_routes.each_with_index do |route, i| +      next if route['statusCode'] != 200 + +      distance = route['response']['routes'][0]['summary']['lengthInMeters'] +      time = route['response']['routes'][0]['summary']['travelTimeInSeconds'] + +      way_costs[i].distance = distance +      way_costs[i].time = time +    end + +    way_costs    end    def convert_way_costs_for_batch(way_costs) diff --git a/lib/way_cost.rb b/lib/way_cost.rb index 32ced0205..027d8f593 100644 --- a/lib/way_cost.rb +++ b/lib/way_cost.rb @@ -1,5 +1,6 @@  class WayCost    attr_reader :departure, :arrival +  attr_writer :distance, :time    def initialize(      departure:, | 
