66require 'atco/journey_route'
77require 'atco/journey_times'
88require 'atco/z_location'
9+ require 'atco/operator'
10+ require 'atco/header'
911require 'iconv'
1012
1113module Atco
@@ -17,113 +19,130 @@ def parse(file)
1719 path = File . expand_path ( file )
1820 data = File . readlines ( path )
1921
20- # locations = []
21- journeys = [ ]
22- header = nil
22+ locations = [ ]
23+ # journeys = []
2324 journey = nil
25+ header = nil
26+ z_journey = nil
27+ operator = nil
28+ journeys = nil
29+ # locations = []
30+ operators = [ ]
31+ gmpte_info = [ ]
32+ first_line = data . first
33+ header = Header . new ( parse_header first_line )
2434 data . each do |line |
2535 case line [ 0 , 2 ]
2636 when 'QS'
2737 journey = Journey . new ( parse_journey_header line )
28- journeys << journey
38+ z_journey . journeys << journey
2939 when 'QO'
3040 journey . stops << Stop . new ( parse_origin line )
3141 when 'QP'
42+ #operator
43+ operator = Operator . new ( parse_operator line )
44+ operators << operator
45+ when 'QQ'
46+ #additional operator info
47+ additional_operator_info = parse_additonal_operator_info line
48+ operator . address = additional_operator_info [ :address ]
3249 when 'QB'
50+ additional_info = parse_additional_location_info ( line )
51+ location . easting = additonal_info [ :grid_reference_easting ]
52+ location . northing = additonal_info [ :grid_reference_northing ]
53+ location . short_code = additional_info [ :location ]
3354 when 'QL'
55+ location = Location . new ( parse_location line )
56+ locations << location
57+ when 'QA'
58+ #alternative location
3459 when 'QH'
3560 when 'QI'
3661 journey . stops << Stop . new ( parse_intermediate line )
3762 when 'QT'
3863 journey . stops << Stop . new ( parse_destination line )
3964 when 'QO'
40- end
41- end
42- # return journeys
43- # end
44-
45- path = File . expand_path ( file )
46- data = File . readlines ( path )
47- gmpte_info = [ ]
48- record_ended = false
49- journey = nil
50- data . each do |line |
51- case line [ 0 , 2 ]
52- when 'ZD'
65+ when 'QR'
66+ #repetition records
67+ when 'ZD'
5368 #this is a gmpte specific thing
5469 #it's a new journey so start again
5570 #the assumption is that the journey record is defined before the stops etc
56- record_ended = false
71+ # record_ended = false
5772 z_locations = [ ]
58- journey = JourneyTimes . new ( parse_journey_times line )
59- journey . z_locations = z_locations
60- journey . journey_identifiers = [ ]
61- when 'ZA'
73+ z_journey = JourneyTimes . new ( parse_journey_times line )
74+ z_journey . z_locations = z_locations
75+ z_journey . journey_identifiers = [ ]
76+ journeys = [ ]
77+ z_journey . journeys = journeys
78+ gmpte_info << z_journey
79+ when 'ZA'
6280 #this is a gmpte specific thing
63- journey . z_locations << ZLocation . new ( parse_stop_location_name line )
64- when 'ZS'
81+ z_journey . z_locations << ZLocation . new ( parse_stop_location_name line )
82+ when 'ZS'
6583 #this is a gmpte specific thing
66- journey . journey_route = JourneyRoute . new ( parse_journey_route line )
67- when 'QS'
68- journey_info = parse_journey_header ( line )
69- #each journey can have several records due to bank holidays, weekends etc.
70- journey . journey_identifiers << journey_info [ :unique_journey_identifier ]
71- #That's the end of the record
72- gmpte_info << journey unless record_ended
73- record_ended = true
74- end
84+ z_journey . journey_route = JourneyRoute . new ( parse_journey_route line )
7585 end
76- return { :journeys => journeys , :gmpte_info => gmpte_info }
86+ end
87+
88+ return { :header => header , :locations => locations , :operators => operators , :journeys => journeys , :gmpte_info => gmpte_info }
89+ end
90+
91+ def parse_header ( string )
92+ {
93+ :file_type => string [ 0 , 8 ] ,
94+ :version => "#{ string [ 8 , 2 ] . to_i } .#{ string [ 10 , 2 ] . to_i } " ,
95+ :file_originator => string [ 12 , 32 ] . strip! ,
96+ :source_product => string [ 44 , 16 ] . strip! ,
97+ :production_datetime => string [ 60 , 14 ]
98+ }
99+ end
100+
101+ def parse_bank_holiday ( string )
102+ {
103+ :record_identity => string [ 0 , 2 ] ,
104+ :transaction_type => string [ 2 , 1 ] ,
105+ :date_of_bank_holiday => string [ 3 , 8 ]
106+ }
107+ end
108+
109+ def parse_operator ( string )
110+ {
111+ :record_identity => string [ 0 , 2 ] ,
112+ :transaction_type => string [ 2 , 1 ] ,
113+ :operator => parse_value ( string [ 3 , 4 ] ) ,
114+ :operator_short_form => parse_value ( string [ 7 , 24 ] ) ,
115+ :operator_legal_name => parse_value ( string [ 31 , 48 ] )
116+ }
117+ end
118+
119+ def parse_additonal_operator_info
120+ {
121+ :record_identity => string [ 0 , 2 ] ,
122+ :address => string [ 77 , 3 ]
123+ }
124+ end
125+
126+ def parse_additional_location_info ( string )
127+ {
128+ :record_identity => string [ 0 , 2 ] ,
129+ :transaction_type => string [ 2 , 1 ] ,
130+ :location => string [ 3 , 12 ] . strip ,
131+ :grid_reference_easting => parse_value ( string [ 15 , 8 ] ) ,
132+ :grid_reference_northing => parse_value ( string [ 23 , 8 ] )
133+ }
134+ end
135+
136+ def parse_location ( string )
137+ {
138+ :record_identity => string [ 0 , 2 ] ,
139+ :transaction_type => string [ 2 , 1 ] ,
140+ :location => parse_value ( string [ 3 , 12 ] ) ,
141+ :full_location => parse_value ( string [ 15 , 48 ] ) ,
142+ :gazetteer_code => string [ 63 , 1 ]
143+ }
77144 end
78145
79- # def parse_header(string)
80- # {
81- # Journey.new(:file_type => string[0,8],
82- # :version => "#{string[8,2].to_i}.#{string[10,2].to_i}",
83- # :file_originator => string[12,32].strip!,
84- # :source_product => string[44,16].strip!,
85- # :production_datetime => string[60,14])
86- # }
87- # end
88- #
89- # def parse_bank_holiday(string)
90- # {
91- # BankHoliday.new(:record_identity => string[0,2],
92- # :transaction_type => string[2,1],
93- # :date_of_bank_holiday => string[3,8])
94- # }
95- # end
96- #
97- # def parse_operator(string)
98- # {
99- # Operator.new(:record_identity => string[0,2],
100- # :transaction_type => string[2,1],
101- # :operator => parse_value(string[3,4]),
102- # :operator_short_form => parse_value(string[7,24]),
103- # :operator_legal_name => parse_value(string[31,48]))
104- # }
105- # end
106- #
107- # def parse_additional_location_info(string)
108- # {
109- # :record_identity => string[0,2],
110- # :transaction_type => string[2,1],
111- # :location => string[3,12].strip,
112- # :grid_reference_easting => parse_value(string[15,8]),
113- # :grid_reference_northing => parse_value(string[23,8])
114- # }
115- # end
116- #
117- # def parse_location(string)
118- # {
119- # :record_identity => string[0,2],
120- # :transaction_type => string[2,1],
121- # :location => parse_value(string[3,12]),
122- # :full_location => parse_value(string[15,48]),
123- # :gazetteer_code => string[63,1]
124- # }
125- # end
126- #
127146 def parse_destination ( string )
128147 {
129148 :record_identity => string [ 0 , 2 ] ,
0 commit comments