aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/stop_areas_to_way_costs_converter.rb21
-rw-r--r--lib/tom_tom.rb22
-rw-r--r--lib/tom_tom/batch.rb54
-rw-r--r--lib/way_cost.rb27
-rw-r--r--lib/way_cost_collection_json_serializer.rb16
5 files changed, 140 insertions, 0 deletions
diff --git a/lib/stop_areas_to_way_costs_converter.rb b/lib/stop_areas_to_way_costs_converter.rb
new file mode 100644
index 000000000..c6a00d31b
--- /dev/null
+++ b/lib/stop_areas_to_way_costs_converter.rb
@@ -0,0 +1,21 @@
+class StopAreasToWayCostsConverter
+ def initialize(stop_areas)
+ @stop_areas = stop_areas
+ end
+
+ def convert
+ @stop_areas.each_cons(2).map do |stop_area_pair|
+ WayCost.new(
+ departure: Geokit::LatLng.new(
+ stop_area_pair[0].latitude,
+ stop_area_pair[0].longitude
+ ),
+ arrival: Geokit::LatLng.new(
+ stop_area_pair[1].latitude,
+ stop_area_pair[1].longitude
+ ),
+ id: "#{stop_area_pair[0].id}-#{stop_area_pair[1].id}"
+ )
+ end
+ end
+end
diff --git a/lib/tom_tom.rb b/lib/tom_tom.rb
new file mode 100644
index 000000000..a1a2bda43
--- /dev/null
+++ b/lib/tom_tom.rb
@@ -0,0 +1,22 @@
+module TomTom
+ BASE_URL = 'https://api.tomtom.com'
+ @api_key = Rails.application.secrets.tomtom_api_key
+
+ @connection = Faraday.new(
+ url: BASE_URL,
+ params: {
+ key: @api_key
+ }
+ ) do |faraday|
+ faraday.use FaradayMiddleware::FollowRedirects, limit: 1
+ faraday.adapter Faraday.default_adapter
+ end
+
+ def self.enabled?
+ @api_key.present?
+ end
+
+ def self.batch(way_costs)
+ TomTom::Batch.new(@connection).batch(way_costs)
+ end
+end
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
diff --git a/lib/way_cost.rb b/lib/way_cost.rb
new file mode 100644
index 000000000..9f860308c
--- /dev/null
+++ b/lib/way_cost.rb
@@ -0,0 +1,27 @@
+class WayCost
+ attr_reader :departure, :arrival, :id
+ attr_accessor :distance, :time
+
+ def initialize(
+ departure:,
+ arrival:,
+ distance: nil,
+ time: nil,
+ id: nil
+ )
+ @departure = departure
+ @arrival = arrival
+ @distance = distance
+ @time = time
+ @id = id
+ end
+
+ def ==(other)
+ other.is_a?(self.class) &&
+ @departure == other.departure &&
+ @arrival == other.arrival &&
+ @distance == other.distance &&
+ @time == other.time &&
+ @id == other.id
+ end
+end
diff --git a/lib/way_cost_collection_json_serializer.rb b/lib/way_cost_collection_json_serializer.rb
new file mode 100644
index 000000000..191871cca
--- /dev/null
+++ b/lib/way_cost_collection_json_serializer.rb
@@ -0,0 +1,16 @@
+class WayCostCollectionJSONSerializer
+ def self.dump(way_costs)
+ return if way_costs.nil?
+
+ costs_by_id = {}
+
+ way_costs.each do |way_cost|
+ costs_by_id[way_cost.id] = {
+ distance: way_cost.distance,
+ time: way_cost.time
+ }
+ end
+
+ JSON.dump(costs_by_id)
+ end
+end