amtrak 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +5 -0
  3. data/README.markdown +184 -2
  4. data/amtrak.gemspec +3 -4
  5. data/lib/amtrak.rb +2 -3
  6. data/lib/amtrak/train_fetcher.rb +25 -28
  7. data/lib/amtrak/train_parser.rb +30 -86
  8. data/lib/amtrak/version.rb +1 -1
  9. data/spec/amtrak/train_fetcher_spec.rb +26 -22
  10. data/spec/amtrak/train_parser_spec.rb +8 -64
  11. data/spec/amtrak_spec.rb +1 -2
  12. data/spec/fixtures/json/_get.json +223 -28
  13. data/spec/fixtures/json/_parse.json +211 -42
  14. data/spec/fixtures/json/pvd_to_bby.json +1131 -0
  15. data/spec/fixtures/vcr/Amtrak/_get/returns_a_list_of_train_times.yml +1160 -1250
  16. data/spec/fixtures/vcr/Amtrak_TrainFetcher/_get/does_the_same_as_get.yml +1159 -1628
  17. data/spec/fixtures/vcr/Amtrak_TrainFetcher/_get/given_a_valid_date_and_invalid_train_stations/does_not_include_various_keys_and_includes_an_error.yml +66 -0
  18. data/spec/fixtures/vcr/Amtrak_TrainFetcher/_get/given_a_valid_date_and_train_stations/includes_various_keys.yml +1181 -0
  19. data/spec/fixtures/vcr/Amtrak_TrainFetcher/_get/given_an_invalid_date_and_valid_train_stations/does_not_include_various_keys_and_includes_an_error.yml +66 -0
  20. metadata +22 -54
  21. data/lib/amtrak/train_fetcher/main_page.rb +0 -83
  22. data/lib/amtrak/train_fetcher/train_page.rb +0 -52
  23. data/spec/amtrak/train_fetcher/main_page_spec.rb +0 -53
  24. data/spec/amtrak/train_fetcher/train_page_spec.rb +0 -18
  25. data/spec/fixtures/html/pvd_to_bby.html +0 -2596
  26. data/spec/fixtures/html/pvd_to_bby_cancelled.html +0 -2671
  27. data/spec/fixtures/json/_parse_cancelled.json +0 -132
  28. data/spec/fixtures/vcr/Amtrak_TrainFetcher/_get/given_a_valid_date_and_invalid_train_stations/does_not_include_various_classes_and_includes_an_error.yml +0 -1073
  29. data/spec/fixtures/vcr/Amtrak_TrainFetcher/_get/given_a_valid_date_and_train_stations/includes_various_classes.yml +0 -1650
  30. data/spec/fixtures/vcr/Amtrak_TrainFetcher/_get/given_an_invalid_date_and_valid_train_stations/does_not_include_various_classes_and_includes_an_error.yml +0 -1071
  31. data/spec/fixtures/vcr/Amtrak_TrainFetcher_MainPage/_session_id/pulls_the_session_id_from_the_cookies.yml +0 -1071
  32. data/spec/fixtures/vcr/Amtrak_TrainFetcher_MainPage/_total_pages/when_more_than_one_page_exists_on_the_website/returns_2.yml +0 -1263
  33. data/spec/fixtures/vcr/Amtrak_TrainFetcher_MainPage/_total_pages/when_only_one_page_exists_on_the_website/returns_1.yml +0 -1154
  34. data/spec/fixtures/vcr/Amtrak_TrainFetcher_TrainPage/_get/when_it_works/includes_various_classes.yml +0 -198
@@ -1,6 +1,6 @@
1
1
  module Amtrak
2
2
  # This module holds the Amtrak version
3
3
  module Version
4
- VERSION = '2.0.0'
4
+ VERSION = '3.0.0'.freeze
5
5
  end
6
6
  end
@@ -2,57 +2,61 @@ require 'spec_helper'
2
2
 
3
3
  describe Amtrak::TrainFetcher do
4
4
  describe '.get', :vcr do
