aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/route_way_cost_unit_converter.rb25
-rw-r--r--lib/stif/reflex_synchronization.rb21
-rw-r--r--lib/stop_areas_to_way_costs_converter.rb21
-rw-r--r--lib/tasks/erd.rake6
-rw-r--r--lib/tasks/imports.rake6
-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
9 files changed, 182 insertions, 16 deletions
diff --git a/lib/route_way_cost_unit_converter.rb b/lib/route_way_cost_unit_converter.rb
new file mode 100644
index 000000000..45edbf538
--- /dev/null
+++ b/lib/route_way_cost_unit_converter.rb
@@ -0,0 +1,25 @@
+class RouteWayCostUnitConverter
+ def self.convert(way_costs)
+ return if way_costs.nil?
+
+ way_costs.each do |_, costs|
+ costs['distance'] = self.meters_to_kilometers(costs['distance'])
+ costs['time'] = self.seconds_to_minutes(costs['time'])
+ end
+ end
+
+ private
+
+ # Round to 2 decimal places to appease JavaScript validation
+ def self.meters_to_kilometers(num)
+ return 0 unless num
+
+ (num / 1000.0).to_i
+ end
+
+ def self.seconds_to_minutes(num)
+ return 0 unless num
+
+ num / 60
+ end
+end
diff --git a/lib/stif/reflex_synchronization.rb b/lib/stif/reflex_synchronization.rb
index 7570e4c49..508d28b40 100644
--- a/lib/stif/reflex_synchronization.rb
+++ b/lib/stif/reflex_synchronization.rb
@@ -151,24 +151,25 @@ module Stif
def create_or_update_stop_area entry
stop = Chouette::StopArea.find_or_create_by(objectid: entry['id'], stop_area_referential: self.defaut_referential )
- stop.kind = :commercial
- stop.deleted_at = nil
{
- :comment => 'Description',
- :name => 'Name',
- :area_type => 'TypeOfPlaceRef',
- :object_version => 'version',
- :zip_code => 'PostalRegion',
- :city_name => 'Town',
- :stif_type => 'OBJECT_STATUS'
+ comment: 'Description',
+ name: 'Name',
+ area_type: 'TypeOfPlaceRef',
+ object_version: 'version',
+ zip_code: 'PostalRegion',
+ city_name: 'Town',
+ stif_type: 'OBJECT_STATUS',
}.each do |k, v| stop[k] = entry[v] end
- # TODO: use stop.update_attributes instead of the above
if entry['gml:pos']
stop['longitude'] = entry['gml:pos'][:lng]
stop['latitude'] = entry['gml:pos'][:lat]
end
+ stop.kind = :commercial
+ stop.deleted_at = nil
+ stop.confirmed_at = Time.now if stop.new_record?
+
if stop.changed?
stop.created_at = entry[:created]
stop.import_xml = entry[:xml]
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/tasks/erd.rake b/lib/tasks/erd.rake
index 96bb7fe37..b5790a9ab 100644
--- a/lib/tasks/erd.rake
+++ b/lib/tasks/erd.rake
@@ -5,11 +5,11 @@ namespace :generate do
sh "bundle exec rake erd only='Organisation,Referential,User,Workbench,Workgroup' filename='organisation' title='Organisation'"
sh "bundle exec rake erd only='Calendar,Referential,ReferentialMetadata,Chouette::Line,Chouette::Route,Chouette::JourneyPattern,Chouette::VehicleJourney,Chouette::VehicleJourneyAtStop,Chouette::TimeTable,Chouette::TimeTableDate,Chouette::TimeTablePeriod,Chouette::Footnote,Chouette::Network,Chouette::Company,Chouette::StopPoint,Chouette::StopArea' filename='offer_datas' title='Offer Datas'"
sh "bundle exec rake erd only='Organisation,StopAreaReferential,StopAreaReferentialSync,StopAreaReferentialSyncMessage,StopAreaReferentialMembership,LineReferential,LineReferentialSync,LineReferentialSyncMessage,LineReferentialMembership' filename='referentiels_externes' title='Référentiels externes'"
- sh "bundle exec rake erd only='NetexImport,Import,WorkbenchImport,ImportResource,ImportMessage' filename='import' title='Import'"
+ sh "bundle exec rake erd only='Import::Netex,Import::Base,Import::Workbench,Import::Resource,Import::Message' filename='import' title='Import'"
sh "bundle exec rake erd only='ComplianceControlSet,ComplianceControlBlock,ComplianceControl,ComplianceCheckSet,ComplianceCheckBlock,ComplianceCheck,ComplianceCheckResource,ComplianceCheckMessage' filename='validation' title='Validation'"
sh "bundle exec rake erd only='Organisation,Workgroup,Workbench,ReferentialSuite,Referential' filename='merge' title='Merge'"
- #sh "bundle exec rake erd only='VehicleJourney,VehicleJourneyExport' filename='export' title='Export'"
- #sh "bundle exec rake erd only='' filename='integration' title='Integration'"
+ sh "bundle exec rake erd only='Export::Base,Export::Message,Export::Resource,Export::Workgroup' filename='export' title='Export'"
+ sh "bundle exec rake erd only='Workbench,Referential,ReferentialSuite,Merge' filename='merge' title='Merge'"
#sh "bundle exec rake erd only='' filename='publication' title='Publication'"
end
diff --git a/lib/tasks/imports.rake b/lib/tasks/imports.rake
index 7162f2ada..d393ab156 100644
--- a/lib/tasks/imports.rake
+++ b/lib/tasks/imports.rake
@@ -3,13 +3,13 @@ require 'tasks/helpers/simple_interfaces'
namespace :import do
desc "Notify parent imports when children finish"
- task notify_parent: :environment do
- ParentNotifier.new(Import).notify_when_finished
+ task notify_parent: :environment do
+ ParentNotifier.new(Import::Base).notify_when_finished
end
desc "Mark old unfinished Netex imports as 'aborted'"
task netex_abort_old: :environment do
- NetexImport.abort_old
+ Import::Netex.abort_old
end
desc "import the given file with the corresponding importer"
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