mvg-live 2.2.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG.md +32 -0
- data/README.md +51 -46
- data/bin/mvg +8 -1
- data/bin/mvg_json +8 -1
- data/lib/mvg/live/cli.rb +30 -6
- data/lib/mvg/live/http.rb +93 -20
- data/lib/mvg/live/parser.rb +15 -48
- data/lib/mvg/live/version.rb +2 -1
- data/lib/mvg/live.rb +73 -31
- data/spec/fixtures/vcr_cassettes/MVG/Live/CLI/test_0002_resolves_the_hacked_station_names_in_CLI_mode.json +139 -0
- data/spec/fixtures/vcr_cassettes/MVG/Live/CLI/test_0005_resolves_the_hacked_station_names_in_CLI_mode.json +139 -0
- data/spec/fixtures/vcr_cassettes/MVG/Live/HTTP/test_0004_logs_the_request_when_a_logger_is_given.json +139 -0
- data/spec/fixtures/vcr_cassettes/MVG/Live/HTTP/test_0004_should_provide_the_response_object_after_retrieving.json +139 -0
- data/spec/fixtures/vcr_cassettes/MVG/Live/HTTP/test_0005_should_provide_the_response_object_after_retrieving.json +139 -0
- data/spec/fixtures/vcr_cassettes/MVG/Live/HTTP/test_0006_should_provide_the_response_object_after_retrieving.json +139 -0
- data/spec/fixtures/vcr_cassettes/MVG/Live/fetching_departures/test_0001_returns_a_usable_data_structure.json +139 -0
- data/spec/fixtures/vcr_cassettes/MVG/Live/fetching_departures/test_0002_returns_valid_UTF-8_destinations.json +139 -0
- data/spec/fixtures/vcr_cassettes/MVG/Live/fetching_departures/test_0003_sorts_the_sorted_result_by_departure_time.json +139 -0
- data/spec/fixtures/vcr_cassettes/MVG/Live/fetching_departures/test_0004_exposes_the_server_time.json +139 -0
- data/spec/fixtures/vcr_cassettes/MVG/Live/fetching_departures/test_0005_restricts_the_request_to_the_selected_transports.json +139 -0
- data/spec/fixtures/vcr_cassettes/MVG/Live/station_lookup/test_0001_suggests_alternatives_for_unknown_stations.json +72 -0
- data/spec/fixtures/vcr_cassettes/MVG/Live/station_lookup/test_0002_matches_station_names_case-insensitively.json +139 -0
- data/spec/fixtures/vcr_cassettes/MVG/Live/station_lookup/test_0003_strips_station_additions_in_parentheses.json +139 -0
- data/spec/fixtures/vcr_cassettes/MVG/Live/transport_filtering/test_0001_only_returns_U-Bahn_departures_when_only_U-Bahn_is_requested.json +139 -0
- data/spec/mvg_live_spec.rb +297 -140
- data/spec/spec_helper.rb +34 -16
- metadata +46 -174
- data/.gitignore +0 -4
- data/.travis.yml +0 -9
- data/Dockerfile +0 -13
- data/Gemfile +0 -4
- data/Gemfile.lock +0 -87
- data/Rakefile +0 -8
- data/fixtures/vcr_cassettes/MVG_Live/HTTP/should_build_the_right_query.json +0 -1
- data/fixtures/vcr_cassettes/MVG_Live/HTTP/should_provide_cached_access_to_the_response_object_through_response_obj.json +0 -1
- data/fixtures/vcr_cassettes/MVG_Live/Parser/unknown_station/should_inform_about_incorrent_stations_and_possible_fits.json +0 -1
- data/fixtures/vcr_cassettes/MVG_Live/Parser/with_S-Bahn/both/should_have_the_same_number_of_entries.json +0 -1
- data/fixtures/vcr_cassettes/MVG_Live/Parser/with_S-Bahn/display/should_display_the_correct_order.json +0 -1
- data/fixtures/vcr_cassettes/MVG_Live/Parser/with_S-Bahn/display/should_have_the_correct_entry_size.json +0 -1
- data/fixtures/vcr_cassettes/MVG_Live/Parser/with_S-Bahn/display/should_have_the_correct_order.json +0 -1
- data/fixtures/vcr_cassettes/MVG_Live/Parser/with_S-Bahn/sorted/should_have_the_correct_entry_size.json +0 -1
- data/fixtures/vcr_cassettes/MVG_Live/Parser/with_S-Bahn/sorted/should_have_the_correct_order.json +0 -1
- data/fixtures/vcr_cassettes/test_0001_should_apply_convenience_hacks_on_station_names.json +0 -1
- data/fixtures/vcr_cassettes/test_0001_should_apply_some_convenience_hacks_on_station_names.json +0 -1
- data/fixtures/vcr_cassettes/test_0001_should_have_the_correct_entry_size.json +0 -1
- data/fixtures/vcr_cassettes/test_0001_should_have_the_same_number_of_entries.json +0 -1
- data/fixtures/vcr_cassettes/test_0001_should_inform_about_incorrent_stations_and_possible_fits.json +0 -1
- data/fixtures/vcr_cassettes/test_0001_should_parse_the_response_and_return_a_usable_data_structure.json +0 -1
- data/fixtures/vcr_cassettes/test_0002_should_apply_the_following_convenince_hacks.json +0 -1
- data/fixtures/vcr_cassettes/test_0003_should_build_the_right_query.json +0 -1
- data/fixtures/vcr_cassettes/test_0004_should_provide_access_to_the_response_object_through_response_obj.json +0 -1
- data/fixtures/vcr_cassettes/test_0004_should_provide_access_to_the_response_through_response.json +0 -1
- data/fixtures/vcr_cassettes/test_0004_should_provide_cached_access_to_the_response_object_through_response_obj.json +0 -1
- data/lib/mvg/live/encoding.rb +0 -13
- data/mvg-live.gemspec +0 -39
- data/spec/factories.rb +0 -26
data/spec/mvg_live_spec.rb
CHANGED
|
@@ -1,203 +1,360 @@
|
|
|
1
|
-
# encoding: UTF-8
|
|
2
1
|
require 'spec_helper'
|
|
2
|
+
require 'tempfile'
|
|
3
3
|
|
|
4
|
-
describe MVG::Live
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
# # https://github.com/myronmarston/vcr/wiki/Usage-with-MiniTest
|
|
8
|
-
# VCR.insert_cassette __name__
|
|
9
|
-
# end
|
|
10
|
-
#
|
|
11
|
-
# after do
|
|
12
|
-
# VCR.eject_cassette
|
|
13
|
-
# end
|
|
4
|
+
describe MVG::Live do
|
|
5
|
+
BASE_TIME = Time.utc(2030, 1, 1, 12, 0, 0)
|
|
6
|
+
BASE_MS = (BASE_TIME.to_f * 1000).to_i
|
|
14
7
|
|
|
15
8
|
describe 'when initialized' do
|
|
16
9
|
it 'can be created with no arguments' do
|
|
17
|
-
|
|
18
|
-
mvglive.must_be_instance_of MVG::Live
|
|
10
|
+
assert_instance_of MVG::Live, build_live
|
|
19
11
|
end
|
|
20
12
|
|
|
21
13
|
it 'can be created for a specific station' do
|
|
22
|
-
|
|
23
|
-
mvglive.station.must_match 'Westfriedhof'
|
|
14
|
+
assert_match 'Westfriedhof', build_live('Westfriedhof').station
|
|
24
15
|
end
|
|
25
16
|
|
|
26
|
-
it 'can be created for
|
|
27
|
-
transports = [
|
|
28
|
-
|
|
29
|
-
|
|
17
|
+
it 'can be created for specific transports' do
|
|
18
|
+
transports = %i[u bus tram s]
|
|
19
|
+
assert_same transports, build_live('Westfriedhof', transports: transports).transports
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'defaults to all transports' do
|
|
23
|
+
assert_equal %i[u bus tram s], build_live.transports
|
|
30
24
|
end
|
|
31
25
|
end
|
|
32
26
|
|
|
33
27
|
describe 'validations' do
|
|
34
|
-
it 'should not valid without a station' do
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
28
|
+
it 'should not be valid without a station' do
|
|
29
|
+
live = build_live
|
|
30
|
+
live.station = nil
|
|
31
|
+
assert_equal false, live.valid?
|
|
38
32
|
end
|
|
39
33
|
|
|
40
34
|
it 'should be valid with a station and the default transports' do
|
|
41
|
-
|
|
42
|
-
mvglive.valid?.must_equal true
|
|
35
|
+
assert_equal true, build_live('Westfriedhof').valid?
|
|
43
36
|
end
|
|
44
37
|
|
|
45
38
|
it 'should not be valid with invalid transports' do
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
39
|
+
live = build_live('Hauptbahnhof', transports: %i[transrapid express_s_bahn_zum_flughafen])
|
|
40
|
+
assert_equal false, live.valid?
|
|
41
|
+
assert_includes live.errors.attribute_names, :transports
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it 'raises when fetching an invalid instance' do
|
|
45
|
+
assert_raises(ArgumentError) { build_live.fetch }
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
describe 'dependencies' do
|
|
50
|
+
it 'ships without runtime dependencies' do
|
|
51
|
+
spec = Gem::Specification.load('mvg-live.gemspec')
|
|
52
|
+
assert_empty spec.runtime_dependencies
|
|
50
53
|
end
|
|
51
54
|
end
|
|
52
55
|
|
|
53
56
|
describe 'HTTP' do
|
|
54
|
-
it 'should
|
|
55
|
-
|
|
56
|
-
mvglive.connection.must_be_instance_of Faraday::Connection
|
|
57
|
+
it 'should point to the correct host and api version' do
|
|
58
|
+
assert_equal 'https://www.mvg.de/api/bgw-pt/v3', build_live('Westfriedhof').base_url
|
|
57
59
|
end
|
|
58
60
|
|
|
59
|
-
it 'should
|
|
60
|
-
|
|
61
|
-
|
|
61
|
+
it 'should send the api key header' do
|
|
62
|
+
assert_equal MVG::Live::HTTP::API_KEY,
|
|
63
|
+
build_live('Westfriedhof').build_request_headers['x-mvg-authority-check-api-key']
|
|
62
64
|
end
|
|
63
65
|
|
|
64
66
|
it 'should build the right query' do
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
live = build_live('Westfriedhof', transports: %i[u tram])
|
|
68
|
+
live.instance_variable_set(:@global_id, 'de:09162:4')
|
|
69
|
+
assert_equal(
|
|
70
|
+
{
|
|
71
|
+
globalId: 'de:09162:4',
|
|
72
|
+
limit: MVG::Live::HTTP::MAX_DEPARTURES,
|
|
73
|
+
transportTypes: 'UBAHN,TRAM'
|
|
74
|
+
},
|
|
75
|
+
live.build_request_params
|
|
76
|
+
)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it 'logs the request when a logger is given' do
|
|
80
|
+
logger = Class.new do
|
|
81
|
+
attr_reader :lines
|
|
82
|
+
|
|
83
|
+
def initialize
|
|
84
|
+
@lines = []
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def debug(line)
|
|
88
|
+
@lines << line
|
|
89
|
+
end
|
|
90
|
+
end.new
|
|
91
|
+
|
|
92
|
+
build_live('Westfriedhof', logger: logger).retrieve
|
|
93
|
+
refute_empty logger.lines
|
|
94
|
+
assert_match(/\AGET https:\/\/www\.mvg\.de\/api\/bgw-pt\/v3\/locations/, logger.lines.first)
|
|
71
95
|
end
|
|
72
96
|
|
|
73
|
-
it '
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
97
|
+
it 'falls back to the raw body for non-JSON responses' do
|
|
98
|
+
live = build_live('Westfriedhof')
|
|
99
|
+
response = Struct.new(:body).new('<html>Service unavailable</html>')
|
|
100
|
+
assert_equal '<html>Service unavailable</html>', live.send(:parse_body, response)
|
|
77
101
|
end
|
|
78
102
|
|
|
79
|
-
it 'should
|
|
103
|
+
it 'should provide the response object after retrieving' do
|
|
104
|
+
live = build_live('Westfriedhof')
|
|
105
|
+
live.retrieve
|
|
106
|
+
assert_instance_of MVG::Live::HTTP::Response, live.response_obj
|
|
107
|
+
assert_match(/\Ade:\d+:\d+\z/, live.global_id)
|
|
108
|
+
end
|
|
80
109
|
end
|
|
81
110
|
|
|
82
|
-
describe '
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
111
|
+
describe 'station lookup' do
|
|
112
|
+
it 'suggests alternatives for unknown stations' do
|
|
113
|
+
live = build_live('Tor', now: BASE_TIME)
|
|
114
|
+
live.fetch
|
|
115
|
+
assert_equal true, live.station_unknown
|
|
116
|
+
assert_includes live.station_alternates, 'Sendlinger Tor'
|
|
117
|
+
assert_nil live.result_sorted
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
it 'matches station names case-insensitively' do
|
|
121
|
+
live = build_live('hackerbrücke', now: BASE_TIME)
|
|
122
|
+
live.fetch
|
|
123
|
+
assert_equal false, live.station_unknown
|
|
124
|
+
refute_nil live.global_id
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
it 'strips station additions in parentheses' do
|
|
128
|
+
live = build_live('Hauptbahnhof', now: BASE_TIME)
|
|
129
|
+
live.fetch
|
|
130
|
+
assert_equal false, live.station_unknown
|
|
131
|
+
assert_operator live.result_sorted.size, :>, 0
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
describe 'fetching departures' do
|
|
136
|
+
before do
|
|
137
|
+
@live = build_live('Hackerbrücke', transports: %i[tram s], now: BASE_TIME)
|
|
138
|
+
@live.fetch
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
it 'returns a usable data structure' do
|
|
142
|
+
assert_instance_of Array, @live.result_display
|
|
143
|
+
refute_empty @live.result_display
|
|
144
|
+
@live.result_display.each do |entry|
|
|
145
|
+
assert_instance_of String, entry[:line]
|
|
146
|
+
assert_instance_of String, entry[:destination]
|
|
147
|
+
assert_instance_of Integer, entry[:minutes]
|
|
88
148
|
end
|
|
89
149
|
end
|
|
90
150
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
@mvglive.retrieve
|
|
95
|
-
@mvglive.parse
|
|
151
|
+
it 'returns valid UTF-8 destinations' do
|
|
152
|
+
@live.result_display.map { |entry| entry[:destination] }.each do |destination|
|
|
153
|
+
assert destination.valid_encoding?
|
|
96
154
|
end
|
|
155
|
+
end
|
|
97
156
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
end
|
|
157
|
+
it 'sorts the sorted result by departure time' do
|
|
158
|
+
minutes = @live.result_sorted.map { |entry| entry[:minutes] }
|
|
159
|
+
assert_equal minutes.sort, minutes
|
|
160
|
+
end
|
|
103
161
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
end.must_equal [
|
|
108
|
-
['17', "Schwanseestraße", 0],
|
|
109
|
-
['17', "Amalienburgstraße", 0],
|
|
110
|
-
['16', 'St. Emmeram', 3],
|
|
111
|
-
['16', 'Romanplatz', 7],
|
|
112
|
-
['17', "Schwanseestraße", 7],
|
|
113
|
-
['17', "Amalienburgstraße", 11],
|
|
114
|
-
['16', 'St. Emmeram', 13],
|
|
115
|
-
['S4', 'Grafrath', 0],
|
|
116
|
-
['S1', "München Flughafen Terminal", 2],
|
|
117
|
-
['S7', "Kreuzstraße", 3],
|
|
118
|
-
['S8', 'Herrsching', 4],
|
|
119
|
-
['S6', "München Ost", 6],
|
|
120
|
-
['S2', 'Erding', 7],
|
|
121
|
-
['S2', 'Petershausen(Obb)', 8],
|
|
122
|
-
['S8', "München Flughafen Terminal", 10],
|
|
123
|
-
['S6', 'Starnberg', 10],
|
|
124
|
-
['S7', "Höllriegelskreuth", 12],
|
|
125
|
-
['S1', "München Ost", 13],
|
|
126
|
-
['S1', "München Ost", 13],
|
|
127
|
-
['S4', 'Grafing Bahnhof', 15]
|
|
128
|
-
]
|
|
129
|
-
end
|
|
130
|
-
end
|
|
162
|
+
it 'exposes the server time' do
|
|
163
|
+
assert_match(/\A\d{2}:\d{2}\z/, @live.server_time)
|
|
164
|
+
end
|
|
131
165
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
166
|
+
it 'restricts the request to the selected transports' do
|
|
167
|
+
lines = @live.result_display.map { |entry| entry[:line] }
|
|
168
|
+
refute_empty lines
|
|
169
|
+
lines.each { |line| refute_match(/\AU\d/, line) }
|
|
170
|
+
end
|
|
171
|
+
end
|
|
137
172
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
['S8', "München Flughafen Terminal", 4],
|
|
148
|
-
['S7', 'Wolfratshausen', 6],
|
|
149
|
-
['16', 'St. Emmeram', 7],
|
|
150
|
-
['S1', "München Ost", 7],
|
|
151
|
-
['S1', "München Ost", 7],
|
|
152
|
-
['S4', 'Grafing Bahnhof', 9],
|
|
153
|
-
['16', 'Romanplatz', 10],
|
|
154
|
-
['S3', 'Mammendorf', 10],
|
|
155
|
-
['17', "Schwanseestraße", 12],
|
|
156
|
-
['S3', 'Holzkirchen', 12],
|
|
157
|
-
['S4', 'Geltendorf', 14],
|
|
158
|
-
['17', "Amalienburgstraße", 15],
|
|
159
|
-
['S1', "München Flughafen Terminal", 16]
|
|
160
|
-
]
|
|
161
|
-
end
|
|
162
|
-
end
|
|
173
|
+
describe 'transport filtering' do
|
|
174
|
+
it 'only returns U-Bahn departures when only U-Bahn is requested' do
|
|
175
|
+
live = build_live('Westfriedhof', transports: [:u], now: BASE_TIME)
|
|
176
|
+
live.fetch
|
|
177
|
+
lines = live.result_display.map { |entry| entry[:line] }
|
|
178
|
+
refute_empty lines
|
|
179
|
+
lines.each { |line| assert_match(/\AU\d/, line) }
|
|
180
|
+
end
|
|
181
|
+
end
|
|
163
182
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
183
|
+
describe 'Parser' do
|
|
184
|
+
before do
|
|
185
|
+
@live = build_live('Hackerbrücke', now: BASE_TIME)
|
|
186
|
+
stub_response(@live, [
|
|
187
|
+
{ 'label' => '17', 'destination' => 'Schwanseestraße', 'realtimeDepartureTime' => BASE_MS },
|
|
188
|
+
{ 'label' => 'S8', 'destination' => 'Herrsching', 'realtimeDepartureTime' => BASE_MS + 240_000 },
|
|
189
|
+
{ 'label' => '16', 'destination' => 'St. Emmeram', 'plannedDepartureTime' => BASE_MS + 120_000 },
|
|
190
|
+
{ 'label' => 'N17', 'destination' => 'Effnerplatz', 'realtimeDepartureTime' => BASE_MS - 60_000 },
|
|
191
|
+
{ 'label' => 'S1', 'destination' => 'Flughafen', 'realtimeDepartureTime' => BASE_MS + 600_000, 'cancelled' => true }
|
|
192
|
+
])
|
|
193
|
+
@live.parse
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
it 'keeps the order of the response for display' do
|
|
197
|
+
assert_equal(
|
|
198
|
+
[
|
|
199
|
+
['17', 'Schwanseestraße', 0],
|
|
200
|
+
['S8', 'Herrsching', 4],
|
|
201
|
+
['16', 'St. Emmeram', 2],
|
|
202
|
+
['N17', 'Effnerplatz', 0]
|
|
203
|
+
],
|
|
204
|
+
@live.result_display.map { |entry| [entry[:line], entry[:destination], entry[:minutes]] }
|
|
205
|
+
)
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
it 'sorts by minutes, line and destination' do
|
|
209
|
+
assert_equal(
|
|
210
|
+
[['17', 0], ['N17', 0], ['16', 2], ['S8', 4]],
|
|
211
|
+
@live.result_sorted.map { |entry| [entry[:line], entry[:minutes]] }
|
|
212
|
+
)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
it 'skips cancelled departures' do
|
|
216
|
+
refute_includes @live.result_display.map { |entry| entry[:line] }, 'S1'
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
it 'drops departures beyond the default 60 minutes' do
|
|
220
|
+
@live.instance_variable_set(:@response_obj, MVG::Live::HTTP::Response.new(200, [
|
|
221
|
+
{ 'label' => '16', 'destination' => 'St. Emmeram', 'realtimeDepartureTime' => BASE_MS + 59 * 60_000 },
|
|
222
|
+
{ 'label' => '17', 'destination' => 'Schwanseestraße', 'realtimeDepartureTime' => BASE_MS + 61 * 60_000 }
|
|
223
|
+
]))
|
|
224
|
+
@live.parse
|
|
225
|
+
assert_equal ['16'], @live.result_display.map { |entry| entry[:line] }
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
it 'keeps all departures with all: true' do
|
|
229
|
+
live = build_live('Hackerbrücke', now: BASE_TIME, all: true)
|
|
230
|
+
stub_response(live, [
|
|
231
|
+
{ 'label' => '16', 'destination' => 'St. Emmeram', 'realtimeDepartureTime' => BASE_MS + 59 * 60_000 },
|
|
232
|
+
{ 'label' => '17', 'destination' => 'Schwanseestraße', 'realtimeDepartureTime' => BASE_MS + 120 * 60_000 }
|
|
233
|
+
])
|
|
234
|
+
live.parse
|
|
235
|
+
assert_equal %w[16 17], live.result_display.map { |entry| entry[:line] }
|
|
236
|
+
assert_nil live.max_minutes
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
it 'honours a custom max_minutes' do
|
|
240
|
+
live = build_live('Hackerbrücke', now: BASE_TIME, max_minutes: 10)
|
|
241
|
+
stub_response(live, [
|
|
242
|
+
{ 'label' => '16', 'destination' => 'St. Emmeram', 'realtimeDepartureTime' => BASE_MS + 5 * 60_000 },
|
|
243
|
+
{ 'label' => '17', 'destination' => 'Schwanseestraße', 'realtimeDepartureTime' => BASE_MS + 11 * 60_000 }
|
|
244
|
+
])
|
|
245
|
+
live.parse
|
|
246
|
+
assert_equal ['16'], live.result_display.map { |entry| entry[:line] }
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
it 'handles error objects instead of a departure array' do
|
|
250
|
+
stub_response(@live, { 'status' => 404, 'error' => 'Not Found' })
|
|
251
|
+
assert_equal [], @live.parse
|
|
169
252
|
end
|
|
170
253
|
end
|
|
171
254
|
|
|
172
255
|
describe 'CLI' do
|
|
173
|
-
it '
|
|
174
|
-
MVG::Live.
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
MVG::Live.
|
|
181
|
-
|
|
182
|
-
end
|
|
183
|
-
|
|
184
|
-
it '
|
|
256
|
+
it 'parses the --all flag and keeps the station intact' do
|
|
257
|
+
parsed = MVG::Live::CLI.parse_argv(['--all', 'Hauptbahnhof'])
|
|
258
|
+
assert_equal true, parsed[:all]
|
|
259
|
+
assert_equal 'Hauptbahnhof', parsed[:station]
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
it 'parses --help' do
|
|
263
|
+
assert_equal true, MVG::Live::CLI.parse_argv(['--help'])[:help]
|
|
264
|
+
assert_equal true, MVG::Live::CLI.parse_argv(['-h'])[:help]
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
it 'documents the default limit in the usage text' do
|
|
268
|
+
assert_includes MVG::Live::CLI.usage, MVG::Live::DEFAULT_MAX_MINUTES.to_s
|
|
269
|
+
end
|
|
270
|
+
it 'should apply the following convenience hacks' do
|
|
185
271
|
hacks = [
|
|
186
272
|
['Stachus', 'Karlsplatz (Stachus)'],
|
|
187
273
|
['Karlsplatz', 'Karlsplatz (Stachus)'],
|
|
188
|
-
['Moosach Bf', 'Moosach
|
|
189
|
-
['Moosach
|
|
274
|
+
['Moosach Bf', 'Moosach'],
|
|
275
|
+
['Moosach Bf.', 'Moosach'],
|
|
276
|
+
['Moosach Bahnhof', 'Moosach']
|
|
190
277
|
]
|
|
191
278
|
|
|
192
279
|
hacks.each do |hack|
|
|
193
|
-
|
|
194
|
-
mvglive.cli_station_hacks_for(hack[0]).must_equal hack[1]
|
|
280
|
+
assert_equal hack[1], build_live.cli_station_hacks_for(hack[0])
|
|
195
281
|
end
|
|
196
282
|
end
|
|
283
|
+
|
|
284
|
+
it 'resolves the hacked station names in CLI mode' do
|
|
285
|
+
live = build_live('Moosach Bf', transports: [:u], cli: true)
|
|
286
|
+
live.fetch
|
|
287
|
+
assert_equal false, live.station_unknown
|
|
288
|
+
refute_nil live.global_id
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
it 'only applies the hacks in CLI mode' do
|
|
292
|
+
live = build_live('Stachus', cli: true)
|
|
293
|
+
assert_equal 'Karlsplatz (Stachus)', live.send(:station_to_mvg, 'Stachus')
|
|
294
|
+
assert_equal 'Stachus', build_live('Stachus').send(:station_to_mvg, 'Stachus')
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
it 'loads the default station and transports from MVG_FILE' do
|
|
298
|
+
file = Tempfile.new('mvg')
|
|
299
|
+
file.write(JSON.generate('default_transports' => ['u'], 'default_station' => 'Hauptbahnhof'))
|
|
300
|
+
file.flush
|
|
301
|
+
|
|
302
|
+
ENV['MVG_FILE'] = file.path
|
|
303
|
+
live = build_live(nil, transports: [], load_user_defaults: true)
|
|
304
|
+
assert_equal 'Hauptbahnhof', live.station
|
|
305
|
+
assert_equal [:u], live.transports
|
|
306
|
+
assert_equal file.path, live.using_config_file
|
|
307
|
+
ensure
|
|
308
|
+
ENV.delete('MVG_FILE')
|
|
309
|
+
file.close!
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
it 'keeps the given station when loading defaults' do
|
|
313
|
+
file = Tempfile.new('mvg')
|
|
314
|
+
file.write(JSON.generate('default_station' => 'Hauptbahnhof'))
|
|
315
|
+
file.flush
|
|
316
|
+
|
|
317
|
+
ENV['MVG_FILE'] = file.path
|
|
318
|
+
assert_equal 'Marienplatz', build_live('Marienplatz', load_user_defaults: true).station
|
|
319
|
+
ensure
|
|
320
|
+
ENV.delete('MVG_FILE')
|
|
321
|
+
file.close!
|
|
322
|
+
end
|
|
197
323
|
end
|
|
198
324
|
|
|
199
325
|
describe 'Output' do
|
|
326
|
+
before do
|
|
327
|
+
@live = build_live('Hackerbrücke', transports: %i[tram])
|
|
328
|
+
@live.instance_variable_set(:@server_time, '12:34')
|
|
329
|
+
@live.instance_variable_set(:@result_sorted, [
|
|
330
|
+
{ line: '16', destination: 'St. Emmeram', minutes: 2 }
|
|
331
|
+
])
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
describe 'display' do
|
|
335
|
+
it 'lists the departures' do
|
|
336
|
+
output = @live.to_display
|
|
337
|
+
assert_includes output, '[ 12:34 ]'
|
|
338
|
+
assert_includes output, '16 | St. Emmeram | 2 Minuten'
|
|
339
|
+
assert_includes output, 'Hackerbrücke: Tram'
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
it 'lists alternatives for unknown stations' do
|
|
343
|
+
live = build_live('Tor', now: BASE_TIME)
|
|
344
|
+
live.instance_variable_set(:@station_unknown, true)
|
|
345
|
+
live.instance_variable_set(:@station_alternates, ['Sendlinger Tor'])
|
|
346
|
+
assert_includes live.to_display, ' - Sendlinger Tor'
|
|
347
|
+
end
|
|
348
|
+
end
|
|
349
|
+
|
|
200
350
|
describe 'JSON' do
|
|
351
|
+
it 'contains the result and meta data' do
|
|
352
|
+
parsed = JSON.parse(@live.to_json, symbolize_names: true)
|
|
353
|
+
assert_equal 'Hackerbrücke', parsed[:station]
|
|
354
|
+
assert_equal '12:34', parsed[:server_time]
|
|
355
|
+
assert_equal 60, parsed[:max_minutes]
|
|
356
|
+
assert_equal [{ line: '16', destination: 'St. Emmeram', minutes: 2 }], parsed[:result_sorted]
|
|
357
|
+
end
|
|
201
358
|
end
|
|
202
359
|
end
|
|
203
360
|
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -1,28 +1,46 @@
|
|
|
1
1
|
require 'rubygems'
|
|
2
|
-
require 'simplecov'
|
|
3
2
|
require 'minitest/spec'
|
|
4
3
|
require 'minitest/autorun'
|
|
5
|
-
require 'minitest-vcr'
|
|
6
|
-
require 'mocha/test_unit'
|
|
7
4
|
require 'vcr'
|
|
8
|
-
require '
|
|
9
|
-
|
|
10
|
-
SimpleCov.start
|
|
11
|
-
SimpleCov.command_name 'test'
|
|
5
|
+
require 'webmock/minitest'
|
|
12
6
|
|
|
13
7
|
require 'mvg/live'
|
|
14
8
|
|
|
15
|
-
|
|
16
|
-
|
|
9
|
+
VCR.configure do |config|
|
|
10
|
+
config.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
|
|
11
|
+
config.hook_into :webmock
|
|
12
|
+
config.default_cassette_options = {
|
|
13
|
+
record: :new_episodes,
|
|
14
|
+
serialize_with: :json
|
|
15
|
+
}
|
|
16
|
+
config.filter_sensitive_data('<MVG_API_KEY>') { MVG::Live::HTTP::API_KEY }
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
# One cassette per test, recorded on demand.
|
|
20
|
+
module CassetteHelper
|
|
21
|
+
def before_setup
|
|
22
|
+
super
|
|
23
|
+
VCR.insert_cassette(cassette_name)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def after_teardown
|
|
27
|
+
VCR.eject_cassette
|
|
28
|
+
super
|
|
29
|
+
end
|
|
20
30
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
c.default_cassette_options = { record: :new_episodes, serialize_with: :json, preserve_exact_body_bytes: true }
|
|
25
|
-
c.debug_logger = STDERR
|
|
31
|
+
def cassette_name
|
|
32
|
+
"#{self.class.name.tr('::', '/')}/#{name}"
|
|
33
|
+
end
|
|
26
34
|
end
|
|
27
35
|
|
|
28
|
-
|
|
36
|
+
class Minitest::Spec
|
|
37
|
+
include CassetteHelper
|
|
38
|
+
|
|
39
|
+
def build_live(station = nil, transports: MVG::Live::ALL_TRANSPORTS, **options)
|
|
40
|
+
MVG::Live.new(station, { transports: transports }.merge(options))
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def stub_response(live, body)
|
|
44
|
+
live.instance_variable_set(:@response_obj, MVG::Live::HTTP::Response.new(200, body))
|
|
45
|
+
end
|
|
46
|
+
end
|