atco 1.0.5 → 1.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/atco/header.rb +48 -0
- data/lib/atco/version.rb +1 -1
- data/lib/atco.rb +11 -19
- data/spec/atco_spec.rb +0 -96
- data/spec/integration/translink_spec.rb +71 -0
- data/spec/unit/header_spec.rb +30 -0
- metadata +6 -3
- /data/spec/fixtures/{example.cif → translink-example.cif} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9b77a9ff8bc2044b0985cdc1cf6b9f9f997e6884c72ce88e713cc807c04aa40
|
4
|
+
data.tar.gz: 8c09fd9b592ce9c1c8eae5b2bf4b36e9b1f1e8a98aff075773285f4a2da28e92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7962c7bd938e9ec5d406b72994148b01368651b49ae4b023088dda40e8317895999805f1d6d8e5cc58d23caf5eb7d05da71fc64251cab7e666c30ba38302929b
|
7
|
+
data.tar.gz: 4b1469dce23b861431b42cbdc1aeaf0f731388c648fb5bd4951b72e1fe889ce567cf0ff451c633737c6879a6cf950752409f79675e83234dee47c60dc03494bf
|
data/lib/atco/header.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Atco
|
4
|
+
# Atco::Header is a class to abstract ATCO-CIF Header data.
|
5
|
+
class Header
|
6
|
+
attr_accessor :file_type,
|
7
|
+
:version,
|
8
|
+
:file_originator,
|
9
|
+
:source_product,
|
10
|
+
:production_datetime
|
11
|
+
|
12
|
+
# Public: Parse a header line from an ATCO-CIF file.
|
13
|
+
# EXAMPLE:
|
14
|
+
# "ATCO-CIF0500Electronic Registration MIA 4.20.18 20090915113809\r\n"
|
15
|
+
def self.parse(line)
|
16
|
+
data = {
|
17
|
+
file_type: line[0, 8],
|
18
|
+
version: "#{line[8, 2].to_i}.#{line[10, 2].to_i}",
|
19
|
+
file_originator: line[12, 32].strip!,
|
20
|
+
source_product: line[44, 16].strip!,
|
21
|
+
production_datetime: line[60, 14]
|
22
|
+
}
|
23
|
+
new(data)
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(data)
|
27
|
+
@file_type = data[:file_type]
|
28
|
+
@version = data[:version]
|
29
|
+
@file_originator = data[:file_originator]
|
30
|
+
@source_product = data[:source_product]
|
31
|
+
@production_datetime = data[:production_datetime]
|
32
|
+
end
|
33
|
+
|
34
|
+
def attributes
|
35
|
+
{
|
36
|
+
file_type: @file_type,
|
37
|
+
version: @version,
|
38
|
+
file_originator: @file_originator,
|
39
|
+
source_product: @source_product,
|
40
|
+
production_datetime: @production_datetime
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
def to_json(*attrs)
|
45
|
+
attributes.to_json(*attrs)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/atco/version.rb
CHANGED
data/lib/atco.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require "open3"
|
4
4
|
require "tempfile"
|
5
|
+
require_relative "atco/header"
|
5
6
|
require_relative "atco/location"
|
6
7
|
require_relative "atco/journey"
|
7
8
|
require_relative "atco/stop"
|
@@ -38,8 +39,8 @@ module Atco # rubocop:disable Metrics/ModuleLength
|
|
38
39
|
unparsed = []
|
39
40
|
|
40
41
|
data.each_with_index do |line, line_number| # rubocop:disable Metrics/BlockLength
|
41
|
-
if
|
42
|
-
header =
|
42
|
+
if line_number.zero?
|
43
|
+
header = Header.parse(line)
|
43
44
|
next
|
44
45
|
end
|
45
46
|
|
@@ -52,13 +53,13 @@ module Atco # rubocop:disable Metrics/ModuleLength
|
|
52
53
|
object = send("parse_#{method}", line)
|
53
54
|
next unless object[:record_identity] && object[:record_identity] == identifier
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
56
|
+
case method
|
57
|
+
when :journey_header
|
58
|
+
current_journey = object
|
59
|
+
when :location
|
60
|
+
current_location = object
|
61
|
+
when :additional_location_info
|
62
|
+
locations << Location.new(current_location, object)
|
62
63
|
end
|
63
64
|
|
64
65
|
if current_journey
|
@@ -73,20 +74,11 @@ module Atco # rubocop:disable Metrics/ModuleLength
|
|
73
74
|
unparsed << { line: line, line_number: line_number }
|
74
75
|
next
|
75
76
|
end
|
77
|
+
objects << object
|
76
78
|
end
|
77
79
|
{ header: header, locations: locations, journeys: journeys, unparsed: unparsed }
|
78
80
|
end
|
79
81
|
|
80
|
-
def parse_header(string)
|
81
|
-
{
|
82
|
-
file_type: string[0, 8],
|
83
|
-
version: "#{string[8, 2].to_i}.#{string[10, 2].to_i}",
|
84
|
-
file_originator: string[12, 32].strip!,
|
85
|
-
source_product: string[44, 16].strip!,
|
86
|
-
production_datetime: string[60, 14]
|
87
|
-
}
|
88
|
-
end
|
89
|
-
|
90
82
|
def parse_bank_holiday(string)
|
91
83
|
{
|
92
84
|
record_identity: string[0, 2],
|
data/spec/atco_spec.rb
CHANGED
@@ -7,44 +7,6 @@ RSpec.describe Atco do # rubocop:disable Metrics/BlockLength
|
|
7
7
|
expect(Atco::VERSION).not_to be nil
|
8
8
|
end
|
9
9
|
|
10
|
-
it "should parse header from fixture" do
|
11
|
-
result = Atco.parse("spec/fixtures/example.cif")
|
12
|
-
expect(result[:header]).to eq(
|
13
|
-
{
|
14
|
-
file_type: "ATCO-CIF",
|
15
|
-
file_originator: "Electronic Registration",
|
16
|
-
source_product: "MIA 4.20.18",
|
17
|
-
version: "5.0",
|
18
|
-
production_datetime: "20090915113809"
|
19
|
-
}
|
20
|
-
)
|
21
|
-
end
|
22
|
-
|
23
|
-
it "should parse locations from fixture" do
|
24
|
-
result = Atco.parse("spec/fixtures/example.cif")
|
25
|
-
expect(result[:header]).to eq(
|
26
|
-
{
|
27
|
-
file_type: "ATCO-CIF",
|
28
|
-
file_originator: "Electronic Registration",
|
29
|
-
source_product: "MIA 4.20.18",
|
30
|
-
version: "5.0",
|
31
|
-
production_datetime: "20090915113809"
|
32
|
-
}
|
33
|
-
)
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should parse header" do
|
37
|
-
expect(Atco.parse_header("ATCO-CIF0500Electronic Registration MIA 4.20.18 20090915113809\r\n")).to eq(
|
38
|
-
{
|
39
|
-
file_type: "ATCO-CIF",
|
40
|
-
version: "5.0",
|
41
|
-
file_originator: "Electronic Registration",
|
42
|
-
source_product: "MIA 4.20.18",
|
43
|
-
production_datetime: "20090915113809"
|
44
|
-
}
|
45
|
-
)
|
46
|
-
end
|
47
|
-
|
48
10
|
it "should parse bank holiday" do
|
49
11
|
expect(Atco.parse_bank_holiday("QHN20061225")).to eq(
|
50
12
|
{
|
@@ -159,62 +121,4 @@ RSpec.describe Atco do # rubocop:disable Metrics/BlockLength
|
|
159
121
|
}
|
160
122
|
)
|
161
123
|
end
|
162
|
-
|
163
|
-
describe "with example.cif" do # rubocop:disable Metrics/BlockLength
|
164
|
-
before(:all) do
|
165
|
-
@atco = Atco.parse("spec/fixtures/example.cif")
|
166
|
-
end
|
167
|
-
|
168
|
-
it "should parse 1 journey" do
|
169
|
-
expect(@atco[:journeys].size).to eq(1)
|
170
|
-
end
|
171
|
-
|
172
|
-
it "should parse journeys into Atco::Joruney objects" do
|
173
|
-
expect(@atco[:journeys]["139748"]).to be_a_kind_of(Atco::Journey)
|
174
|
-
end
|
175
|
-
|
176
|
-
it "should parse 6 stops for journey 139748" do
|
177
|
-
expect(@atco[:journeys]["139748"].stops.size).to eq(6)
|
178
|
-
end
|
179
|
-
|
180
|
-
it "should parse 2 locations" do
|
181
|
-
expect(@atco[:locations].size).to eq(2)
|
182
|
-
end
|
183
|
-
|
184
|
-
it "should output file as JSON" do
|
185
|
-
output = File.join(File.dirname(__FILE__), "artefacts", "test.json")
|
186
|
-
File.open(output, "w+") do |f|
|
187
|
-
f.flush
|
188
|
-
f.write(JSON.pretty_generate(@atco))
|
189
|
-
end
|
190
|
-
|
191
|
-
expect(File.exist?(output)).to be true
|
192
|
-
|
193
|
-
data = File.read(output)
|
194
|
-
json = JSON.parse(data)
|
195
|
-
expect(json).to be_a(Hash)
|
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
|
-
{
|
205
|
-
line: "GS00001433 N Belfast Metro Ops 7000\n",
|
206
|
-
line_number: 3
|
207
|
-
}
|
208
|
-
)
|
209
|
-
end
|
210
|
-
|
211
|
-
it "should not parse GR records" do
|
212
|
-
expect(@atco[:unparsed][1]).to eq(
|
213
|
-
{
|
214
|
-
line: "GR00001433Donegall Square East 7000\n", # rubocop:disable Layout/LineLength
|
215
|
-
line_number: 4
|
216
|
-
}
|
217
|
-
)
|
218
|
-
end
|
219
|
-
end
|
220
124
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe "with example.cif" do # rubocop:disable Metrics/BlockLength
|
4
|
+
before(:all) do
|
5
|
+
@atco = Atco.parse("spec/fixtures/translink-example.cif")
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should parse header from fixture" do
|
9
|
+
expect(@atco[:header].attributes).to eq(
|
10
|
+
{
|
11
|
+
file_type: "ATCO-CIF",
|
12
|
+
file_originator: "Electronic Registration",
|
13
|
+
source_product: "MIA 4.20.18",
|
14
|
+
version: "5.0",
|
15
|
+
production_datetime: "20090915113809"
|
16
|
+
}
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should parse 1 journey" do
|
21
|
+
expect(@atco[:journeys].size).to eq(1)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should parse journeys into Atco::Joruney objects" do
|
25
|
+
expect(@atco[:journeys]["139748"]).to be_a_kind_of(Atco::Journey)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should parse 6 stops for journey 139748" do
|
29
|
+
expect(@atco[:journeys]["139748"].stops.size).to eq(6)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should parse 2 locations" do
|
33
|
+
expect(@atco[:locations].size).to eq(2)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should output file as JSON" do
|
37
|
+
output = File.join(File.dirname(__FILE__), "..", "artefacts", "test.json")
|
38
|
+
File.open(output, "w+") do |f|
|
39
|
+
f.flush
|
40
|
+
f.write(JSON.pretty_generate(@atco))
|
41
|
+
end
|
42
|
+
|
43
|
+
expect(File.exist?(output)).to be true
|
44
|
+
|
45
|
+
data = File.read(output)
|
46
|
+
json = JSON.parse(data)
|
47
|
+
expect(json).to be_a(Hash)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should return 17 unparsed lines" do
|
51
|
+
expect(@atco[:unparsed].size).to eq(17)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should not parse GS records" do
|
55
|
+
expect(@atco[:unparsed][0]).to eq(
|
56
|
+
{
|
57
|
+
line: "GS00001433 N Belfast Metro Ops 7000\n",
|
58
|
+
line_number: 3
|
59
|
+
}
|
60
|
+
)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should not parse GR records" do
|
64
|
+
expect(@atco[:unparsed][1]).to eq(
|
65
|
+
{
|
66
|
+
line: "GR00001433Donegall Square East 7000\n", # rubocop:disable Layout/LineLength
|
67
|
+
line_number: 4
|
68
|
+
}
|
69
|
+
)
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Atco::Header do
|
4
|
+
before(:all) do
|
5
|
+
line = "ATCO-CIF0500Electronic Registration MIA 4.20.18 20090915113809\r\n"
|
6
|
+
@header = Atco::Header.parse(line)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should be a Header object" do
|
10
|
+
expect(@header).to be_a_kind_of(Atco::Header)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should parse header to attributes" do
|
14
|
+
expect(@header.attributes).to eq(
|
15
|
+
{
|
16
|
+
file_type: "ATCO-CIF",
|
17
|
+
version: "5.0",
|
18
|
+
file_originator: "Electronic Registration",
|
19
|
+
source_product: "MIA 4.20.18",
|
20
|
+
production_datetime: "20090915113809"
|
21
|
+
}
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should parse header to json" do
|
26
|
+
expect(@header.to_json).to eq(
|
27
|
+
"{\"file_type\":\"ATCO-CIF\",\"version\":\"5.0\",\"file_originator\":\"Electronic Registration\",\"source_product\":\"MIA 4.20.18\",\"production_datetime\":\"20090915113809\"}" # rubocop:disable Metrics/LineLength
|
28
|
+
)
|
29
|
+
end
|
30
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: atco
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Rice
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-04-
|
11
|
+
date: 2024-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -49,14 +49,17 @@ files:
|
|
49
49
|
- README.md
|
50
50
|
- Rakefile
|
51
51
|
- lib/atco.rb
|
52
|
+
- lib/atco/header.rb
|
52
53
|
- lib/atco/journey.rb
|
53
54
|
- lib/atco/location.rb
|
54
55
|
- lib/atco/stop.rb
|
55
56
|
- lib/atco/version.rb
|
56
57
|
- spec/atco_spec.rb
|
57
|
-
- spec/fixtures/example.cif
|
58
58
|
- spec/fixtures/example.json
|
59
|
+
- spec/fixtures/translink-example.cif
|
60
|
+
- spec/integration/translink_spec.rb
|
59
61
|
- spec/spec_helper.rb
|
62
|
+
- spec/unit/header_spec.rb
|
60
63
|
homepage: http://github.com/davidjrice/atco
|
61
64
|
licenses:
|
62
65
|
- MIT
|
File without changes
|