5
- let(:output) do
6
- described_class.get('pvd', 'bby', date: Date.parse('2015-12-31'))
7
- end
5
+ let(:output) { described_class.get('pvd', 'bby', date: Date.parse('2017-10-12')) }
6
+ let(:segment) { output['journeys'][0]['segments'][0] }
8
7
 
9
8
  it 'does the same as #get' do
10
- expect(output.join).to include('resp_by_citypair_subheading_trainname')
11
- expect(output.join).to include('resp_by_citypair_depart_status_details')
12
- expect(output.join).to include('resp_by_citypair_arrive_status_details')
9
+ expect(segment.keys).to include(
10
+ 'originEstimatedDepartureDateTime',
11
+ 'originScheduledDepartureDateTime',
12
+ 'trainNumber'
13
+ )
13
14
  end
14
15
  end
15
16
 
16
17
  describe '#get', :vcr do
17
18
  describe 'given a valid date and train stations' do
18
19
  subject do
19
- described_class.new('pvd', 'bby', date: Date.parse('2015-12-31'))
20
+ described_class.new('pvd', 'bby', date: Date.parse('2017-10-12'))
20
21
  end
21
22
 
22
23
  let(:output) { subject.get }
23
-
24
- it 'includes various classes' do
25
- expect(output.join).to include('resp_by_citypair_subheading_trainname')
26
- expect(output.join).to include('resp_by_citypair_depart_status_details')
27
- expect(output.join).to include('resp_by_citypair_arrive_status_details')
24
+ let(:segment) { output['journeys'][0]['segments'][0] }
25
+
26
+ it 'includes various keys' do
27
+ expect(segment.keys).to include(
28
+ 'originEstimatedDepartureDateTime',
29
+ 'originScheduledDepartureDateTime',
30
+ 'trainNumber'
31
+ )
28
32
  end
29
33
  end
30
34
 
31
35
  describe 'given an invalid date and valid train stations' do
32
36
  subject do
33
- described_class.new('pvd', 'bby', date: Date.parse('2014-11-06'))
37
+ described_class.new('pvd', 'bby', date: Date.parse('2014-10-12'))
34
38
  end
35
39
 
36
40
  let(:output) { subject.get }
41
+ let(:error) { output['errors'][0] }
37
42
 
38
- it 'does not include various classes and includes an error' do
39
- expect(output.join).to_not include('resp_by_citypair_subheading_trainname')
40
- expect(output.join).to_not include('resp_by_citypair_depart_status_details')
41
- expect(output.join).to_not include('resp_by_citypair_arrive_status_details')
43
+ it 'does not include various keys and includes an error' do
44
+ expect(error['code']).to eq('R602')
45
+ expect(error['text']).to match(/R602/)
42
46
  end
43
47
  end
44
48
 
45
49
  describe 'given a valid date and invalid train stations' do
46
50
  subject do
47
- described_class.new('askdf', 'bby', date: Date.parse('2015-12-31'))
51
+ described_class.new('askdf', 'bby', date: Date.parse('2017-10-12'))
48
52
  end
49
53
 
50
54
  let(:output) { subject.get }
55
+ let(:error) { output['errors'][0] }
51
56
 
52
- it 'does not include various classes and includes an error' do
53
- expect(output.join).to_not include('resp_by_citypair_subheading_trainname')
54
- expect(output.join).to_not include('resp_by_citypair_depart_status_details')
55
- expect(output.join).to_not include('resp_by_citypair_arrive_status_details')
57
+ it 'does not include various keys and includes an error' do
58
+ expect(error['code']).to eq('APPLICATION_ERROR')
59
+ expect(error['text']).to match(/R440/)
56
60
  end
57
61
  end
58
62
  end
@@ -1,52 +1,22 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Amtrak::TrainParser do
4
- describe '#clean_msg' do
5
- subject { described_class.new('') }
6
- let(:output) { subject.clean_msg(message) }
7
-
8
- context 'on a string with no time' do
9
- let(:message) { 'Scheduled arrival' }
10
- let(:expected) { nil }
11
-
12
- it 'returns nil' do
13
- expect(output).to eq(expected)
14
- end
15
- end
16
-
17
- context 'on a string with a time' do
18
- let(:message) { 'Scheduled arrival 3:30 pm' }
19
- let(:expected) { '3:30 pm' }
20
-
21
- it 'returns the time' do
22
- expect(output).to eq(expected)
23
- end
24
- end
25
-
26
- context 'on a string with only a time' do
27
- let(:message) { '3:30 pm' }
28
- let(:expected) { '3:30 pm' }
29
-
30
- it 'returns the time' do
31
- expect(output).to eq(expected)
32
- end
33
- end
34
- end
35
-
36
4
  describe '.parse' do
