Skip to content

Commit b7c1da3

Browse files
ianwdunlopdavidjrice
authored andcommitted
parse file line by line with case statements.
new models
1 parent d4c7c81 commit b7c1da3

File tree

4 files changed

+124
-115
lines changed

4 files changed

+124
-115
lines changed

lib/atco.rb

Lines changed: 86 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -11,70 +11,42 @@
1111
module Atco
1212
VERSION = '0.0.1'
1313

14-
class << self
15-
16-
@path = nil
17-
@@methods = {
18-
:bank_holiday => 'QH',
19-
:operator => 'QP',
20-
:additional_location_info => 'QB',
21-
:location => 'QL',
22-
:destination => 'QT',
23-
:intermediate => 'QI',
24-
:origin => 'QO',
25-
:journey_header => 'QS'
26-
}
27-
28-
@@gmpte_methods = {
29-
:journey_route => 'ZS',
30-
:stop_location_name => 'ZA',
31-
:journey_times => 'ZD'
32-
}
14+
class Parser
3315

3416
def parse(file)
35-
@path = File.expand_path(file)
36-
data = File.readlines(@path)
17+
path = File.expand_path(file)
18+
data = File.readlines(path)
3719

38-
objects = []
39-
current_journey = nil
40-
current_location = nil
41-
locations = []
42-
journeys = {}
20+
# locations = []
21+
journeys = []
4322
header = nil
44-
23+
journey = nil
4524
data.each do |line|
46-
if line == data.first
47-
header = parse_header(line)
48-
next
49-
end
50-
@@methods.each do |method,identifier|
51-
object = self.send("parse_#{method}", line)
52-
if object[:record_identity] && object[:record_identity] == identifier
53-
current_journey = object if object[:record_identity] && object[:record_identity] == @@methods[:journey_header]
54-
if object[:record_identity] && ( object[:record_identity] == @@methods[:location] || object[:record_identity] == @@methods[:additional_location_info] )
55-
if object[:record_identity] == @@methods[:location]
56-
current_location = object
57-
else
58-
locations << Location.new(current_location, object)
59-
end
60-
end
61-
62-
if current_journey
63-
if journeys[current_journey[:unique_journey_identifier]]
64-
journeys[current_journey[:unique_journey_identifier]].stops << Stop.new(object)
65-
else
66-
journeys[current_journey[:unique_journey_identifier]] = Journey.new(object)
67-
end
68-
end
69-
objects << object
70-
end
25+
case line[0,2]
26+
when 'QS'
27+
journey = Journey.new(parse_journey_header line)
28+
journeys << journey
29+
when 'QO'
30+
journey.stops << Stop.new(parse_origin line)
31+
when 'QP'
32+
when 'QB'
33+
when 'QL'
34+
when 'QH'
35+
when 'QI'
36+
journey.stops << Stop.new(parse_intermediate line)
37+
when 'QT'
38+
journey.stops << Stop.new(parse_destination line)
39+
when 'QO'
7140
end
7241
end
42+
# return journeys
43+
# end
7344

74-
@path = File.expand_path(file)
75-
data = File.readlines(@path)
45+
path = File.expand_path(file)
46+
data = File.readlines(path)
7647
gmpte_info=[]
7748
record_ended = false
49+
journey = nil
7850
data.each do |line|
7951
case line[0,2]
8052
when 'ZD'
@@ -83,75 +55,75 @@ def parse(file)
8355
#the assumption is that the journey record is defined before the stops etc
8456
record_ended = false
8557
z_locations=[]
86-
@journey = JourneyTimes.new(parse_journey_times line)
87-
@journey.z_locations=z_locations
88-
@journey.journey_identifiers = []
58+
journey = JourneyTimes.new(parse_journey_times line)
59+
journey.z_locations=z_locations
60+
journey.journey_identifiers = []
8961
when 'ZA'
9062
#this is a gmpte specific thing
91-
@journey.z_locations << ZLocation.new(parse_stop_location_name line)
63+
journey.z_locations << ZLocation.new(parse_stop_location_name line)
9264
when 'ZS'
9365
#this is a gmpte specific thing
94-
@journey.journey_route = JourneyRoute.new(parse_journey_route line)
66+
journey.journey_route = JourneyRoute.new(parse_journey_route line)
9567
when 'QS'
9668
journey_info = parse_journey_header(line)
9769
#each journey can have several records due to bank holidays, weekends etc.
98-
@journey.journey_identifiers << journey_info[:unique_journey_identifier]
70+
journey.journey_identifiers << journey_info[:unique_journey_identifier]
9971
#That's the end of the record
100-
gmpte_info << @journey unless record_ended
72+
gmpte_info << journey unless record_ended
10173
record_ended = true
10274
end
10375
end
104-
return {:header => header, :locations => locations, :journeys => journeys, :gmpte_info=>gmpte_info}
105-
end
106-
107-
def parse_header(string)
108-
{
109-
:file_type => string[0,8],
110-
:version => "#{string[8,2].to_i}.#{string[10,2].to_i}",
111-
:file_originator => string[12,32].strip!,
112-
:source_product => string[44,16].strip!,
113-
:production_datetime => string[60,14]
114-
}
76+
return {:journeys => journeys, :gmpte_info=>gmpte_info}
11577
end
11678

