diff options
| author | Alban Peignier | 2018-03-15 17:09:34 +0100 |
|---|---|---|
| committer | GitHub | 2018-03-15 17:09:34 +0100 |
| commit | f011f7e9806ffeaaba3ad73510bc818211f55dbd (patch) | |
| tree | b59a6a71f82b7121fb3a0ffa3a65b9d7cedaa3aa /lib/tom_tom/batch.rb | |
| parent | d3d3c0f40c37d716cfccbf9297bfbdc98c692521 (diff) | |
| parent | 9291d45e825edbaf52cb556c102498366985496f (diff) | |
| download | chouette-core-f011f7e9806ffeaaba3ad73510bc818211f55dbd.tar.bz2 | |
Merge pull request #379 from af83/6095-route--calculate-distance-and-time-cost-between-stops
Calculate distance and time cost between Route stops. Refs #6095
Diffstat (limited to 'lib/tom_tom/batch.rb')
| -rw-r--r-- | lib/tom_tom/batch.rb | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/tom_tom/batch.rb b/lib/tom_tom/batch.rb new file mode 100644 index 000000000..6ceb9c226 --- /dev/null +++ b/lib/tom_tom/batch.rb @@ -0,0 +1,54 @@ +module TomTom + class Batch + def initialize(connection) + @connection = connection + end + + def batch(way_costs) + params = URI.encode_www_form({ + travelMode: 'bus', + routeType: 'shortest' + }) + batch_items = convert_way_costs(way_costs).map do |locations| + { + query: "/calculateRoute/#{locations}/json?#{params}" + } + end + + response = @connection.post do |req| + req.url '/routing/1/batch/json' + req.headers['Content-Type'] = 'application/json' + req.body = { + batchItems: batch_items + }.to_json + end + + extract_costs_to_way_costs!( + way_costs, + JSON.parse(response.body) + ) + end + + def extract_costs_to_way_costs!(way_costs, batch_json) + calculated_routes = batch_json['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(way_costs) + way_costs.map do |way_cost| + "#{way_cost.departure.lat},#{way_cost.departure.lng}" \ + ":#{way_cost.arrival.lat},#{way_cost.arrival.lng}" + end + end + end +end |
