diff options
| -rw-r--r-- | lib/tom_tom.rb | 71 | ||||
| -rw-r--r-- | lib/way_cost.rb | 4 | ||||
| -rw-r--r-- | spec/lib/tom_tom_spec.rb | 25 |
3 files changed, 99 insertions, 1 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 diff --git a/lib/way_cost.rb b/lib/way_cost.rb index b47836a28..32ced0205 100644 --- a/lib/way_cost.rb +++ b/lib/way_cost.rb @@ -1,10 +1,12 @@ class WayCost + attr_reader :departure, :arrival + def initialize( departure:, arrival:, distance: nil, time: nil, - id:, # TODO: calculate ID automatically + id: nil # TODO: calculate ID automatically ) @departure = departure @arrival = arrival diff --git a/spec/lib/tom_tom_spec.rb b/spec/lib/tom_tom_spec.rb new file mode 100644 index 000000000..57f22d26a --- /dev/null +++ b/spec/lib/tom_tom_spec.rb @@ -0,0 +1,25 @@ +RSpec.describe TomTom do + let(:tomtom) { TomTom.new } + + describe "#convert_way_costs_for_batch" do + it "turns WayCost points into a collection of colon-separated strings" do + way_costs = [ + WayCost.new( + departure: Geokit::LatLng.new(2, 48), + arrival: Geokit::LatLng.new(3, 46) + ), + WayCost.new( + departure: Geokit::LatLng.new(-71, 42), + arrival: Geokit::LatLng.new(-71.5, 42.9) + ) + ] + + expect( + tomtom.convert_way_costs_for_batch(way_costs) + ).to eq([ + '2,48:3,46', + '-71,42:-71.5,42.9' + ]) + end + end +end |
