ns-yapi 0.5.0 → 0.6.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/.rubocop.yml +19 -0
- data/.ruby-version +1 -1
- data/.travis.yml +5 -2
- data/Gemfile +13 -5
- data/Gemfile.lock +95 -44
- data/lib/model/prices_url.rb +7 -5
- data/lib/ns_client.rb +123 -97
- data/ns.gemspec +16 -14
- data/rakefile.rb +3 -1
- data/spec/model/prices_url_spec.rb +23 -23
- data/spec/ns_client_spec.rb +158 -173
- data/spec/nsyapi_spec.rb +17 -22
- data/spec/spec_helper.rb +13 -18
- metadata +9 -8
data/ns.gemspec
CHANGED
@@ -1,23 +1,25 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
5
|
|
5
6
|
Gem::Specification.new do |gem|
|
6
|
-
gem.name =
|
7
|
-
gem.
|
8
|
-
gem.
|
9
|
-
gem.
|
10
|
-
gem.
|
11
|
-
gem.
|
12
|
-
gem.
|
7
|
+
gem.name = 'ns-yapi'
|
8
|
+
gem.license = 'MIT'
|
9
|
+
gem.version = '0.6.0'
|
10
|
+
gem.authors = ['Stefan Hendriks', 'Derek Kraan', 'Bob Forma']
|
11
|
+
gem.email = ['info@zilverline.com']
|
12
|
+
gem.description = 'Yet Another (Ruby) NS API client'
|
13
|
+
gem.summary = 'A Ruby client for the NS (Dutch Railways) API'
|
14
|
+
gem.homepage = 'https://github.com/zilverline/ns-api'
|
13
15
|
|
14
|
-
gem.files = `git ls-files`.split(
|
15
|
-
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
16
|
+
gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
16
18
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
|
-
gem.require_paths = [
|
19
|
+
gem.require_paths = ['lib']
|
18
20
|
|
19
21
|
gem.add_dependency 'addressable'
|
20
|
-
gem.add_dependency 'httpclient'
|
21
|
-
gem.add_dependency 'nori'
|
22
22
|
gem.add_dependency 'nokogiri'
|
23
|
+
gem.add_dependency 'nori'
|
24
|
+
gem.add_dependency 'rest-client'
|
23
25
|
end
|
data/rakefile.rb
CHANGED
@@ -1,45 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe PricesUrl do
|
4
|
-
|
5
|
-
|
6
|
-
PricesUrl.new("www.somehost.com")
|
6
|
+
it 'takes a fixed url as constructor argument' do
|
7
|
+
PricesUrl.new('www.somehost.com')
|
7
8
|
end
|
8
9
|
|
9
|
-
it
|
10
|
-
expect { PricesUrl.new(nil) }.to raise_error(PricesUrl::InvalidURL,
|
10
|
+
it 'raises an error when no url is given' do
|
11
|
+
expect { PricesUrl.new(nil) }.to raise_error(PricesUrl::InvalidURL, 'You must give an url, ie http://www.ns.nl/api')
|
11
12
|
end
|
12
13
|
|
13
|
-
let(:prices_url) { PricesUrl.new(
|
14
|
+
let(:prices_url) { PricesUrl.new('hostname') }
|
14
15
|
|
15
|
-
it
|
16
|
-
prices_url.url(date: Date.new(2013, 7, 12)).
|
16
|
+
it 'uses given date' do
|
17
|
+
expect(prices_url.url(date: Date.new(2013, 7, 12))).to eq('hostname?date=12072013')
|
17
18
|
end
|
18
19
|
|
19
|
-
it
|
20
|
-
expected_from =
|
21
|
-
prices_url.url(from:
|
20
|
+
it 'uses from' do
|
21
|
+
expected_from = 'Amsterdam'
|
22
|
+
expect(prices_url.url(from: 'Amsterdam')).to eq("hostname?from=#{expected_from}")
|
22
23
|
end
|
23
24
|
|
24
|
-
it
|
25
|
-
expected_to =
|
26
|
-
prices_url.url(to:
|
25
|
+
it 'uses to' do
|
26
|
+
expected_to = 'Purmerend'
|
27
|
+
expect(prices_url.url(to: 'Purmerend')).to eq("hostname?to=#{expected_to}")
|
27
28
|
end
|
28
29
|
|
29
|
-
it
|
30
|
-
expected_via =
|
31
|
-
prices_url.url(via:
|
30
|
+
it 'uses via' do
|
31
|
+
expected_via = 'Zaandam'
|
32
|
+
expect(prices_url.url(via: 'Zaandam')).to eq("hostname?via=#{expected_via}")
|
32
33
|
end
|
33
34
|
|
34
|
-
it
|
35
|
-
prices_url.url(from:
|
35
|
+
it 'uses any all variables for from/to/via' do
|
36
|
+
expect(prices_url.url(from: 'Purmerend', to: 'Amsterdam', via: 'Zaandam')).to eq('hostname?from=Purmerend&to=Amsterdam&via=Zaandam')
|
36
37
|
end
|
37
38
|
|
38
|
-
it
|
39
|
-
prices_url.url(from:
|
39
|
+
it 'html encodes' do
|
40
|
+
expect(prices_url.url(from: 'Purmerend', to: 'Amsterdam Centraal')).to eq('hostname?from=Purmerend&to=Amsterdam%20Centraal')
|
40
41
|
end
|
41
42
|
|
42
43
|
ANY_DATE = Date.new(2013, 8, 13)
|
43
|
-
ANY_DATE_STR =
|
44
|
-
|
44
|
+
ANY_DATE_STR = '13082013'
|
45
45
|
end
|
data/spec/ns_client_spec.rb
CHANGED
@@ -1,265 +1,250 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require 'spec_helper'
|
3
4
|
require 'yaml'
|
4
5
|
|
5
6
|
describe NSClient do
|
7
|
+
let!(:client) { NSClient.new('username', 'password') }
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
context "Stations" do
|
10
|
-
|
11
|
-
context "with valid xml" do
|
12
|
-
|
9
|
+
context 'Stations' do
|
10
|
+
context 'with valid xml' do
|
13
11
|
before :each do
|
14
|
-
stub_ns_client_request
|
12
|
+
stub_ns_client_request 'https://webservices.ns.nl/ns-api-stations-v2', load_fixture('stations.xml')
|
15
13
|
end
|
16
14
|
|
17
|
-
it
|
15
|
+
it 'should return all stations' do
|
18
16
|
stations = client.stations
|
19
|
-
stations.size.
|
17
|
+
expect(stations.size).to eq(620)
|
20
18
|
end
|
21
19
|
|
22
|
-
it
|
20
|
+
it 'should return expected first station from list' do
|
23
21
|
stations = client.stations
|
24
22
|
first_station = stations.first
|
25
|
-
first_station.class.
|
26
|
-
first_station.type.
|
27
|
-
first_station.code.
|
28
|
-
first_station.short_name.
|
29
|
-
first_station.name.
|
30
|
-
first_station.long_name.
|
31
|
-
first_station.country.
|
32
|
-
first_station.uiccode.
|
33
|
-
first_station.lat.
|
34
|
-
first_station.long.
|
23
|
+
expect(first_station.class).to eq(NSClient::Station)
|
24
|
+
expect(first_station.type).to eq('knooppuntIntercitystation')
|
25
|
+
expect(first_station.code).to eq('HT')
|
26
|
+
expect(first_station.short_name).to eq("H'bosch")
|
27
|
+
expect(first_station.name).to eq("'s-Hertogenbosch")
|
28
|
+
expect(first_station.long_name).to eq("'s-Hertogenbosch")
|
29
|
+
expect(first_station.country).to eq('NL')
|
30
|
+
expect(first_station.uiccode).to eq('8400319')
|
31
|
+
expect(first_station.lat).to eq('51.69048')
|
32
|
+
expect(first_station.long).to eq('5.29362')
|
35
33
|
end
|
36
34
|
|
37
|
-
it
|
35
|
+
it 'should retrieve a convenient hash with usable station names and codes for prices usage' do
|
38
36
|
stations = client.stations_short
|
39
|
-
stations.size.
|
40
|
-
stations[
|
37
|
+
expect(stations.size).to eq(620)
|
38
|
+
expect(stations['HT']).to eq(["'s-Hertogenbosch", 'NL'])
|
41
39
|
end
|
42
|
-
|
43
|
-
|
44
40
|
end
|
45
41
|
|
46
|
-
describe
|
47
|
-
|
42
|
+
describe 'integration specs', :integration do
|
48
43
|
# requires a credentials.yml in spec/fixtures with a username and password
|
49
|
-
it
|
50
|
-
found_error = false
|
44
|
+
it 'should parse live data correctly', focus: true do
|
51
45
|
WebMock.allow_net_connect!
|
52
|
-
#while (!found_error) do
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
46
|
+
# while (!found_error) do
|
47
|
+
credentials = YAML.load_file(File.join(File.dirname(__FILE__), 'fixtures/credentials.yml'))
|
48
|
+
client = NSClient.new(credentials['username'], credentials['password'])
|
49
|
+
stations = client.stations
|
50
|
+
station = stations.find { |s| s.code == 'OETZ' }
|
51
|
+
expected_count = 628
|
52
|
+
found_error = !(
|
53
|
+
station.code == 'OETZ' &&
|
54
|
+
station.country == 'A' &&
|
55
|
+
station.name == 'Ötztal' &&
|
56
|
+
stations.count == expected_count
|
57
|
+
)
|
58
|
+
|
59
|
+
if found_error
|
60
|
+
f = File.open('/tmp/ns_stations_without_oztal.xml', 'w')
|
61
|
+
f.write(client.last_received_raw_xml)
|
62
|
+
f.close
|
63
|
+
raise "Could not find staiton with code 'OETZ', see /tmp/ns_stations_without_oztal.xml" if station.blank?
|
64
|
+
|
65
|
+
raise "Found station, but with different properties or size differs? Country should be 'A' but is #{station.country}, station name should be 'Ötztal' but is #{station.name}, and the count should be #{expected_count}. (count is #{stations.count}) see /tmp/ns_stations_without_oztal.xml"
|
66
|
+
else
|
67
|
+
p "Test went OK, #{stations.count} stations found"
|
68
|
+
end
|
69
|
+
# end
|
70
70
|
# remove the loop to constantly check NS if we are doubting their source
|
71
71
|
end
|
72
|
-
|
73
72
|
end
|
74
|
-
|
75
73
|
end
|
76
74
|
|
77
|
-
describe
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
it "should return all stations" do
|
82
|
-
stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-stations-v2", load_fixture('stations_list_with_invalid_new_lines.xml')
|
75
|
+
describe 'invalid stations xml' do
|
76
|
+
context 'with only newlines/spaces we can fix' do
|
77
|
+
it 'should return all stations' do
|
78
|
+
stub_ns_client_request 'https://webservices.ns.nl/ns-api-stations-v2', load_fixture('stations_list_with_invalid_new_lines.xml')
|
83
79
|
stations = client.stations
|
84
|
-
stations.size.
|
80
|
+
expect(stations.size).to eq(2)
|
85
81
|
end
|
86
82
|
|
87
83
|
{
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
84
|
+
'< Code>' => '<Code>',
|
85
|
+
'< Code >' => '<Code>',
|
86
|
+
'< Code >' => '<Code>',
|
87
|
+
'< Code>' => '<Code>',
|
88
|
+
'</ Code>' => '</Code>',
|
89
|
+
'< / Code>' => '</Code>',
|
90
|
+
'</ Code >' => '</Code>',
|
91
|
+
'</ Code >' => '</Code>',
|
92
|
+
"</\n\nCode >" => '</Code>',
|
93
|
+
"</\r\tCode\n>" => '</Code>',
|
94
|
+
"</Co\nde\n>" => '</Code>'
|
99
95
|
}.each do |k, v|
|
100
96
|
it "removes unwanted whitespace from #{k} , expecting #{v} (remove_unwanted_whitespace)" do
|
101
|
-
client.remove_unwanted_whitespace(k).
|
97
|
+
expect(client.remove_unwanted_whitespace(k)).to eq v
|
102
98
|
end
|
103
99
|
end
|
104
|
-
|
105
100
|
end
|
106
101
|
|
107
102
|
context "with mangled XML we cannot / won't fix" do
|
108
|
-
|
109
|
-
|
110
|
-
stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-stations-v2", load_fixture('stations_list_mangled.xml')
|
103
|
+
it 'raises an error when xml is unparseable' do
|
104
|
+
stub_ns_client_request 'https://webservices.ns.nl/ns-api-stations-v2', load_fixture('stations_list_mangled.xml')
|
111
105
|
expect { client.stations }.to raise_error(NSClient::UnparseableXMLError)
|
112
106
|
end
|
113
|
-
|
114
107
|
end
|
115
|
-
|
116
108
|
end
|
117
109
|
|
118
|
-
context
|
119
|
-
|
120
|
-
|
121
|
-
stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-storingen?actual=true", load_fixture('disruptions.xml')
|
110
|
+
context 'Disruptions' do
|
111
|
+
it 'should retrieve planned and unplanned disruptions' do
|
112
|
+
stub_ns_client_request 'https://webservices.ns.nl/ns-api-storingen?actual=true', load_fixture('disruptions.xml')
|
122
113
|
disruptions = client.disruptions
|
123
|
-
disruptions.size.
|
124
|
-
disruptions[:planned].size.
|
125
|
-
disruptions[:unplanned].size.
|
114
|
+
expect(disruptions.size).to eq(2)
|
115
|
+
expect(disruptions[:planned].size).to eq(1)
|
116
|
+
expect(disruptions[:unplanned].size).to eq(1)
|
126
117
|
end
|
127
118
|
|
128
|
-
it
|
129
|
-
stub_ns_client_request
|
119
|
+
it 'should retrieve expected planned disruption' do
|
120
|
+
stub_ns_client_request 'https://webservices.ns.nl/ns-api-storingen?actual=true', load_fixture('disruptions.xml')
|
130
121
|
disruptions = client.disruptions
|
131
|
-
disruptions.size.
|
122
|
+
expect(disruptions.size).to eq(2)
|
132
123
|
planned_disruption = disruptions[:planned].first
|
133
|
-
planned_disruption.class.
|
124
|
+
expect(planned_disruption.class).to eq(NSClient::PlannedDisruption)
|
134
125
|
|
135
|
-
planned_disruption.id.
|
136
|
-
planned_disruption.trip.
|
137
|
-
planned_disruption.reason.
|
138
|
-
planned_disruption.advice.
|
126
|
+
expect(planned_disruption.id).to eq('2010_almo_wp_18_19dec')
|
127
|
+
expect(planned_disruption.trip).to eq('Almere Oostvaarders-Weesp/Naarden-Bussum')
|
128
|
+
expect(planned_disruption.reason).to eq('Beperkt treinverkeer, businzet en/of omreizen, extra reistijd 15-30 min.')
|
129
|
+
expect(planned_disruption.advice).to eq("Maak gebruik van de overige treinen of de bussen: reis tussen Weesp en Almere Centrum met de NS-bus in
|
139
130
|
plaats van de trein tussen Almere Centrum en Lelystad Centrum rijden vier Sprinters per uur reis tussen Almere
|
140
|
-
Muziekwijk en Naarden-Bussum via Weesp"
|
141
|
-
planned_disruption.message.
|
142
|
-
planned_disruption.cause.
|
131
|
+
Muziekwijk en Naarden-Bussum via Weesp")
|
132
|
+
expect(planned_disruption.message).to eq('Test message')
|
133
|
+
expect(planned_disruption.cause).to eq('oorzaak')
|
143
134
|
end
|
144
135
|
|
145
|
-
it
|
146
|
-
stub_ns_client_request
|
136
|
+
it 'should retrieve expected unplanned disruption' do
|
137
|
+
stub_ns_client_request 'https://webservices.ns.nl/ns-api-storingen?actual=true', load_fixture('disruptions.xml')
|
147
138
|
disruptions = client.disruptions
|
148
|
-
disruptions.size.
|
139
|
+
expect(disruptions.size).to eq(2)
|
149
140
|
unplanned_disruption = disruptions[:unplanned].first
|
150
|
-
unplanned_disruption.class.
|
141
|
+
expect(unplanned_disruption.class).to eq(NSClient::UnplannedDisruption)
|
151
142
|
|
152
|
-
unplanned_disruption.id.
|
153
|
-
unplanned_disruption.trip.
|
154
|
-
unplanned_disruption.reason.
|
155
|
-
unplanned_disruption.cause.
|
156
|
-
unplanned_disruption.message.
|
157
|
-
unplanned_disruption.datetime_string ==
|
143
|
+
expect(unplanned_disruption.id).to eq('prio-13345')
|
144
|
+
expect(unplanned_disruption.trip).to eq("'s-Hertogenbosch-Nijmegen")
|
145
|
+
expect(unplanned_disruption.reason).to eq('beperkingen op last van de politie')
|
146
|
+
expect(unplanned_disruption.cause).to eq('oorzaak')
|
147
|
+
expect(unplanned_disruption.message).to eq('Another test message')
|
148
|
+
unplanned_disruption.datetime_string == '2010-12-16T11:16:00+0100' # intentional, give raw data. Let user parse if needed.
|
158
149
|
end
|
159
150
|
|
160
|
-
it
|
161
|
-
stub_ns_client_request
|
151
|
+
it 'should not return disruption when empty in response' do
|
152
|
+
stub_ns_client_request 'https://webservices.ns.nl/ns-api-storingen?actual=true', load_fixture('no_disruptions.xml')
|
162
153
|
disruptions = client.disruptions
|
163
|
-
disruptions.size.
|
164
|
-
disruptions[:planned].size.
|
165
|
-
disruptions[:unplanned].size.
|
166
|
-
end
|
167
|
-
|
168
|
-
describe
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
disruptions
|
174
|
-
disruptions.size.
|
175
|
-
disruptions[:
|
176
|
-
disruptions[:unplanned].size.should == 0
|
154
|
+
expect(disruptions.size).to eq(2)
|
155
|
+
expect(disruptions[:planned].size).to eq(0)
|
156
|
+
expect(disruptions[:unplanned].size).to eq(0)
|
157
|
+
end
|
158
|
+
|
159
|
+
describe 'for a specific station' do
|
160
|
+
it 'should retrieve disruptions for station name' do
|
161
|
+
# ie, for Amsterdam only (https://webservices.ns.nl/ns-api-storingen?station=Amsterdam)
|
162
|
+
stub_ns_client_request 'https://webservices.ns.nl/ns-api-storingen?station=Amsterdam', load_fixture('disruptions_amsterdam.xml')
|
163
|
+
disruptions = client.disruptions 'Amsterdam'
|
164
|
+
expect(disruptions.size).to eq(2)
|
165
|
+
expect(disruptions[:planned].size).to eq(4)
|
166
|
+
expect(disruptions[:unplanned].size).to eq(0)
|
177
167
|
end
|
178
168
|
|
179
|
-
it
|
180
|
-
stub_ns_client_request
|
181
|
-
expect { client.disruptions
|
169
|
+
it 'should raise an error when using invalid station name' do
|
170
|
+
stub_ns_client_request 'https://webservices.ns.nl/ns-api-storingen?station=bla', load_fixture('disruption_invalid_station_name.xml')
|
171
|
+
expect { client.disruptions 'bla' }.to raise_error(NSClient::InvalidStationNameError, "Could not find a station with name 'bla'")
|
182
172
|
end
|
183
173
|
end
|
184
|
-
|
185
|
-
|
186
174
|
end
|
187
175
|
|
188
|
-
context
|
189
|
-
|
190
|
-
|
191
|
-
stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-prijzen-v3?from=Rotterdam&to=Glanerbrug&date=17062013", load_fixture('prices.xml')
|
176
|
+
context 'Prices' do
|
177
|
+
it 'should retrieve prices for a trip' do
|
178
|
+
stub_ns_client_request 'https://webservices.ns.nl/ns-api-prijzen-v3?from=Rotterdam&to=Glanerbrug&date=17062013', load_fixture('prices.xml')
|
192
179
|
date = Date.strptime('17-06-2013', '%d-%m-%Y')
|
193
|
-
response = client.prices from:
|
194
|
-
response.class.
|
195
|
-
response.tariff_units.
|
196
|
-
response.products.size.
|
180
|
+
response = client.prices from: 'Rotterdam', to: 'Glanerbrug', date: date
|
181
|
+
expect(response.class).to eq(NSClient::PricesResponse)
|
182
|
+
expect(response.tariff_units).to eq(205)
|
183
|
+
expect(response.products.size).to eq(2)
|
197
184
|
|
198
|
-
response.enkele_reis.size.
|
199
|
-
response.dagretour.size.
|
185
|
+
expect(response.enkele_reis.size).to eq(6)
|
186
|
+
expect(response.dagretour.size).to eq(6)
|
200
187
|
end
|
201
188
|
|
202
|
-
it
|
203
|
-
stub_ns_client_request
|
189
|
+
it 'should retrieve expected price data' do
|
190
|
+
stub_ns_client_request 'https://webservices.ns.nl/ns-api-prijzen-v3?from=Rotterdam&to=Glanerbrug&date=17062013', load_fixture('prices.xml')
|
204
191
|
date = Date.strptime('17-06-2013', '%d-%m-%Y')
|
205
|
-
response = client.prices from:
|
206
|
-
response.class.
|
207
|
-
response.tariff_units.
|
208
|
-
response.products.size.
|
192
|
+
response = client.prices from: 'Rotterdam', to: 'Glanerbrug', date: date
|
193
|
+
expect(response.class).to eq(NSClient::PricesResponse)
|
194
|
+
expect(response.tariff_units).to eq(205)
|
195
|
+
expect(response.products.size).to eq(2)
|
209
196
|
|
210
|
-
assert_price(response.enkele_reis[0],
|
211
|
-
assert_price(response.enkele_reis[1],
|
212
|
-
assert_price(response.enkele_reis[2],
|
213
|
-
assert_price(response.enkele_reis[3],
|
214
|
-
assert_price(response.enkele_reis[4],
|
215
|
-
assert_price(response.enkele_reis[5],
|
197
|
+
assert_price(response.enkele_reis[0], 'vol tarief', '1', 41.5)
|
198
|
+
assert_price(response.enkele_reis[1], 'reductie_20', '1', 33.2)
|
199
|
+
assert_price(response.enkele_reis[2], 'reductie_40', '1', 24.9)
|
200
|
+
assert_price(response.enkele_reis[3], 'vol tarief', '2', 24.4)
|
201
|
+
assert_price(response.enkele_reis[4], 'reductie_20', '2', 19.5)
|
202
|
+
assert_price(response.enkele_reis[5], 'reductie_40', '2', 14.6)
|
216
203
|
end
|
217
204
|
|
218
|
-
it
|
219
|
-
expect
|
220
|
-
client.prices from: nil, to:
|
221
|
-
|
205
|
+
it 'should raise error when from is not given' do
|
206
|
+
expect do
|
207
|
+
client.prices from: nil, to: 'Amsterdam'
|
208
|
+
end.to raise_error(NSClient::MissingParameter, 'from station is required')
|
222
209
|
end
|
223
210
|
|
224
|
-
it
|
211
|
+
it 'should raise an error when from is not a valid station name' do
|
225
212
|
date = Date.strptime('17-06-2013', '%d-%m-%Y')
|
226
|
-
stub_ns_client_request
|
227
|
-
expect
|
228
|
-
client.prices from:
|
229
|
-
|
213
|
+
stub_ns_client_request 'https://webservices.ns.nl/ns-api-prijzen-v3?from=Amsterdam&to=Purmerend&date=17062013', load_fixture('prices_invalid_station_name.xml')
|
214
|
+
expect do
|
215
|
+
client.prices from: 'Amsterdam', to: 'Purmerend', date: date
|
216
|
+
end.to raise_error(NSClient::InvalidStationNameError, "'Amsterdam' is not a valid station name")
|
230
217
|
end
|
231
218
|
|
232
|
-
it
|
233
|
-
expect
|
234
|
-
client.prices from:
|
235
|
-
|
219
|
+
it 'should raise error when to is not given' do
|
220
|
+
expect do
|
221
|
+
client.prices from: 'Purmerend', to: nil
|
222
|
+
end.to raise_error(NSClient::MissingParameter, 'to station is required')
|
236
223
|
end
|
237
224
|
|
238
|
-
it
|
239
|
-
expect
|
225
|
+
it 'should raise error complaining about from and to missing when both not given' do
|
226
|
+
expect do
|
240
227
|
client.prices from: nil, to: nil
|
241
|
-
|
228
|
+
end.to raise_error(NSClient::MissingParameter, 'from and to station is required')
|
242
229
|
end
|
243
230
|
|
244
|
-
it
|
245
|
-
expect
|
231
|
+
it 'should raise an error when from and to are the same' do
|
232
|
+
expect do
|
246
233
|
client.prices from: 'AMA', to: 'AMA'
|
247
|
-
|
234
|
+
end.to raise_error(NSClient::SameDestinationError, 'from (AMA) and to (AMA) parameters should not be equal')
|
248
235
|
end
|
249
|
-
|
250
236
|
end
|
251
237
|
|
252
238
|
def assert_price(element, expected_type, expected_train_class, expected_amount)
|
253
|
-
element.type.
|
254
|
-
element.train_class.
|
255
|
-
element.amount.
|
239
|
+
expect(element.type).to eq(expected_type)
|
240
|
+
expect(element.train_class).to eq(expected_train_class)
|
241
|
+
expect(element.amount).to eq(expected_amount)
|
256
242
|
end
|
257
243
|
|
258
244
|
def stub_ns_client_request(url, response)
|
259
245
|
# headers based on "username", "password"
|
260
|
-
stub_request(:get, url)
|
261
|
-
|
262
|
-
|
246
|
+
stub_request(:get, url)
|
247
|
+
.with(headers: { 'Authorization' => 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=' })
|
248
|
+
.to_return(status: 200, body: response, headers: {})
|
263
249
|
end
|
264
|
-
|
265
250
|
end
|