aboutsummaryrefslogtreecommitdiffstats
path: root/lib/tom_tom.rb
blob: a2ebeb1a840551a821515bbe2a3dbf34da03f3bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class TomTom
  BASE_URL = 'https://api.tomtom.com'
  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 calculate_route(way_costs)
    params = URI.encode_www_form({
      travelMode: 'bus',
      routeType: 'shortest'
    })
    locations = convert_way_costs_for_calculate_route(way_costs)

    response = @connection.post do |req|
      req.url "/routing/1/calculateRoute/#{locations}/json?#{params}"
      req.headers['Content-Type'] = 'application/json'
    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 do |route|
      next if route['statusCode'] != 200

      distance = route['response']['routes']
    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

  def convert_way_costs_for_calculate_route(way_costs)
    coordinates = []

    way_costs.map do |way_cost|
      coordinates << "#{way_cost.departure.lat},#{way_cost.departure.lng}"
      coordinates << "#{way_cost.arrival.lat},#{way_cost.arrival.lng}"
    end

    coordinates
      .uniq
      .join(':')
  end
end