aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stif/codif_line_synchronization.rb
diff options
context:
space:
mode:
authorEdouard Maffert2016-07-28 15:05:03 +0200
committerEdouard Maffert2016-07-28 15:05:36 +0200
commit6dc3c7a58d5c0fc0beb8a428616dce52e2361305 (patch)
treee004686be3d1db486fbcba1f1eddccc32761a097 /lib/stif/codif_line_synchronization.rb
parent3911e0fa90db021a3a2db78e2c3f76003fdde51b (diff)
downloadchouette-core-6dc3c7a58d5c0fc0beb8a428616dce52e2361305.tar.bz2
add codif_line_synchronization #907
Diffstat (limited to 'lib/stif/codif_line_synchronization.rb')
-rw-r--r--lib/stif/codif_line_synchronization.rb81
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/stif/codif_line_synchronization.rb b/lib/stif/codif_line_synchronization.rb
new file mode 100644
index 000000000..0c4c6e91e
--- /dev/null
+++ b/lib/stif/codif_line_synchronization.rb
@@ -0,0 +1,81 @@
+module Stif
+ module CodifLineSynchronization
+ class << self
+ # Don't check last synchronizations date and lines last update if first_sync
+ def synchronize first_sync = false
+ # Check last synchronization and synchronization interval
+ date = DateTime.now.to_date - LineReferential.first.sync_interval.days
+ last_sync = LineReferential.first.line_referential_sync.line_sync_operations.last.try(:created_at)
+ return if (last_sync.nil? || last_sync.to_date > date) && !first_sync
+
+ # TODO Check exceptions and status messages
+ begin
+ # Fetch Codifline operators and lines
+ client = Codifligne::API.new
+ operators = client.operators
+ lines = client.lines
+
+ operators.map{ |o| create_or_update_company(o) }
+ lines.map{ |l| create_or_update_line(l) if first_sync || l.instance_variable_get(:@updated_at).to_date > last_sync.to_date}
+
+ delete_deprecated_companies(operators)
+ deactivate_deprecated_lines(lines)
+
+ LineReferential.first.line_referential_sync.record_status "OK"
+ rescue Exception => e
+ ap e.message
+ LineReferential.first.line_referential_sync.record_status "Error"
+ end
+ end
+
+ def create_or_update_company(api_operator)
+ params = {
+ name: api_operator.name,
+ objectid: api_operator.stif_id
+ }
+ company = save_or_update(params, Chouette::Company)
+ end
+
+ def create_or_update_line(api_line)
+ params = {
+ name: api_line.name,
+ objectid: api_line.stif_id,
+ number: api_line.short_name,
+ deactivated: false
+ }
+
+ # Find Company
+ # TODO Check behavior when operator_codes count is 0 or > 1
+ if api_line.operator_codes.any?
+ company_id = "STIF:CODIFLIGNE:Operator:" + api_line.operator_codes.first
+ params[:company] = Chouette::Company.find_by(objectid: company_id)
+ end
+
+ save_or_update(params, Chouette::Line)
+ end
+
+ def delete_deprecated_companies(operators)
+ ids = operators.map{ |o| o.stif_id }.to_a
+ Chouette::Company.where.not(objectid: ids).destroy_all
+ end
+
+ def deactivate_deprecated_lines(lines)
+ ids = lines.map{ |l| l.stif_id }.to_a
+ Chouette::Line.where.not(objectid: ids).map { |l| l.deactivated = true ; l.save }
+ end
+
+ def save_or_update(params, klass)
+ params[:line_referential] = LineReferential.first
+ object = klass.where(objectid: params[:objectid]).first
+ if object
+ object.assign_attributes(params)
+ object.save if object.changed?
+ else
+ object = klass.new(params)
+ object.save if object.valid?
+ end
+ object
+ end
+ end
+ end
+end \ No newline at end of file