aboutsummaryrefslogtreecommitdiffstats
path: root/lib/tom_tom.rb
diff options
context:
space:
mode:
authorTeddy Wing2018-03-09 15:55:49 +0100
committerTeddy Wing2018-03-14 16:38:39 +0100
commitb27dd5fe2f061e2ef94d250fc5a06c1d8e84179a (patch)
tree900a3ddcc45526b03e5b026f39422573e02301a4 /lib/tom_tom.rb
parent23cc4317f72c3776307ee15344462e151ba1dc52 (diff)
downloadchouette-core-b27dd5fe2f061e2ef94d250fc5a06c1d8e84179a.tar.bz2
Add `TomTom` class to communicate with their API
Provides an interface to communicate with the TomTom API. Currently includes a method `#batch` to make a batch routing request (https://developer.tomtom.com/online-routing/online-routing-documentation/batch-routing). Left a bunch of development-related code in just to preserve my in-progress stages. Originally I was told to use the matrix routing API, but that turned out to not match what we wanted. Namely, matrix routing would produce a table with every point routed with every other point. We instead want routes of each segment in a line, in order (or, just the diagonal of the matrix). Using the batch API allows us to get the routes we need without doing unnecessary work. Update the `WayCost` class to provide `departure` and `arrival` methods as readers, and make `id` an optional parameter for now. (We still need to figure out how we're dealing with ID.) Refs #6095
Diffstat (limited to 'lib/tom_tom.rb')
-rw-r--r--lib/tom_tom.rb71
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/tom_tom.rb b/lib/tom_tom.rb
new file mode 100644
index 000000000..5343519fd
--- /dev/null
+++ b/lib/tom_tom.rb
@@ -0,0 +1,71 @@
+class TomTom
+ BASE_URL = 'https://api.tomtom.com'
+# https://api.tomtom.com/routing/1/matrix/xml?key=<APIKEY>&routeType=shortest&travelMode=truck
+ 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
+
+ # Maximum size of matrix is 700 (number of origins multiplied by number of destinations), so examples of matrix dimensions are: 5x10, 10x10, 28x25 (it does not need to be square).
+ # def matrix(opts)
+ # @connection.post do |req|
+ # req.url '/routing/1/matrix/json'
+ # req.params {
+ # routeType: 'shortest',
+ # travelMode: opts[:travel_mode],
+ # routingOption: 'TODO'
+ # }
+ # req.body {
+ # origins: [],
+ # destinations: []
+ # }
+ # )
+ # end
+
+# Maximum number of batch items is 700.
+# {
+# "batchItems": [
+# {"query": "/calculateRoute/52.36006039665441,4.851064682006836:52.36187528311709,4.850560426712036/json?travelMode=car&routeType=shortest&traffic=true&departAt=now&maxAlternatives=0"},
+# {"query": "/calculateRoute/52.36241907934766,4.850034713745116:52.36173769505809,4.852169752120972/json?travelMode=teleport&routeType=shortest&traffic=true&departAt=now"}
+# ]
+# }
+ def batch(way_costs)
+ # TODO: figure out param assembly (maybe Net::HTTP has something?)
+ params = URI.encode_www_form({
+ travelMode: 'car',
+ routeType: 'shortest',
+ traffic: 'true',
+ departAt: 'now',
+ maxAlternatives: 0
+ })
+ 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
+ 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