aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlban Peignier2017-08-27 23:50:35 +0200
committerAlban Peignier2017-08-27 23:50:35 +0200
commitd79f84398849e9c32fdf41582d299dd914fb8452 (patch)
treee03fee9d967d8d4435784c9650806c8cfa47ca78
parent4a9923079ed9d1658f7c6321ceab1d7e5c086037 (diff)
downloadchouette-core-d79f84398849e9c32fdf41582d299dd914fb8452.tar.bz2
Create STIF::NetexFile to read zip content and create ReferentialMetadata from real data. Refs #4273
-rw-r--r--app/controllers/api/v1/netex_imports_controller.rb22
-rw-r--r--lib/referential_metadata_kludge.rb22
-rw-r--r--lib/stif/netex_file.rb72
-rw-r--r--spec/fixtures/single_reference_import.zipbin220 -> 5446 bytes
-rw-r--r--spec/lib/stif/netex_file_spec.rb27
-rw-r--r--spec/requests/api/v1/netex_import_spec.rb14
6 files changed, 123 insertions, 34 deletions
diff --git a/app/controllers/api/v1/netex_imports_controller.rb b/app/controllers/api/v1/netex_imports_controller.rb
index 212ba1302..cb863b9fc 100644
--- a/app/controllers/api/v1/netex_imports_controller.rb
+++ b/app/controllers/api/v1/netex_imports_controller.rb
@@ -48,10 +48,6 @@ module Api
end
def create_referential
- # TODO: >>> REMOVE ME !!!!
- metadata = ReferentialMetadataKludge.make_metadata_from_name! netex_import_params['name']
- # <<< REMOVE ME !!!!
-
@new_referential =
Referential.new(
name: netex_import_params['name'],
@@ -62,6 +58,24 @@ module Api
@new_referential.save
end
+ def metadata
+ metadata = ReferentialMetadata.new
+
+ if netex_import_params['file']
+ netex_file = STIF::NetexFile.new(netex_import_params['file'].to_io)
+ frame = netex_file.frames.first
+
+ if frame
+ metadata.periodes = frame.periods
+
+ line_objectids = frame.line_refs.map { |ref| "STIF:CODIFLIGNE:Line:#{ref}" }
+ metadata.line_ids = @workbench.lines.where(objectid: line_objectids).pluck(:id)
+ end
+ end
+
+ metadata
+ end
+
def netex_import_params
params
.require('netex_import')
diff --git a/lib/referential_metadata_kludge.rb b/lib/referential_metadata_kludge.rb
deleted file mode 100644
index 62318ce3c..000000000
--- a/lib/referential_metadata_kludge.rb
+++ /dev/null
@@ -1,22 +0,0 @@
-module ReferentialMetadataKludge extend self
-
- def make_metadata_from_name! name
- line_ids = Chouette::Line.where(
- objectid: ['C00108', 'C00109'].map do |id|
- "STIF:CODIFLIGNE:Line:#{id}"
- end
- ).pluck(:id)
-
- ReferentialMetadata.new(
- line_ids: line_ids,
- periodes: name_to_periods(name))
- end
-
- def name_to_periods name
- {'offre1' => [Date.new(2017,3,1)...Date.new(2017,3,29)],
- 'offre2' => [Date.new(2017,4,1)...Date.new(2017,12,31)],
- 'OFFRE_TRANSDEV_20170301122517' => [Date.new(2017,3,1)...Date.new(2017,3,29)],
- 'OFFRE_TRANSDEV_20170301122519' => [Date.new(2017,4,1)...Date.new(2017,12,31)]}.fetch name
- end
-
-end
diff --git a/lib/stif/netex_file.rb b/lib/stif/netex_file.rb
new file mode 100644
index 000000000..424663c1c
--- /dev/null
+++ b/lib/stif/netex_file.rb
@@ -0,0 +1,72 @@
+module STIF
+ class NetexFile
+
+ CALENDAR_FILE_NAME = 'calendriers.xml'
+ LINE_FILE_FORMAT = /^offre_.*\.xml$/
+ XML_NAME_SPACE = "http://www.netex.org.uk/netex"
+
+ def initialize(file_name)
+ @file_name = file_name
+ end
+
+ def frames
+ frames = Hash.new { |h,k| h[k] = NetexFile::Frame.new(k) }
+ Zip::File.open(@file_name) do |zipfile|
+ zipfile.each do |entry|
+ next unless entry.ftype == :file
+
+ entry_dir_name, entry_file_name = File.split(entry.name)
+ case entry_file_name
+ when CALENDAR_FILE_NAME
+ entry.get_input_stream do |stream|
+ frames[entry_dir_name].parse_calendars(stream.read)
+ end
+ when LINE_FILE_FORMAT
+ frames[entry_dir_name].add_offer_file(entry_file_name)
+ end
+ end
+ end
+ frames.values
+ end
+
+ end
+
+ class NetexFile::Frame
+
+ attr_accessor :name
+
+ def initialize(name)
+ @name = name
+ end
+
+ def parse_calendars(calendars)
+ # <netex:ValidBetween>
+ # <netex:FromDate>2017-03-01</netex:FromDate>
+ # <netex:ToDate>2017-03-31</netex:ToDate>
+ # </netex:ValidBetween>
+ xml = Nokogiri::XML(calendars)
+ xml.xpath("//netex:ValidBetween", "netex" => NetexFile::XML_NAME_SPACE).each do |valid_between|
+ from_date = valid_between.xpath("netex:FromDate").try :text
+ to_date = valid_between.xpath("netex:ToDate").try :text
+ periods << Range.new(Date.parse(from_date), Date.parse(to_date))
+ end
+ end
+
+ LINE_FORMAT = /^offre_.*\.xml$/
+
+ def add_offer_file(file_name)
+ if file_name =~ /^offre_([^_]*)_/
+ line_refs << $1
+ end
+ end
+
+ def periods
+ @periods ||= []
+ end
+
+ def line_refs
+ @line_refs ||= []
+ end
+
+ end
+end
diff --git a/spec/fixtures/single_reference_import.zip b/spec/fixtures/single_reference_import.zip
index 4aee23614..37a516f69 100644
--- a/spec/fixtures/single_reference_import.zip
+++ b/spec/fixtures/single_reference_import.zip
Binary files differ
diff --git a/spec/lib/stif/netex_file_spec.rb b/spec/lib/stif/netex_file_spec.rb
new file mode 100644
index 000000000..d84807fe5
--- /dev/null
+++ b/spec/lib/stif/netex_file_spec.rb
@@ -0,0 +1,27 @@
+require "rails_helper"
+
+RSpec.describe STIF::NetexFile do
+
+ let( :zip_file ){ fixtures_path 'OFFRE_TRANSDEV_2017030112251.zip' }
+
+ let(:frames) { STIF::NetexFile.new(zip_file).frames }
+
+ it "should return a frame for each sub directory" do
+ expect(frames.size).to eq(2)
+ end
+
+ def period(from, to)
+ Range.new(Date.parse(from), Date.parse(to))
+ end
+
+
+ context "each frame" do
+ it "should return the line identifiers defined in frame" do
+ expect(frames.map(&:line_refs)).to eq([%w{C00109 C00108}]*2)
+ end
+ it "should return periods defined in frame calendars" do
+ expect(frames.map(&:periods)).to eq([[period("2017-04-01", "2017-12-31")], [period("2017-03-01","2017-03-31")]])
+ end
+ end
+
+end
diff --git a/spec/requests/api/v1/netex_import_spec.rb b/spec/requests/api/v1/netex_import_spec.rb
index 70a41256e..06ff76e14 100644
--- a/spec/requests/api/v1/netex_import_spec.rb
+++ b/spec/requests/api/v1/netex_import_spec.rb
@@ -32,9 +32,8 @@ RSpec.describe "NetexImport", type: :request do
let( :authorization ){ authorization_token_header( get_api_key.token ) }
it 'succeeds' do
- # TODO: Handle better when `ReferentialMetadataKludge` is reworked
- create(:line, objectid: 'STIF:CODIFLIGNE:Line:C00108')
- create(:line, objectid: 'STIF:CODIFLIGNE:Line:C00109')
+ create(:line, objectid: 'STIF:CODIFLIGNE:Line:C00108', line_referential: workbench.line_referential)
+ create(:line, objectid: 'STIF:CODIFLIGNE:Line:C00109', line_referential: workbench.line_referential)
post_request.(netex_import: legal_attributes)
expect( response ).to be_success
@@ -46,16 +45,15 @@ RSpec.describe "NetexImport", type: :request do
end
it 'creates a NetexImport object in the DB' do
- # TODO: Handle better when `ReferentialMetadataKludge` is reworked
- create(:line, objectid: 'STIF:CODIFLIGNE:Line:C00108')
- create(:line, objectid: 'STIF:CODIFLIGNE:Line:C00109')
+ create(:line, objectid: 'STIF:CODIFLIGNE:Line:C00108', line_referential: workbench.line_referential)
+ create(:line, objectid: 'STIF:CODIFLIGNE:Line:C00109', line_referential: workbench.line_referential)
expect{ post_request.(netex_import: legal_attributes) }.to change{NetexImport.count}.by(1)
end
it 'creates a correct Referential' do
- create(:line, objectid: 'STIF:CODIFLIGNE:Line:C00108')
- create(:line, objectid: 'STIF:CODIFLIGNE:Line:C00109')
+ create(:line, objectid: 'STIF:CODIFLIGNE:Line:C00108', line_referential: workbench.line_referential)
+ create(:line, objectid: 'STIF:CODIFLIGNE:Line:C00109', line_referential: workbench.line_referential)
legal_attributes # force object creation for correct to change behavior
expect{post_request.(netex_import: legal_attributes)}.to change{Referential.count}.by(1)