117-
def parse_bank_holiday(string)
118-
{
119-
:record_identity => string[0,2],
120-
:transaction_type => string[2,1],
121-
:date_of_bank_holiday => string[3,8]
122-
}
123-
end
124-
125-
def parse_operator(string)
126-
{
127-
:record_identity => string[0,2],
128-
:transaction_type => string[2,1],
129-
:operator => parse_value(string[3,4]),
130-
:operator_short_form => parse_value(string[7,24]),
131-
:operator_legal_name => parse_value(string[31,48])
132-
}
133-
end
134-
135-
def parse_additional_location_info(string)
136-
{
137-
:record_identity => string[0,2],
138-
:transaction_type => string[2,1],
139-
:location => string[3,12].strip,
140-
:grid_reference_easting => parse_value(string[15,8]),
141-
:grid_reference_northing => parse_value(string[23,8])
142-
}
143-
end
144-
145-
def parse_location(string)
146-
{
147-
:record_identity => string[0,2],
148-
:transaction_type => string[2,1],
149-
:location => parse_value(string[3,12]),
150-
:full_location => parse_value(string[15,48]),
151-
:gazetteer_code => string[63,1]
152-
}
153-
end
154-
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+
#
155127
def parse_destination(string)
156128
{
157129
:record_identity => string[0,2],
@@ -162,7 +134,7 @@ def parse_destination(string)
162134
:fare_stage_indicator => string[23,2]
163135
}
164136
end
165-
137+
166138
def parse_intermediate(string)
167139
{
168140
:record_identity => string[0,2],
@@ -175,7 +147,7 @@ def parse_intermediate(string)
175147
:fare_stage_indicator => string[28,2]
176148
}
177149
end
178-
150+
179151
def parse_origin(string)
180152
{
181153
:record_identity => string[0,2],
@@ -186,7 +158,7 @@ def parse_origin(string)
186158
:fare_stage_indicator => string[23,2]
187159
}
188160
end
189-
161+
#
190162
def parse_journey_header(string)
191163
{
192164
:record_identity => string[0,2],
@@ -211,7 +183,7 @@ def parse_journey_header(string)
211183
:route_direction => string[64,1]
212184
}
213185
end
214-
186+
#
215187
def parse_value(value)
216188
return value.strip if value
217189
end

lib/atco/bank_hoilday.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Atco
2+
3+
class BankHoliday
4+
:record_identity => string[0,2],
5+
:transaction_type => string[2,1],
6+
:date_of_bank_holiday
7+
attr_accessor :record_identity, :transaction_type, :date_of_bank_holiday
8+
attr_writer :record_identity, :transaction_type, :date_of_bank_holiday
9+
10+
def initialize(data)
11+
@record_identity = data[:record_identity]
12+
@transaction_type = data[:transaction_type]
13+
@date_of_bank_holiday = data[:date_of_bank_holiday]
14+
end
15+
16+
end
17+
18+
end

lib/atco/operator.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Atco
2+
3+
class Operator
4+
5+
attr_accessor :record_identity, :transaction_type, :operator, :operator_short_form, :operator_legal_name
6+
attr_writer :record_identity, :transaction_type, :operator, :operator_short_form, :operator_legal_name
7+
8+
def initialize(data)
9+
@record_identity = data[:record_identity]
10+
@transaction_type = data[:transaction_type]
11+
@operator = data[:operator]
12+
@operator_short_form = data[:operator_short_form]
13+
@operator_legal_name = data[:operator_legal_name]
14+
end
15+
16+
end
17+
18+
end

lib/atco/stop.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Atco
22

33
class Stop
44

5-
attr_accessor :bay_number, :location, :timing_point_indicator, :fare_stage_indicator, :published_departure_time, :record_identity
5+
attr_accessor :bay_number, :location, :timing_point_indicator, :fare_stage_indicator, :published_arrival_time, :published_departure_time, :record_identity
66

77
def origin?; @record_identity == "QO"; end
88
def intermediate?; @record_identity == "QI"; end
@@ -14,6 +14,7 @@ def initialize(data)
1414
@timing_point_indicator = data[:timing_point_indicator]
1515
@fare_stage_indicator = data[:fare_stage_indicator]
1616
@published_departure_time = data[:published_departure_time]
17+
@published_arrival_time = data[:published_arrival_time]
1718
@record_identity = data[:record_identity]
1819
end
1920

0 commit comments

Comments
 (0)