From ab0cb3af230e3b6e8c480c34581f9779036a0673 Mon Sep 17 00:00:00 2001 From: Xinhui Date: Thu, 6 Oct 2016 10:17:02 +0200 Subject: Sax parsing stop_place quays --- Gemfile | 2 +- Gemfile.lock | 9 ++-- app/models/chouette/stop_area.rb | 2 - lib/stif/reflex_synchronization.rb | 87 ++++++++++++++++++---------------- spec/models/chouette/stop_area_spec.rb | 1 - spec/tasks/reflex_rake_spec.rb | 3 +- 6 files changed, 52 insertions(+), 52 deletions(-) diff --git a/Gemfile b/Gemfile index 67e7f6f0d..ea010d51d 100644 --- a/Gemfile +++ b/Gemfile @@ -51,7 +51,7 @@ gem 'polylines' # Codifligne API gem 'codifligne', git: 'git@github.com:AF83/stif-codifline-api.git' # Reflex API -gem 'reflex', git: 'git@github.com:AF83/stif-reflex-api.git' +gem 'reflex', git: 'git@github.com:AF83/stif-reflex-api.git', branch: 'sax_refactoring' # Authentication gem 'devise', '~> 3.4.0' diff --git a/Gemfile.lock b/Gemfile.lock index e2b7d073f..a9a74eabe 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -14,7 +14,8 @@ GIT GIT remote: git@github.com:AF83/stif-reflex-api.git - revision: aff037d3d5d48f28d3c844dc5bbf2970ba2e4458 + revision: b4c983e94f3177fb05d28ab84299c0e5e5b1defb + branch: sax_refactoring specs: reflex (0.0.1) nokogiri (~> 1.6) @@ -289,10 +290,9 @@ GEM net-ssh-gateway (1.2.0) net-ssh (>= 2.6.5) newrelic_rpm (3.11.2.286) - nokogiri (1.6.8) + nokogiri (1.6.8.1) mini_portile2 (~> 2.1.0) - pkg-config (~> 1.1.7) - nokogiri (1.6.8-java) + nokogiri (1.6.8.1-java) notiffany (0.0.6) nenv (~> 0.1) shellany (~> 0.0) @@ -301,7 +301,6 @@ GEM parser (2.2.0.3) ast (>= 1.1, < 3.0) pg (0.18.1) - pkg-config (1.1.7) poltergeist (1.6.0) capybara (~> 2.1) cliver (~> 0.3.1) diff --git a/app/models/chouette/stop_area.rb b/app/models/chouette/stop_area.rb index 398f37996..815304cca 100644 --- a/app/models/chouette/stop_area.rb +++ b/app/models/chouette/stop_area.rb @@ -34,8 +34,6 @@ class Chouette::StopArea < Chouette::ActiveRecord validates_format_of :registration_number, :with => %r{\A[\d\w_\-]+\Z}, :allow_blank => true validates_presence_of :name - validates_presence_of :area_type - validates_presence_of :latitude, :if => :longitude validates_presence_of :longitude, :if => :latitude validates_numericality_of :latitude, :less_than_or_equal_to => 90, :greater_than_or_equal_to => -90, :allow_nil => true diff --git a/lib/stif/reflex_synchronization.rb b/lib/stif/reflex_synchronization.rb index 956f94696..471b226c2 100644 --- a/lib/stif/reflex_synchronization.rb +++ b/lib/stif/reflex_synchronization.rb @@ -14,7 +14,6 @@ module Stif client = Reflex::API.new processed = [] initial_count = Chouette::StopArea.where(deleted_at: nil).count - existing_records = Hash[Chouette::StopArea.pluck(:import_xml, :objectid).map{|xml, id| [id, xml]}] ['getOR', 'getOP'].each do |method| start = Time.now @@ -25,19 +24,18 @@ module Stif end # Create or update stop_area for every quay, stop_place - stop_areas = results[:Quay].merge(results[:StopPlace]) - start = Time.now - stop_areas.each do |id, entry| - processed << entry.id - next unless need_update_or_create?(existing_records, entry) + stop_areas = results[:Quay] | results[:StopPlace] + + start = Time.now + stop_areas.each do |entry| + processed << entry['id'] self.create_or_update_stop_area entry end Rails.logger.info "Reflex:sync - Create or update StopArea done in #{Time.now - start} seconds" # Walk through every entry and set parent stop_area start = Time.now - stop_areas.each do |id, entry| - next unless need_update_or_create?(existing_records, entry) + stop_areas.each do |entry| self.stop_area_set_parent entry end Rails.logger.info "Reflex:sync - StopArea set parent done in #{Time.now - start} seconds" @@ -48,10 +46,6 @@ module Stif } end - def need_update_or_create? existing_records, entry - return true unless existing_records.key?(entry.id) && entry.xml == existing_records[entry.id] - end - def set_deleted_stop_area processed start = Time.now deleted = Chouette::StopArea.where(deleted_at: nil).pluck(:objectid).uniq - processed @@ -63,18 +57,18 @@ module Stif 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 + return false unless entry['parent'] || entry['quays'] + stop = self.find_by_object_id entry['id'] return false unless stop - if entry.try(:parent_site_ref) - stop.parent = self.find_by_object_id entry.parent_site_ref + if entry['parent'] + stop.parent = self.find_by_object_id entry['parent'] stop.save! if stop.changed end - if entry.try(:quays) - entry.quays.each do |quay| - children = self.find_by_object_id(quay[:ref]) + if entry['quays'] + entry['quays'].each do |id| + children = self.find_by_object_id id next unless children children.parent = stop children.save! if children.changed? @@ -82,39 +76,50 @@ module Stif end end + def access_point_access_type entry + if entry['IsEntry'] == 'true' && entry['IsExit'] == 'true' + 'in_out' + elsif entry['IsEntry'] == 'true' + 'in' + elsif entry['IsExit'] == 'true' + 'out' + end + end + def create_or_update_access_point entry, stop_area - access = Chouette::AccessPoint.find_or_create_by(objectid: "dummy:AccessPoint:#{entry.id.tr(':', '')}") + access = Chouette::AccessPoint.find_or_create_by(objectid: "dummy:AccessPoint:#{entry['id'].tr(':', '')}") # Hack, on save object_version will be incremented by 1 - entry.version = entry.version.to_i + 1 if access.persisted? + entry['version'] = entry['version'].to_i + 1 if access.persisted? + access.access_type = self.access_point_access_type(entry) access.stop_area = stop_area { - :name => :name, - :access_type => :access_type, - :object_version => :version, - :zip_code => :postal_code, - :city_name => :city, - :import_xml => :xml - }.each do |k, v| access[k] = entry.try(v) end - access.save if access.changed? + :name => 'Name', + :object_version => 'version', + :zip_code => 'PostalRegion', + :city_name => 'Town' + }.each do |k, v| access[k] = entry[v] end + access.save! if access.changed? end def create_or_update_stop_area entry - stop = Chouette::StopArea.find_or_create_by(objectid: entry.id) + stop = Chouette::StopArea.find_or_create_by(objectid: entry['id']) stop.deleted_at = nil stop.stop_area_referential = self.defaut_referential { - :name => :name, - :creation_time => :created, - :area_type => :area_type, - :object_version => :version, - :zip_code => :postal_code, - :city_name => :city, - :import_xml => :xml - }.each do |k, v| stop[k] = entry.try(v) end - stop.save! if stop.changed? + :name => 'Name', + :creation_time => 'created', + :area_type => 'type', + :object_version => 'version', + :zip_code => 'PostalRegion', + :city_name => 'Town' + }.each do |k, v| stop[k] = entry[v] end + if stop.changed? + stop.import_xml = entry[:xml] + stop.save! + end # Create AccessPoint from StopPlaceEntrance - if entry.try(:entrances) - entry.entrances.each do |entrance| + if entry[:stop_place_entrances] + entry[:stop_place_entrances].each do |entrance| self.create_or_update_access_point entrance, stop end end diff --git a/spec/models/chouette/stop_area_spec.rb b/spec/models/chouette/stop_area_spec.rb index 53d4fcee4..cdddb407d 100644 --- a/spec/models/chouette/stop_area_spec.rb +++ b/spec/models/chouette/stop_area_spec.rb @@ -15,7 +15,6 @@ describe Chouette::StopArea, :type => :model do it { is_expected.to belong_to(:stop_area_referential) } it { is_expected.to validate_presence_of :name } - it { is_expected.to validate_presence_of :area_type } it { is_expected.to validate_numericality_of :latitude } it { is_expected.to validate_numericality_of :longitude } diff --git a/spec/tasks/reflex_rake_spec.rb b/spec/tasks/reflex_rake_spec.rb index 46910de72..bf2440e50 100644 --- a/spec/tasks/reflex_rake_spec.rb +++ b/spec/tasks/reflex_rake_spec.rb @@ -30,9 +30,8 @@ describe 'reflex:sync' do it 'should map xml data to StopArea attribute' do stop_area = Chouette::StopArea.find_by(objectid: 'FR:77153:LDA:69325:STIF') - expect(stop_area.city_name).to eq 'Dammartin-en-Goƫle' expect(stop_area.zip_code).to eq '77153' - expect(stop_area.area_type).to eq 'StopPlace' + expect(stop_area.area_type).to eq 'LDA' end context 'On next sync' do -- cgit v1.2.3