Module: Atco
- Defined in:
- lib/atco.rb,
lib/atco/stop.rb,
lib/atco/header.rb,
lib/atco/journey.rb,
lib/atco/version.rb,
lib/atco/location.rb
Overview
Public: Atco is a module that provides a parser for the ATCO-CIF data format.
Defined Under Namespace
Classes: Header, Journey, Location, Stop, UnidentifiedRecordError
Constant Summary collapse
- METHODS =
{ bank_holiday: "QH", operator: "QP", additional_location_info: "QB", location: "QL", destination: "QT", intermediate: "QI", origin: "QO", journey_header: "QS" }.freeze
- METHODS_BY_RECORD_IDENTITY =
METHODS.invert.freeze
- VERSION =
"1.0.9"
Class Method Summary collapse
-
.parse(file) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity.
- .parse_additional_location_info(string) ⇒ Object
- .parse_bank_holiday(string) ⇒ Object
- .parse_destination(string) ⇒ Object
- .parse_intermediate(string) ⇒ Object
-
.parse_journey_header(string) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
- .parse_location(string) ⇒ Object
- .parse_operator(string) ⇒ Object
- .parse_origin(string) ⇒ Object
- .parse_value(value) ⇒ Object
Class Method Details
.parse(file) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/atco.rb', line 29 def parse(file) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity @path = File.(file) data = File.readlines(@path) current_journey = nil current_location = nil locations = [] journeys = {} header = nil unparsed = [] data.each_with_index do |line, line_number| # rubocop:disable Metrics/BlockLength if line_number.zero? header = Header.parse(line) next end identifier = line[0, 2] method = METHODS_BY_RECORD_IDENTITY[identifier] begin raise UnidentifiedRecordError, "Unidentified record: #{identifier}" unless method object = send("parse_#{method}", line) next unless object[:record_identity] && object[:record_identity] == identifier case method when :journey_header current_journey = object when :location current_location = object when :additional_location_info locations << Location.new(current_location, object) end if current_journey if journeys[current_journey[:unique_journey_identifier]] journeys[current_journey[:unique_journey_identifier]].stops << Stop.new(object) else journeys[current_journey[:unique_journey_identifier]] = Journey.new(object) end end rescue UnidentifiedRecordError unparsed << { line: line, line_number: line_number } next end end { header: header, locations: locations, journeys: journeys, unparsed: unparsed } end |
.parse_additional_location_info(string) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/atco.rb', line 97 def parse_additional_location_info(string) { record_identity: string[0, 2], transaction_type: string[2, 1], location: string[3, 12].strip, grid_reference_easting: parse_value(string[15, 8]), grid_reference_northing: parse_value(string[23, 8]) } end |
.parse_bank_holiday(string) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/atco.rb', line 79 def parse_bank_holiday(string) { record_identity: string[0, 2], transaction_type: string[2, 1], date_of_bank_holiday: string[3, 8] } end |
.parse_destination(string) ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/atco.rb', line 117 def parse_destination(string) { record_identity: string[0, 2], location: string[2, 12], published_arrival_time: string[14, 4], bay_number: parse_value(string[18, 3]), timing_point_indicator: string[21, 2], fare_stage_indicator: string[23, 2] } end |
.parse_intermediate(string) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/atco.rb', line 128 def parse_intermediate(string) { record_identity: string[0, 2], location: string[2, 12], published_arrival_time: string[14, 4], published_departure_time: string[18, 4], activity_flag: string[22, 1], bay_number: parse_value(string[23, 3]), timing_point_indicator: string[26, 2], fare_stage_indicator: string[28, 2] } end |
.parse_journey_header(string) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/atco.rb', line 152 def parse_journey_header(string) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength { record_identity: string[0, 2], transaction_type: string[2, 1], operator: string[3, 4].strip, unique_journey_identifier: string[7, 6], first_date_of_operation: parse_value(string[13, 8]), last_date_of_operation: parse_value(string[21, 8]), operates_on_mondays: string[29, 1], operates_on_tuesdays: string[30, 1], operates_on_wednesdays: string[31, 1], operates_on_thursdays: string[32, 1], operates_on_fridays: string[33, 1], operates_on_saturdays: string[34, 1], operates_on_sundays: string[35, 1], school_term_time: parse_value(string[36, 1]), bank_holidays: parse_value(string[37, 1]), route_number: parse_value(string[38, 4]), running_board: parse_value(string[42, 6]), vehicle_type: parse_value(string[48, 8]), registration_number: parse_value(string[56, 8]), route_direction: string[64, 1] } end |
.parse_location(string) ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/atco.rb', line 107 def parse_location(string) { record_identity: string[0, 2], transaction_type: string[2, 1], location: parse_value(string[3, 12]), full_location: parse_value(string[15, 48]), gazetteer_code: string[63, 1] } end |
.parse_operator(string) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/atco.rb', line 87 def parse_operator(string) { record_identity: string[0, 2], transaction_type: string[2, 1], operator: parse_value(string[3, 4]), operator_short_form: parse_value(string[7, 24]), operator_legal_name: parse_value(string[31, 48]) } end |
.parse_origin(string) ⇒ Object
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/atco.rb', line 141 def parse_origin(string) { record_identity: string[0, 2], location: string[2, 12], published_departure_time: string[14, 4], bay_number: parse_value(string[18, 3]), timing_point_indicator: string[21, 2], fare_stage_indicator: string[23, 2] } end |
.parse_value(value) ⇒ Object
177 178 179 |
# File 'lib/atco.rb', line 177 def parse_value(value) value&.strip end |