37
5
  let(:parser) { double(Amtrak::TrainParser) }
6
+
38
7
  it 'calls new and parse' do
39
- expect(described_class).to receive(:new).with('') { parser }
8
+ expect(described_class).to receive(:new).with('journeys' => []) { parser }
40
9
  expect(parser).to receive(:parse)
41
- described_class.parse('')
10
+ described_class.parse('journeys' => [])
42
11
  end
43
12
  end
44
13
 
45
14
  describe '#parse' do
46
- subject { described_class.new(document) }
15
+ subject { described_class.new(json) }
47
16
  let(:output) { subject.parse }
17
+ let(:json) { JSON.parse(document) }
48
18
  let(:document) do
49
- File.read(File.join('spec', 'fixtures', 'html', 'pvd_to_bby.html'))
19
+ File.read(File.join('spec', 'fixtures', 'json', 'pvd_to_bby.json'))
50
20
  end
51
21
  let(:expected) do
52
22
  JSON.parse(
@@ -55,34 +25,8 @@ describe Amtrak::TrainParser do
55
25
  )
56
26
  end
57
27
 
58
- context 'without an error' do
59
- it 'returns a list of train times' do
60
- expect(output).to eq(expected)
61
- end
62
-
63
- context 'with an error' do
64
- it 'raises a TrainParser::Error' do
65
- expect(subject.document).to receive(:search) {
66
- fail Nokogiri::SyntaxError
67
- }
68
- expect { output }.to raise_error(Amtrak::TrainParser::Error)
69
- end
70
- end
71
-
72
- context 'with a cancelled train' do
73
- let(:document) do
74
- File.read(File.join('spec', 'fixtures', 'html', 'pvd_to_bby_cancelled.html'))
75
- end
76
- let(:expected) do
77
- JSON.parse(
78
- File.read(File.join('spec', 'fixtures', 'json', '_parse_cancelled.json')),
79
- symbolize_names: true
80
- )
81
- end
82
- it 'returns a list with some trains marked Cancelled' do
83
- expect(output).to eq(expected)
84
- end
85
- end
28
+ it 'returns a list of train times' do
29
+ expect(output).to eq(expected)
86
30
  end
87
31
  end
88
32
  end
data/spec/amtrak_spec.rb CHANGED
@@ -4,7 +4,7 @@ describe Amtrak do
4
4
  context '.get' do
5
5
  let(:from) { 'pvd' }
6
6
  let(:to) { 'bby' }
7
- let(:date) { Date.parse('Sat, Jan 3, 2016') }
7
+ let(:date) { Date.parse('Thur, Oct 12, 2017') }
8
8
  let(:output) { described_class.get(from, to, date: date) }
9
9
  let(:expected) do
