ns-yapi 0.4.2 → 0.4.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cea7f72750a0a8d079579025fcc8cbcd02084149
4
+ data.tar.gz: 858eebcce69e1fac3d84fed99621e35038baaad0
5
+ SHA512:
6
+ metadata.gz: 70447d1d9c34a7494c1c758bbb6fec97d5bc60ce5a69aadb7f9719005135863ef83b092a29715dacd797e82fbc229f40024cea2bb9f6287e272248e4e2cac402
7
+ data.tar.gz: 6efd6d32c0c045dab8df034230ee80a0623422b8a499c1a63df928b653e1a90f54fc32ce600ac8623b59142edaa5040d5812f602c5b5e9a00503747b0ea8c2ea
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-1.9.3-p194
1
+ 2.1.2
data/README.md CHANGED
@@ -1,4 +1,8 @@
1
- Yet Another NS API [![Build Status](https://travis-ci.org/stefanhendriks/ns-api.png?branch=master)](https://travis-ci.org/stefanhendriks/ns-api) [![Coverage Status](https://coveralls.io/repos/stefanhendriks/ns-api/badge.png)](https://coveralls.io/r/stefanhendriks/ns-api)
1
+ Yet Another NS API
2
+
3
+ [![Build Status](https://travis-ci.org/zilverline/ns-api.svg?branch=master)](https://travis-ci.org/zilverline/ns-api)
4
+ [![Coverage Status](https://coveralls.io/repos/zilverline/ns-api/badge.svg?branch=master)](https://coveralls.io/r/zilverline/ns-api?branch=master)
5
+
2
6
  ==================
3
7
  A Ruby client for the NS API.
4
8
 
@@ -135,4 +139,4 @@ enkelereis_prices = prices.enkelereis
135
139
  Copyright
136
140
  ---------
137
141
  Copyright (c) 2013 Zilverline / Stefan Hendriks.
138
- See [LICENSE](https://github.com/zilverline/ns-api/blob/master/LICENSE.mkd) for details.
142
+ See [LICENSE](https://github.com/zilverline/ns-api/blob/master/LICENSE.mkd) for details.
data/lib/ns_client.rb CHANGED
@@ -3,9 +3,7 @@ require 'pathname'
3
3
  require 'time'
4
4
  require 'nori'
5
5
  require 'nokogiri'
6
-
7
6
  require 'httpclient'
8
-
9
7
  require "addressable/uri"
10
8
 
11
9
  this = Pathname.new(__FILE__).realpath
@@ -47,7 +45,7 @@ class NSClient
47
45
  def initialize(username, password)
48
46
  @client = HTTPClient.new
49
47
  @client.set_auth("http://webservices.ns.nl", username, password)
50
- @prices_url = PricesUrl.new("http://webservices.ns.nl/ns-api-prijzen-v2")
48
+ @prices_url = PricesUrl.new("http://webservices.ns.nl/ns-api-prijzen-v3")
51
49
  @last_received_raw_xml = ""
52
50
  @last_received_corrected_xml = ""
53
51
  end
@@ -70,6 +68,8 @@ class NSClient
70
68
  raise MissingParameter, "from and to station is required" if (opts[:from] == nil && opts[:to] == nil)
71
69
  raise MissingParameter, "from station is required" unless opts[:from]
72
70
  raise MissingParameter, "to station is required" unless opts[:to]
71
+ raise SameDestinationError,
72
+ "from (#{opts[:from]}) and to (#{opts[:to]}) parameters should not be equal" if opts[:from] == opts[:to]
73
73
  response_xml = get_xml(@prices_url.url(opts))
74
74
  raise_error_when_response_is_error(response_xml)
75
75
  parse_prices(response_xml)
@@ -77,19 +77,23 @@ class NSClient
77
77
 
78
78
  def parse_prices(response_xml)
79
79
  prices_response = PricesResponse.new
80
- (response_xml/'/Producten').each do |products|
81
- prices_response.tariff_units = (products/'./Tariefeenheden').text.to_i
80
+ (response_xml/'/VervoerderKeuzes/VervoerderKeuze').each do |transporter|
81
+ prices_response.tariff_units = (transporter/'./Tariefeenheden').text.to_i
82
82
 
83
- (products/'Product').each do |product|
83
+ (transporter/'ReisType').each do |travel_type|
84
84
  prices = []
85
- (product/'Prijs').each do |price_element|
86
- product_price = ProductPrice.new
87
- product_price.type = price_element.attr("korting")
88
- product_price.train_class = price_element.attr("klasse")
89
- product_price.amount = price_element.text.gsub(",", ".").to_f
90
- prices << product_price
85
+
86
+ (travel_type/'ReisKlasse').each do |travel_class|
87
+ (travel_class/'Korting/Kortingsprijs').each do |price_element|
88
+ product_price = ProductPrice.new
89
+ product_price.discount = price_element.attr("name")
90
+ product_price.train_class = travel_class.attr("klasse")
91
+ product_price.amount = price_element.attr("prijs").gsub(",", ".").to_f
92
+ prices << product_price
93
+ end
91
94
  end
92
- name = product.attr('naam')
95
+
96
+ name = travel_type.attr('name')
93
97
  prices_response.products[name] = prices
94
98
  end
95
99
 
@@ -197,13 +201,21 @@ class NSClient
197
201
  end
198
202
 
199
203
  def dagretour
200
- products["Dagretour"]
204
+ products["Retour"]
201
205
  end
202
206
 
203
207
  end
204
208
 
205
209
  class ProductPrice
206
- attr_accessor :type, :train_class, :amount
210
+ attr_accessor :discount, :train_class, :amount
211
+ DISCOUNT_MAP ||= {
212
+ "20% korting" => "reductie_20",
213
+ "40% korting" => "reductie_40"
214
+ }.freeze
215
+
216
+ def type
217
+ DISCOUNT_MAP.fetch(discount, discount)
218
+ end
207
219
  end
208
220
 
209
221
  class UnplannedDisruption
@@ -227,4 +239,7 @@ class NSClient
227
239
  class UnparseableXMLError < StandardError
228
240
  end
229
241
 
242
+ class SameDestinationError < StandardError
243
+ end
244
+
230
245
  end
data/ns.gemspec CHANGED
@@ -4,12 +4,12 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = "ns-yapi"
7
- gem.version = '0.4.2'
8
- gem.authors = ["Stefan Hendriks"]
7
+ gem.version = '0.4.3'
8
+ gem.authors = ["Stefan Hendriks", "Derek Kraan", "Bob Forma"]
9
9
  gem.email = ["stefanhen83@gmail.com"]
10
10
  gem.description = %q{Yet Another (Ruby) NS API client}
11
11
  gem.summary = %q{A Ruby client for the NS (Dutch Railways) API}
12
- gem.homepage = "https://github.com/stefanhendriks/ns-api"
12
+ gem.homepage = "https://github.com/zilverline/ns-api"
13
13
 
14
14
  gem.files = `git ls-files`.split($/)
15
15
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -19,5 +19,4 @@ Gem::Specification.new do |gem|
19
19
  gem.add_dependency 'httpclient'
20
20
  gem.add_dependency 'nori'
21
21
  gem.add_dependency 'nokogiri'
22
-
23
22
  end
@@ -1,20 +1,46 @@
1
- <?xml version="1.0" encoding="UTF-8" ?>
2
- <Producten>
3
- <Tariefeenheden>10</Tariefeenheden>
4
- <Product naam="Enkele reis">
5
- <Prijs korting="vol tarief" klasse="2">2,40</Prijs>
6
- <Prijs korting="reductie_20" klasse="2">1,90</Prijs>
7
- <Prijs korting="reductie_40" klasse="2">1,40</Prijs>
8
- <Prijs korting="vol tarief" klasse="1">4,10</Prijs>
9
- <Prijs korting="reductie_20" klasse="1">3,30</Prijs>
10
- <Prijs korting="reductie_40" klasse="1">2,50</Prijs>
11
- </Product>
12
- <Product naam="Dagretour">
13
- <Prijs korting="vol tarief" klasse="2">4,80</Prijs>
14
- <Prijs korting="reductie_20" klasse="2">3,80</Prijs>
15
- <Prijs korting="reductie_40" klasse="2">2,80</Prijs>
16
- <Prijs korting="vol tarief" klasse="1">8,20</Prijs>
17
- <Prijs korting="reductie_20" klasse="1">6,60</Prijs>
18
- <Prijs korting="reductie_40" klasse="1">5,00</Prijs>
19
- </Product>
20
- </Producten>
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <VervoerderKeuzes>
3
+ <VervoerderKeuze naam="GEEN_KEUZE">
4
+ <Tariefeenheden>205</Tariefeenheden>
5
+ <ReisType name="Enkele reis">
6
+ <ReisKlasse klasse="1">
7
+ <Prijsdeel vervoerder="NS" prijs="41,50" naar="RTD" van="GBR"/>
8
+ <Totaal>41,50</Totaal>
9
+ <Korting>
10
+ <Kortingsprijs name="vol tarief" prijs="41,50"/>
11
+ <Kortingsprijs name="20% korting" prijs="33,20"/>
12
+ <Kortingsprijs name="40% korting" prijs="24,90"/>
13
+ </Korting>
14
+ </ReisKlasse>
15
+ <ReisKlasse klasse="2">
16
+ <Prijsdeel vervoerder="NS" prijs="24,40" naar="RTD" van="GBR"/>
17
+ <Totaal>24,40</Totaal>
18
+ <Korting>
19
+ <Kortingsprijs name="vol tarief" prijs="24,40"/>
20
+ <Kortingsprijs name="20% korting" prijs="19,50"/>
21
+ <Kortingsprijs name="40% korting" prijs="14,60"/>
22
+ </Korting>
23
+ </ReisKlasse>
24
+ </ReisType>
25
+ <ReisType name="Retour">
26
+ <ReisKlasse klasse="1">
27
+ <Prijsdeel vervoerder="NS" prijs="83,00" naar="RTD" van="GBR"/>
28
+ <Totaal>83,00</Totaal>
29
+ <Korting>
30
+ <Kortingsprijs name="vol tarief" prijs="83,00"/>
31
+ <Kortingsprijs name="20% korting" prijs="66,40"/>
32
+ <Kortingsprijs name="40% korting" prijs="49,80"/>
33
+ </Korting>
34
+ </ReisKlasse>
35
+ <ReisKlasse klasse="2">
36
+ <Prijsdeel vervoerder="NS" prijs="48,80" naar="RTD" van="GBR"/>
37
+ <Totaal>48,80</Totaal>
38
+ <Korting>
39
+ <Kortingsprijs name="vol tarief" prijs="48,80"/>
40
+ <Kortingsprijs name="20% korting" prijs="39,00"/>
41
+ <Kortingsprijs name="40% korting" prijs="29,20"/>
42
+ </Korting>
43
+ </ReisKlasse>
44
+ </ReisType>
45
+ </VervoerderKeuze>
46
+ </VervoerderKeuzes>
@@ -188,11 +188,11 @@ describe NSClient do
188
188
  context "Prices" do
189
189
 
190
190
  it "should retrieve prices for a trip" do
191
- stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-prijzen-v2?from=Purmerend&to=Amsterdam&via=Zaandam&date=17062013", load_fixture('prices.xml')
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')
192
192
  date = Date.strptime('17-06-2013', '%d-%m-%Y')
193
- response = client.prices from: "Purmerend", to: "Amsterdam", via: "Zaandam", date: date
193
+ response = client.prices from: "Rotterdam", to: "Glanerbrug", date: date
194
194
  response.class.should == NSClient::PricesResponse
195
- response.tariff_units.should == 10
195
+ response.tariff_units.should == 205
196
196
  response.products.size.should == 2
197
197
 
198
198
  response.enkele_reis.size.should == 6
@@ -200,19 +200,19 @@ describe NSClient do
200
200
  end
201
201
 
202
202
  it "should retrieve expected price data" do
203
- stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-prijzen-v2?from=Purmerend&to=Amsterdam&via=Zaandam&date=17062013", load_fixture('prices.xml')
203
+ stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-prijzen-v3?from=Rotterdam&to=Glanerbrug&date=17062013", load_fixture('prices.xml')
204
204
  date = Date.strptime('17-06-2013', '%d-%m-%Y')
205
- response = client.prices from: "Purmerend", to: "Amsterdam", via: "Zaandam", date: date
205
+ response = client.prices from: "Rotterdam", to: "Glanerbrug", date: date
206
206
  response.class.should == NSClient::PricesResponse
207
- response.tariff_units.should == 10
207
+ response.tariff_units.should == 205
208
208
  response.products.size.should == 2
209
209
 
210
- assert_price(response.enkele_reis[0], "vol tarief", "2", 2.4)
211
- assert_price(response.enkele_reis[1], "reductie_20", "2", 1.90)
212
- assert_price(response.enkele_reis[2], "reductie_40", "2", 1.40)
213
- assert_price(response.enkele_reis[3], "vol tarief", "1", 4.10)
214
- assert_price(response.enkele_reis[4], "reductie_20", "1", 3.3)
215
- assert_price(response.enkele_reis[5], "reductie_40", "1", 2.5)
210
+ assert_price(response.enkele_reis[0], "vol tarief", "1", 41.5)
211
+ assert_price(response.enkele_reis[1], "reductie_20", "1", 33.2)
212
+ assert_price(response.enkele_reis[2], "reductie_40", "1", 24.9)
213
+ assert_price(response.enkele_reis[3], "vol tarief", "2", 24.4)
214
+ assert_price(response.enkele_reis[4], "reductie_20", "2", 19.5)
215
+ assert_price(response.enkele_reis[5], "reductie_40", "2", 14.6)
216
216
  end
217
217
 
218
218
  it "should raise error when from is not given" do
@@ -223,7 +223,7 @@ describe NSClient do
223
223
 
224
224
  it "should raise an error when from is not a valid station name" do
225
225
  date = Date.strptime('17-06-2013', '%d-%m-%Y')
226
- stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-prijzen-v2?from=Amsterdam&to=Purmerend&date=17062013", load_fixture('prices_invalid_station_name.xml')
226
+ stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-prijzen-v3?from=Amsterdam&to=Purmerend&date=17062013", load_fixture('prices_invalid_station_name.xml')
227
227
  expect {
228
228
  client.prices from: "Amsterdam", to: "Purmerend", date: date
229
229
  }.to raise_error(NSClient::InvalidStationNameError, "'Amsterdam' is not a valid station name")
@@ -241,6 +241,12 @@ describe NSClient do
241
241
  }.to raise_error(NSClient::MissingParameter, "from and to station is required")
242
242
  end
243
243
 
244
+ it "should raise an error when from and to are the same" do
245
+ expect {
246
+ client.prices from: 'AMA', to: 'AMA'
247
+ }.to raise_error(NSClient::SameDestinationError, "from (AMA) and to (AMA) parameters should not be equal")
248
+ end
249
+
244
250
  end
245
251
 
246
252
  def assert_price(element, expected_type, expected_train_class, expected_amount)
metadata CHANGED
@@ -1,62 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ns-yapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
5
- prerelease:
4
+ version: 0.4.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Stefan Hendriks
8
+ - Derek Kraan
9
+ - Bob Forma
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2013-09-11 00:00:00.000000000 Z
13
+ date: 2015-01-26 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: httpclient
16
17
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
18
  requirements:
19
- - - ! '>='
19
+ - - ">="
20
20
  - !ruby/object:Gem::Version
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
25
  requirements:
27
- - - ! '>='
26
+ - - ">="
28
27
  - !ruby/object:Gem::Version
29
28
  version: '0'
30
29
  - !ruby/object:Gem::Dependency
31
30
  name: nori
32
31
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
32
  requirements:
35
- - - ! '>='
33
+ - - ">="
36
34
  - !ruby/object:Gem::Version
37
35
  version: '0'
38
36
  type: :runtime
39
37
  prerelease: false
40
38
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
39
  requirements:
43
- - - ! '>='
40
+ - - ">="
44
41
  - !ruby/object:Gem::Version
45
42
  version: '0'
46
43
  - !ruby/object:Gem::Dependency
47
44
  name: nokogiri
48
45
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
46
  requirements:
51
- - - ! '>='
47
+ - - ">="
52
48
  - !ruby/object:Gem::Version
53
49
  version: '0'
54
50
  type: :runtime
55
51
  prerelease: false
56
52
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
53
  requirements:
59
- - - ! '>='
54
+ - - ">="
60
55
  - !ruby/object:Gem::Version
61
56
  version: '0'
62
57
  description: Yet Another (Ruby) NS API client
@@ -66,10 +61,10 @@ executables: []
66
61
  extensions: []
67
62
  extra_rdoc_files: []
68
63
  files:
69
- - .gitignore
70
- - .ruby-gemset
71
- - .ruby-version
72
- - .travis.yml
64
+ - ".gitignore"
65
+ - ".ruby-gemset"
66
+ - ".ruby-version"
67
+ - ".travis.yml"
73
68
  - Gemfile
74
69
  - Gemfile.lock
75
70
  - LICENSE.mkd
@@ -93,35 +88,28 @@ files:
93
88
  - spec/ns_client_spec.rb
94
89
  - spec/nsyapi_spec.rb
95
90
  - spec/spec_helper.rb
96
- homepage: https://github.com/stefanhendriks/ns-api
91
+ homepage: https://github.com/zilverline/ns-api
97
92
  licenses: []
93
+ metadata: {}
98
94
  post_install_message:
99
95
  rdoc_options: []
100
96
  require_paths:
101
97
  - lib
102
98
  required_ruby_version: !ruby/object:Gem::Requirement
103
- none: false
104
99
  requirements:
105
- - - ! '>='
100
+ - - ">="
106
101
  - !ruby/object:Gem::Version
107
102
  version: '0'
108
- segments:
109
- - 0
110
- hash: -3423485013195934807
111
103
  required_rubygems_version: !ruby/object:Gem::Requirement
112
- none: false
113
104
  requirements:
114
- - - ! '>='
105
+ - - ">="
115
106
  - !ruby/object:Gem::Version
116
107
  version: '0'
117
- segments:
118
- - 0
119
- hash: -3423485013195934807
120
108
  requirements: []
121
109
  rubyforge_project:
122
- rubygems_version: 1.8.24
110
+ rubygems_version: 2.2.2
123
111
  signing_key:
124
- specification_version: 3
112
+ specification_version: 4
125
113
  summary: A Ruby client for the NS (Dutch Railways) API
126
114
  test_files:
127
115
  - spec/fixtures/disruption_invalid_station_name.xml