diff options
| author | Teddy Wing | 2018-03-12 12:41:28 +0100 |
|---|---|---|
| committer | Teddy Wing | 2018-03-14 16:38:39 +0100 |
| commit | f04a89b3af581c8956add61e4a05edc0ae9405f5 (patch) | |
| tree | 390b4b6baaf181b8a461e3fd71b91be128bea909 /lib | |
| parent | 0d5e6f92f74a06807c67c9d0427dd5b5543a6400 (diff) | |
| download | chouette-core-f04a89b3af581c8956add61e4a05edc0ae9405f5.tar.bz2 | |
Move `lib/tom_tom.rb` to `lib/tom_tom/batch.rb`
Separate the functionality a little better by moving the `/batch`
endpoint code into a new class. The goal here is also to lay the
foundation for being able to call `TomTom.batch()` instead of
`TomTom.new.batch()`.
Refs #6095
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/tom_tom.rb | 58 | ||||
| -rw-r--r-- | lib/tom_tom/batch.rb | 58 |
2 files changed, 59 insertions, 57 deletions
diff --git a/lib/tom_tom.rb b/lib/tom_tom.rb index 415bba54b..2947961c3 100644 --- a/lib/tom_tom.rb +++ b/lib/tom_tom.rb @@ -1,58 +1,2 @@ -class TomTom - BASE_URL = 'https://api.tomtom.com' - API_KEY = Rails.application.secrets.tomtom_api_key - - def initialize - @connection = Faraday.new( - url: BASE_URL, - params: { - key: API_KEY - } - ) do |faraday| - faraday.use FaradayMiddleware::FollowRedirects, limit: 1 - faraday.adapter Faraday.default_adapter - end - end - - def batch(way_costs) - params = URI.encode_www_form({ - travelMode: 'bus', - routeType: 'shortest' - }) - batch_items = convert_way_costs_for_batch(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 - - 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) - way_costs.map do |way_cost| - "#{way_cost.departure.lat},#{way_cost.departure.lng}" \ - ":#{way_cost.arrival.lat},#{way_cost.arrival.lng}" - end - end +module TomTom end diff --git a/lib/tom_tom/batch.rb b/lib/tom_tom/batch.rb new file mode 100644 index 000000000..e20989c84 --- /dev/null +++ b/lib/tom_tom/batch.rb @@ -0,0 +1,58 @@ +class TomTom::Batch + BASE_URL = 'https://api.tomtom.com' + API_KEY = Rails.application.secrets.tomtom_api_key + + def initialize + @connection = Faraday.new( + url: BASE_URL, + params: { + key: API_KEY + } + ) do |faraday| + faraday.use FaradayMiddleware::FollowRedirects, limit: 1 + faraday.adapter Faraday.default_adapter + end + end + + def batch(way_costs) + params = URI.encode_www_form({ + travelMode: 'bus', + routeType: 'shortest' + }) + batch_items = convert_way_costs_for_batch(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 + + 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) + 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 |