10
10
  JSON.parse(
@@ -14,7 +14,6 @@ describe Amtrak do
14
14
  end
15
15
 
16
16
  it 'returns a list of train times', :vcr do
17
- puts JSON.pretty_generate(output)
18
17
  expect(output).to eq(expected)
19
18
  end
20
19
  end
@@ -1,54 +1,249 @@
1
1
  [
2
2
  {
3
- "number": 194,
3
+ "number": 178,
4
4
  "departure": {
5
- "date": "Sun, Jan 3 2016",
6
- "scheduled_time": "8:25 pm",
7
- "estimated_time": "8:29 pm"
5
+ "date": "2017-10-11",
6
+ "scheduled_time": "23:29:00",
7
+ "estimated_time": "23:29:00"
8
8
  },
9
9
  "arrival": {
10
- "date": "Sun, Jan 3 2016",
11
- "scheduled_time": "9:10 pm",
12
- "estimated_time": "9:00 pm"
10
+ "date": "2017-10-11",
11
+ "scheduled_time": "23:29:00",
12
+ "estimated_time": "23:29:00"
13
13
  }
14
14
  },
15
15
  {
16
- "number": 2256,
16
+ "number": 66,
17
17
  "departure": {
18
- "date": "Sun, Jan 3 2016",
19
- "scheduled_time": "8:55 pm",
20
- "estimated_time": ""
18
+ "date": "2017-10-12",
19
+ "scheduled_time": "06:56:00",
20
+ "estimated_time": "06:56:00"
21
21
  },
22
22
  "arrival": {
23
- "date": "Sun, Jan 3 2016",
24
- "scheduled_time": "9:39 pm",
25
- "estimated_time": ""
23
+ "date": "2017-10-12",
24
+ "scheduled_time": "06:56:00",
25
+ "estimated_time": "06:56:00"
26
26
  }
27
27
  },
28
28
  {
29
- "number": 2258,
29
+ "number": 2190,
30
30
  "departure": {
31
- "date": "Sun, Jan 3 2016",
32
- "scheduled_time": "9:54 pm",
33
- "estimated_time": "9:59 pm"
31
+ "date": "2017-10-12",
32
+ "scheduled_time": "09:20:00",
33
+ "estimated_time": null
34
34
  },
35
35
  "arrival": {
36
- "date": "Sun, Jan 3 2016",
37
- "scheduled_time": "10:40 pm",
38
- "estimated_time": "10:38 pm"
36
+ "date": "2017-10-12",
37
+ "scheduled_time": "09:20:00",
38
+ "estimated_time": null
39
39
  }
40
40
  },
41
41
  {
42
- "number": 132,
42
+ "number": 190,
43
43
  "departure": {
44
- "date": "Sun, Jan 3 2016",
45
- "scheduled_time": "11:02 pm",
46
- "estimated_time": "11:03 pm"
44
+ "date": "2017-10-12",
45
+ "scheduled_time": "10:17:00",
46
+ "estimated_time": null
47
47
  },
48
48
  "arrival": {
49
- "date": "Sun, Jan 3 2016",
50
- "scheduled_time": "11:49 pm",
51
- "estimated_time": "11:35 pm"
49
+ "date": "2017-10-12",
50
+ "scheduled_time": "10:17:00",
51
+ "estimated_time": null
52
+ }
53
+ },
54
+ {
55
+ "number": 2150,
56
+ "departure": {
57
+ "date": "2017-10-12",
58
+ "scheduled_time": "10:53:00",
59
+ "estimated_time": null
60
+ },
61
+ "arrival": {
62
+ "date": "2017-10-12",
63
+ "scheduled_time": "10:53:00",
64
+ "estimated_time": null
65
+ }
66
+ },
67
+ {
68
+ "number": 170,
69
+ "departure": {
70
+ "date": "2017-10-12",
71
+ "scheduled_time": "11:55:00",
72
+ "estimated_time": null
73
+ },
74
+ "arrival": {
75
+ "date": "2017-10-12",
76
+ "scheduled_time": "11:55:00",
77
+ "estimated_time": null
78
+ }
79
+ },
80
+ {
81
+ "number": 2154,
82
+ "departure": {
83
+ "date": "2017-10-12",
84
+ "scheduled_time": "12:56:00",
85
+ "estimated_time": null
86
+ },
87
+ "arrival": {
88
+ "date": "2017-10-12",
89
+ "scheduled_time": "12:56:00",
90
+ "estimated_time": null
91
+ }
92
+ },
93
+ {
94
+ "number": 172,
95
+ "departure": {
96
+ "date": "2017-10-12",
97
+ "scheduled_time": "14:23:00",
98
+ "estimated_time": null
99
+ },
100
+ "arrival": {
101
+ "date": "2017-10-12",
102
+ "scheduled_time": "14:23:00",
103
+ "estimated_time": null
104
+ }
105
+ },
106
+ {
107
+ "number": 2158,
108
+ "departure": {
109
+ "date": "2017-10-12",
110
+ "scheduled_time": "14:59:00",
111
+ "estimated_time": null
112
+ },
113
+ "arrival": {
114
+ "date": "2017-10-12",
115
+ "scheduled_time": "14:59:00",
116
+ "estimated_time": null
117
+ }
118
+ },
119
+ {
120
+ "number": 86,
121
+ "departure": {
122
+ "date": "2017-10-12",
123
+ "scheduled_time": "15:47:00",
124
+ "estimated_time": null
125
+ },
126
+ "arrival": {
127
+ "date": "2017-10-12",
128
+ "scheduled_time": "15:47:00",
129
+ "estimated_time": null
130
+ }
131
+ },
132
+ {
133
+ "number": 2160,
134
+ "departure": {
135
+ "date": "2017-10-12",
136
+ "scheduled_time": "16:02:00",
137
+ "estimated_time": null
138
+ },
139
+ "arrival": {
140
+ "date": "2017-10-12",
141
+ "scheduled_time": "16:02:00",
142
+ "estimated_time": null
143
+ }
144
+ },
145
+ {
146
+ "number": 174,
147
+ "departure": {
148
+ "date": "2017-10-12",
149
+ "scheduled_time": "17:30:00",
150
+ "estimated_time": null
151
+ },
152
+ "arrival": {
153
+ "date": "2017-10-12",
154
+ "scheduled_time": "17:30:00",
155
+ "estimated_time": null
156
+ }
157
+ },
158
+ {
159
+ "number": 2164,
160
+ "departure": {
161
+ "date": "2017-10-12",
162
+ "scheduled_time": "17:51:00",
163
+ "estimated_time": null
164
+ },
165
+ "arrival": {
166
+ "date": "2017-10-12",
167
+ "scheduled_time": "17:51:00",
168
+ "estimated_time": null
169
+ }
170
+ },
171
+ {
172
+ "number": 2166,
173
+ "departure": {
174
+ "date": "2017-10-12",
175
+ "scheduled_time": "18:54:00",
176
+ "estimated_time": null
177
+ },
178
+ "arrival": {
179
+ "date": "2017-10-12",
180
+ "scheduled_time": "18:54:00",
181
+ "estimated_time": null
182
+ }
183
+ },
184
+ {
185
+ "number": 176,
186
+ "departure": {
187
+ "date": "2017-10-12",
188
+ "scheduled_time": "19:19:00",
189
+ "estimated_time": null
190
+ },
191
+ "arrival": {
192
+ "date": "2017-10-12",
193
+ "scheduled_time": "19:19:00",
194
+ "estimated_time": null
195
+ }
196
+ },
197
+ {
198
+ "number": 2168,
199
+ "departure": {
200
+ "date": "2017-10-12",
201
+ "scheduled_time": "19:56:00",
202
+ "estimated_time": null
203
+ },
204
+ "arrival": {
205
+ "date": "2017-10-12",
206
+ "scheduled_time": "19:56:00",
207
+ "estimated_time": null
208
+ }
209
+ },
210
+ {
211
+ "number": 2170,
212
+ "departure": {
213
+ "date": "2017-10-12",
214
+ "scheduled_time": "20:54:00",
215
+ "estimated_time": null
216
+ },
217
+ "arrival": {
218
+ "date": "2017-10-12",
219
+ "scheduled_time": "20:54:00",
220
+ "estimated_time": null
221
+ }
222
+ },
223
+ {
224
+ "number": 94,
225
+ "departure": {
226
+ "date": "2017-10-12",
227
+ "scheduled_time": "21:16:00",
228
+ "estimated_time": null
229
+ },
230
+ "arrival": {
231
+ "date": "2017-10-12",
232
+ "scheduled_time": "21:16:00",
233
+ "estimated_time": null
234
+ }
235
+ },
236
+ {
237
+ "number": 2172,
238
+ "departure": {
239
+ "date": "2017-10-12",
240
+ "scheduled_time": "22:03:00",
241
+ "estimated_time": null
242
+ },
243
+ "arrival": {
244
+ "date": "2017-10-12",
245
+ "scheduled_time": "22:03:00",
246
+ "estimated_time": null
52
247
  }
53
248
  }
54
249
  ]