Skip to content

Commit 711fb54

Browse files
ianwdunlopdavidjrice
authored andcommitted
parse additional types of records
1 parent b7c1da3 commit 711fb54

File tree

5 files changed

+111
-91
lines changed

5 files changed

+111
-91
lines changed

atco.gemspec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ Gem::Specification.new do |s|
2525
"lib/atco/stop.rb",
2626
"lib/atco/z_location.rb",
2727
"lib/atco/journey_times.rb",
28-
"lib/atco/journey_route.rb"
28+
"lib/atco/journey_route.rb",
29+
"lib/atco/header.rb",
30+
"lib/atco/operator.rb"
2931
]
3032
s.homepage = %q{http://github.com/davidjrice/atco}
3133
s.rdoc_options = ["--charset=UTF-8"]

lib/atco.rb

Lines changed: 101 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
require 'atco/journey_route'
77
require 'atco/journey_times'
88
require 'atco/z_location'
9+
require 'atco/operator'
10+
require 'atco/header'
911
require 'iconv'
1012

1113
module 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],

lib/atco/journey_times.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ module Atco
22

33
class JourneyTimes
44

5-
attr_accessor :journey_identifiers, :record_identity, :start_time, :end_time, :days_of_the_week_text, :z_locations, :journey_route
6-
attr_writer :journey_identifiers, :record_identity, :start_time, :end_time, :days_of_the_week_text, :z_locations, :journey_route
5+
attr_accessor :journeys, :journey_identifiers, :record_identity, :start_time, :end_time, :days_of_the_week_text, :z_locations, :journey_route
6+
attr_writer :journeys, :journey_identifiers, :record_identity, :start_time, :end_time, :days_of_the_week_text, :z_locations, :journey_route
77

88
def initialize(data)
99
@record_identity = data[:record_identity]

lib/atco/location.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ module Atco
22

33
class Location
44

5-
attr_accessor :name, :identifier, :easting, :northing, :gazeteer_code
5+
attr_accessor :name, :identifier, :easting, :northing, :gazeteer_code, :short_code
6+
attr_writer :name, :identifier, :easting, :northing, :gazeteer_code, :short_code
67

7-
def initialize(location_header, additional_location_information)
8+
def initialize(location_header)
89
@name = location_header[:full_location]
910
@identifier = location_header[:record_identity]
10-
@easting = additional_location_information[:grid_reference_easting]
11-
@northing = additional_location_information[:grid_reference_northing]
1211
@gazeteer_code = location_header[:gazetteer_code]
1312
end
1413

lib/atco/operator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ module Atco
22

33
class Operator
44

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
5+
attr_accessor :record_identity, :transaction_type, :operator, :operator_short_form, :operator_legal_name, :address
6+
attr_writer :record_identity, :transaction_type, :operator, :operator_short_form, :operator_legal_name, :address
77

88
def initialize(data)
99
@record_identity = data[:record_identity]

0 commit comments

Comments
 (0)