aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/referential_metadata_kludge.rb22
-rw-r--r--lib/stif/netex_file.rb72
2 files changed, 72 insertions, 22 deletions
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