atco 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 82cd5858676f7202932ecbe6d68846066019bd75c5637c33a44ec3eaf83fe599
4
- data.tar.gz: 144dad716574ca9f45f63170193de8c1115edc6a4ece1557516c57e9d1d250a3
3
+ metadata.gz: 59b747dbf14f2f4ab07ca86c8344fda6294efbf4d7b83cdd11fcfa45a9e4961b
4
+ data.tar.gz: 276916993eae21683270e35ba8d4d167820eb2ae65dd410ab535f94a7ba50134
5
5
  SHA512:
6
- metadata.gz: 947c0a1bbfa755bacb1d907c50f90720e98dcebfef88d500b6e418c652827846ea2c32372e8aed4fdd8e1e4aad71ce9f61acd1f0c51b0dd68bd696d1e9774a98
7
- data.tar.gz: 9556d7e0bc6cd268b326475c03c3136a59f5f0066db0d1a84fa7510ee9e4eeb0a0f8cc1a63cb1869bd7165184eb54481afdb984d0cd2bb79bc7c6dcfe21a1e32
6
+ metadata.gz: 54cf093ae098cc2fbc2b38067ffeeec9fa7a513107b00a4cb7822b8c0b65e273b016be3465dffed59403a9f88ce9f4cfb3c750236fb8c1354ffa9347511b26ca
7
+ data.tar.gz: 92c5635e38a717f4ba9f0e036d081463b4d84bb5bd3d1788d99b191b54e8bcd09f6a44bc114f5495302a191fc47b8887f8c97934696c7a5b6f3ad9d6217fc544
data/lib/atco/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Atco
4
- VERSION = "1.0.2"
4
+ VERSION = "1.0.3"
5
5
  end
data/lib/atco.rb CHANGED
@@ -9,6 +9,8 @@ require_relative "atco/version"
9
9
 
10
10
  # Public: Atco is a module that provides a parser for the ATCO-CIF data format.
11
11
  module Atco # rubocop:disable Metrics/ModuleLength
12
+ class UnidentifiedRecordError < StandardError; end
13
+
12
14
  class << self # rubocop:disable Metrics/ClassLength
13
15
  @path = nil
14
16
  METHODS = {
@@ -21,6 +23,7 @@ module Atco # rubocop:disable Metrics/ModuleLength
21
23
  origin: "QO",
22
24
  journey_header: "QS"
23
25
  }.freeze
26
+ METHODS_BY_RECORD_IDENTITY = METHODS.invert.freeze
24
27
 
25
28
  def parse(file) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
26
29
  @path = File.expand_path(file)
@@ -32,13 +35,20 @@ module Atco # rubocop:disable Metrics/ModuleLength
32
35
  locations = []
33
36
  journeys = {}
34
37
  header = nil
38
+ unparsed = []
35
39
 
36
- data.each do |line|
40
+ data.each_with_index do |line, line_number| # rubocop:disable Metrics/BlockLength
37
41
  if line == data.first
38
42
  header = parse_header(line)
39
43
  next
40
44
  end
41
- METHODS.each do |method, identifier|
45
+
46
+ identifier = line[0, 2]
47
+ method = METHODS_BY_RECORD_IDENTITY[identifier]
48
+
49
+ begin
50
+ raise UnidentifiedRecordError, "Unidentified record: #{identifier}" unless method
51
+
42
52
  object = send("parse_#{method}", line)
43
53
  next unless object[:record_identity] && object[:record_identity] == identifier
44
54
 
@@ -59,9 +69,12 @@ module Atco # rubocop:disable Metrics/ModuleLength
59
69
  end
60
70
  end
61
71
  objects << object
72
+ rescue UnidentifiedRecordError
73
+ unparsed << {line: line, line_number: line_number}
74
+ next
62
75
  end
63
76
  end
64
- { header: header, locations: locations, journeys: journeys }
77
+ { header: header, locations: locations, journeys: journeys, unparsed: unparsed }
65
78
  end
66
79
 
67
80
  def parse_header(string)
data/spec/atco_spec.rb CHANGED
@@ -194,5 +194,24 @@ RSpec.describe Atco do # rubocop:disable Metrics/BlockLength
194
194
  json = JSON.parse(data)
195
195
  expect(json).to be_a(Hash)
196
196
  end
197
+
198
+ it "should return 17 unparsed lines" do
199
+ expect(@atco[:unparsed].size).to eq(17)
200
+ end
201
+
202
+ it "should not parse GS records" do
203
+ expect(@atco[:unparsed][0]).to eq({
204
+ line: "GS00001433 N Belfast Metro Ops 7000\n",
205
+ line_number: 3
206
+ })
207
+ end
208
+
209
+ it "should not parse GR records" do
210
+ expect(@atco[:unparsed][1]).to eq({
211
+ line: "GR00001433Donegall Square East 7000\n",
212
+ line_number: 4
213
+ })
214
+ end
215
+
197
216
  end
198
217
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: atco
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Rice