aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stif/reflex_synchronization.rb
blob: fd2f497e26eec7d054f149a6ba08eda1b447b61c (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
80
module Stif
  module ReflexSynchronization
    class << self
      def defaut_referential
        StopAreaReferential.find_by(name: "Reflex")
      end

      # Todo remove dummy objectid
      def find_by_object_id objectid
        Chouette::StopArea.find_by(objectid: "dummy:StopArea:#{objectid.tr(':', '')}")
      end

      def synchronize
        client = Reflex::API.new
        ['getOR', 'getOP'].each do |method|
          results    = client.process method
          stop_areas = results[:Quay].merge(results[:StopPlace])

          results[:StopPlaceEntrance].each do |id, entry|
            self.create_or_update_access_point entry
          end
          Rails.logger.debug "Reflex:sync - StopPlaceEntrance sync done !"
          stop_areas.each do |id, entry|
            self.create_or_update_stop_area entry
          end
          Rails.logger.debug "Reflex:sync - StopAreas sync done !"
          stop_areas.each do |id, entry|
            self.stop_area_set_parent entry
          end
          Rails.logger.debug "Reflex:sync - StopAreas : set  parents sync done !"
        end
      end

      def stop_area_set_parent entry
        return false unless entry.try(:parent_site_ref) || entry.try(:quays)
        stop = self.find_by_object_id entry.id

        if entry.try(:parent_site_ref)
          stop.parent = self.find_by_object_id entry.parent_site_ref
          stop.save if stop.changed
        end

        if entry.try(:quays)
          entry.quays.each do |quay|
            children = self.find_by_object_id(quay[:ref])
            next unless children
            children.parent = stop
            children.save if children.changed?
          end
        end
      end

      def create_or_update_access_point entry
        access = Chouette::AccessPoint.find_or_create_by(objectid: "dummy:AccessPoint:#{entry.id.tr(':', '')}")
        access.name           = entry.name
        # access.object_version = entry.version
        access.zip_code       = entry.postal_code
        access.city_name      = entry.city
        access.access_type    = entry.area_type
        access.save if access.changed?
      end

      def create_or_update_stop_area entry
        stop = Chouette::StopArea.find_or_create_by(objectid: "dummy:StopArea:#{entry.id.tr(':', '')}")
        stop.stop_area_referential = self.defaut_referential

        stop.name          = entry.name
        stop.creation_time = entry.created
        stop.area_type     = entry.area_type
        # Todo fixe object_version auto incremented
        # by DefaultAttributesSupport prepare_auto_columns
        # stop.object_version = entry.version
        stop.zip_code  = entry.postal_code
        stop.city_name = entry.city
        stop.save if stop.changed?
        stop
      end
    end
  end
end