ns-yapi 0.1 → 0.1.1

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.
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ns-yapi (0.1)
5
- httpi
4
+ ns-yapi (0.1.1)
5
+ httpclient
6
6
  nokogiri
7
7
  nori
8
8
 
@@ -21,8 +21,6 @@ GEM
21
21
  safe_yaml (~> 0.9.0)
22
22
  diff-lcs (1.2.4)
23
23
  httpclient (2.3.3)
24
- httpi (2.0.2)
25
- rack
26
24
  metaclass (0.0.1)
27
25
  mime-types (1.23)
28
26
  mini_portile (0.5.0)
@@ -32,7 +30,6 @@ GEM
32
30
  nokogiri (1.6.0)
33
31
  mini_portile (~> 0.5.0)
34
32
  nori (2.2.0)
35
- rack (1.5.2)
36
33
  rake (10.0.3)
37
34
  rest-client (1.6.7)
38
35
  mime-types (>= 1.16)
data/README.md CHANGED
@@ -2,16 +2,55 @@ Yet Another NS API [![Build Status](https://travis-ci.org/stefanhendriks/ns-api.
2
2
  ==================
3
3
  A Ruby client for the NS API.
4
4
 
5
+ Goal
6
+ ====
7
+ I'd like to write a comprehensive, yet very thin implementation of the NS API. For now I have limited access therefor this gem is limited.
5
8
 
9
+ If you have credentials that work with the prices-api calls, and are willing to share them with me so I can expand this gem. Please contact me.
10
+
11
+ You can also send me an example response, so I can work from there. Although I would like to see it working for real as well.
12
+
13
+ Pull requests are welcome.
6
14
 
7
15
  Usage
8
16
  =====
9
- First, make sure you have a username and password from the NS API website.
17
+ First, make sure you have a username and password from the NS API website. (at: http://www.ns.nl/api)
18
+
19
+ You can use the NSYAPI singleton, you can configure it by using a configuration block:
20
+ ```ruby
21
+ require 'ns_client'
22
+
23
+ NSYapi::configure do |config|
24
+ config.username = "some-username"
25
+ config.password = "some-password"
26
+ end
27
+
28
+ client = NSYapi::client
29
+
30
+ client.stations
31
+ ```
32
+
33
+ or, you can instantiate the NSClient yourself, providing a username and password. You can then regulate the instance yourself.
34
+
10
35
  ```ruby
36
+ require 'ns_client'
37
+
11
38
  # get username/password from NS site
12
39
  client = NSClient.new("my-username", "my-password")
13
40
 
41
+ ```
42
+
43
+ After you have created a client, you can use it for several operations
44
+
45
+ Retrieve all stations
46
+ =====================
47
+
48
+ ```ruby
14
49
  # get all known stations
15
- client.stations
50
+ stations = client.stations
51
+ station = stations.first
52
+ station.name # 's-Hertogenbosch
53
+ station.lat # 51.69048
54
+ station.long # 5.29362
16
55
  ```
17
56
 
data/lib/ns_client.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'pathname'
2
2
  require 'time'
3
- require 'httpi'
4
3
  require 'nori'
5
4
  require 'nokogiri'
6
5
 
@@ -16,6 +15,29 @@ Dir.glob(File.join(lib_path, '/**/*.rb')).each do |file|
16
15
  require file
17
16
  end
18
17
 
18
+ module NSYapi
19
+
20
+ class Configuration
21
+ attr_accessor :username, :password
22
+ end
23
+
24
+ # Configures Plugin.
25
+ def self.configure(configuration = NSYapi::Configuration.new)
26
+ yield configuration if block_given?
27
+ @@configuration = configuration
28
+ end
29
+
30
+ def self.configuration # :nodoc:
31
+ @@configuration ||= Plugin::NSYapi.new
32
+ end
33
+
34
+ def self.client
35
+ @client_instance = NSClient.new(configuration.username, configuration.password) unless @client_instance
36
+ @client_instance
37
+ end
38
+
39
+ end
40
+
19
41
  class NSClient
20
42
 
21
43
  def initialize(username, password)
@@ -43,6 +65,36 @@ class NSClient
43
65
  result
44
66
  end
45
67
 
68
+ def disruptions
69
+ response = @client.get "http://username:password@webservices.ns.nl/ns-api-storingen?"
70
+ result = {planned: [], unplanned: []}
71
+ xdoc = Nokogiri.XML(response.content)
72
+
73
+ (xdoc/'/Storingen').each { |disruption|
74
+
75
+ (disruption/'Ongepland/Storing').each { |unplanned|
76
+ # TODO: check if element has data
77
+ unplanned_disruption = UnplannedDisruption.new
78
+ result[:unplanned] << unplanned_disruption
79
+ }
80
+
81
+ (disruption/'Gepland/Storing').each { |planned|
82
+ # TODO: check if element has data
83
+ planned_disruption = PlannedDisruption.new
84
+ result[:planned] << planned_disruption
85
+ }
86
+ }
87
+ result
88
+ end
89
+
90
+ class UnplannedDisruption
91
+ # TODO: add properties
92
+ end
93
+
94
+ class PlannedDisruption
95
+ # TODO: add properties
96
+ end
97
+
46
98
  class Station
47
99
  attr_accessor :code, :type, :land, :short_name, :name, :long_name, :uiccode, :synonyms, :lat, :long
48
100
  end
data/ns.gemspec CHANGED
@@ -4,10 +4,10 @@ $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.1'
7
+ gem.version = '0.1.1'
8
8
  gem.authors = ["Stefan Hendriks"]
9
9
  gem.email = ["stefanhen83@gmail.com"]
10
- gem.description = %q{A Ruby client for the NS (Dutch Railways) API}
10
+ gem.description = %q{Yet Another (Ruby) NS API}
11
11
  gem.summary = %q{A Ruby client for the NS (Dutch Railways) API}
12
12
  gem.homepage = "https://github.com/stefanhendriks/ns-api"
13
13
 
@@ -16,7 +16,8 @@ Gem::Specification.new do |gem|
16
16
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
17
  gem.require_paths = ["lib"]
18
18
 
19
- gem.add_dependency 'httpi'
19
+ gem.add_dependency 'httpclient'
20
20
  gem.add_dependency 'nori'
21
21
  gem.add_dependency 'nokogiri'
22
+
22
23
  end
@@ -0,0 +1,24 @@
1
+ <Storingen>
2
+ <Ongepland>
3
+ <Storing>
4
+ <id>prio-13345</id>
5
+ <Traject>'s-Hertogenbosch-Nijmegen</Traject>
6
+ <Reden>beperkingen op last van de politie</Reden>
7
+ <Bericht></Bericht>
8
+ <Datum>2010-12-16T11:16:00+0100</Datum>
9
+ </Storing>
10
+ </Ongepland>
11
+ <Gepland>
12
+ <Storing>
13
+ <id>2010_almo_wp_18_19dec</id>
14
+ <Traject>Almere Oostvaarders-Weesp/Naarden-Bussum</Traject>
15
+ <Periode>zaterdag 18 en zondag 19 december</Periode>
16
+ <Reden>Beperkt treinverkeer, businzet en/of omreizen, extra reistijd 15-30 min.</Reden>
17
+ <Advies>Maak gebruik van de overige treinen of de bussen: reis tussen Weesp en Almere Centrum met de NS-bus in
18
+ plaats van de trein tussen Almere Centrum en Lelystad Centrum rijden vier Sprinters per uur reis tussen Almere
19
+ Muziekwijk en Naarden-Bussum via Weesp
20
+ </Advies>
21
+ <Bericht></Bericht>
22
+ </Storing>
23
+ </Gepland>
24
+ </Storingen>
@@ -0,0 +1,148 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+
3
+ <Storingen>
4
+ <Ongepland>
5
+
6
+ </Ongepland>
7
+ <Gepland>
8
+
9
+ <Storing>
10
+ <id>2013_hrl_aachen_14_17jun</id>
11
+
12
+ <Traject>Heerlen-Aachen Hbf</Traject>
13
+
14
+
15
+ <Periode>vrijdag 14 juni tot en met maandag 17 juni 13.00 uur</Periode>
16
+
17
+
18
+ <Advies>U kunt gebruikmaken van de bus tussen Heerlen en Herzogenrath kunt u gebruikmaken van de bussen tussen
19
+ Heerlen en Heerlen de Kissel kunt u gebruikmaken van de reguliere busdienst van Veolia
20
+ </Advies>
21
+
22
+
23
+ <Bericht><![CDATA[
24
+ <p>
25
+ <b>Wanneer: vrijdag 14 juni tot en met maandag 17 juni 13.00 uur</b>
26
+ <br/>
27
+ <b>Oorzaak: aangepaste dienstregeling</b>
28
+ <br/>
29
+ <b>Advies: U kunt gebruikmaken van de bus</b>
30
+ <br/> tussen Heerlen en Herzogenrath kunt u gebruikmaken van de bussen<br/> tussen Heerlen en Heerlen de Kissel kunt u gebruikmaken van de reguliere busdienst van Veolia<br/><br/><b>Extra Reistijd: een kwartier tot een half uur</b><br/></p>
31
+ ]]></Bericht>
32
+
33
+
34
+ <Oorzaak>aangepaste dienstregeling</Oorzaak>
35
+
36
+
37
+ <Vertraging>een kwartier tot een half uur</Vertraging>
38
+
39
+ </Storing>
40
+
41
+ <Storing>
42
+ <id>2013_asd_basel_15_16jun</id>
43
+
44
+ <Traject>Amsterdam C-Koeln Hbf-Frankfurt (M) Hbf-Basel SBB</Traject>
45
+
46
+
47
+ <Periode>zaterdag 15 en zondag 16 juni</Periode>
48
+
49
+
50
+ <Advies>U kunt gebruikmaken van de gewijzigde dienstregeling door werkzaamheden rijdt ICE International via een
51
+ andere route daardoor stopt ICE International niet in Arnhem, Oberhausen, Duisburg en Duesseldorf, maar extra in
52
+ 's-Hertogenbosch en Eindhoven en vaak ook in Moenchengladgach; de vertrek- en aankomsttijden op alle stations
53
+ zullen worden gewijzigd houdt u rekening met eerder vertrekken of later aankomen
54
+ </Advies>
55
+
56
+
57
+ <Bericht><![CDATA[
58
+ <p>
59
+ <b>Wanneer: zaterdag 15 en zondag 16 juni</b>
60
+ <br/>
61
+ <b>Oorzaak: door werkzaamheden</b>
62
+ <br/>
63
+ <b>Advies: U kunt gebruikmaken van de gewijzigde dienstregeling</b>
64
+ <br/> door werkzaamheden rijdt ICE International via een andere route<br/> daardoor stopt ICE International niet in Arnhem, Oberhausen, Duisburg en Duesseldorf, maar extra in 's-Hertogenbosch en Eindhoven en vaak ook in Moenchengladgach; de vertrek- en aankomsttijden op alle stations zullen worden gewijzigd<br/> houdt u rekening met eerder vertrekken of later aankomen<br/><br/><b>Extra Reistijd: een uur</b><br/></p>
65
+ ]]></Bericht>
66
+
67
+
68
+ <Oorzaak>door werkzaamheden</Oorzaak>
69
+
70
+
71
+ <Vertraging>een uur</Vertraging>
72
+
73
+ </Storing>
74
+
75
+ <Storing>
76
+ <id>2013_amf_brn_ut_16jun</id>
77
+
78
+ <Traject>Amersfoort/Baarn/Utrecht Centraal</Traject>
79
+
80
+
81
+ <Periode>zondag 16 juni</Periode>
82
+
83
+
84
+ <Advies>U kunt gebruikmaken van de omgeleide Intercity, omreizen of van de bussen Intercity's tussen Amersfoort en
85
+ Utrecht Centraal rijden via Hilversum Sprinters tussen Baarn, Den Dolder en Utrecht Centraal rijden niet; er
86
+ rijdt elk kwartier een NS-bus tussen Baarn, Den Dolder en Utrecht Overvecht tussen Utrecht Overvecht en Utrecht
87
+ Centraal kunt u gebruikmaken van de Sprinters tussen Hilversum en Utrecht Centraal tussen Baarn en Utrecht
88
+ Overvecht/Utrecht Centraal kunt u omreizen via Hilversum
89
+ </Advies>
90
+
91
+
92
+ <Bericht><![CDATA[
93
+ <p>
94
+ <b>Wanneer: zondag 16 juni</b>
95
+ <br/>
96
+ <b>Oorzaak: door werkzaamheden</b>
97
+ <br/>
98
+ <b>Advies: U kunt gebruikmaken van de omgeleide Intercity, omreizen of van de bussen</b>
99
+ <br/> Intercity's tussen Amersfoort en Utrecht Centraal rijden via Hilversum<br/> Sprinters tussen Baarn, Den Dolder en Utrecht Centraal rijden niet; er rijdt elk kwartier een NS-bus tussen Baarn, Den Dolder en Utrecht Overvecht<br/> tussen Utrecht Overvecht en Utrecht Centraal kunt u gebruikmaken van de Sprinters tussen Hilversum en Utrecht Centraal<br/> tussen Baarn en Utrecht Overvecht/Utrecht Centraal kunt u omreizen via Hilversum<br/><br/><b>Extra Reistijd: een kwartier tot een half uur</b><br/></p>
100
+ ]]></Bericht>
101
+
102
+
103
+ <Oorzaak>door werkzaamheden</Oorzaak>
104
+
105
+
106
+ <Vertraging>een kwartier tot een half uur</Vertraging>
107
+
108
+ </Storing>
109
+
110
+ <Storing>
111
+ <id>2013_asd_bd_16jun</id>
112
+
113
+ <Traject>Amsterdam Centraal-Breda Fyra</Traject>
114
+
115
+
116
+ <Periode>zondag 16 juni</Periode>
117
+
118
+
119
+ <Advies>U kunt gebruikmaken van de overige treinen Tot 09.30 uur: de treinen die om 06.56, 07.26 en om 07.56 uur
120
+ uit Amsterdam Centraal zouden vertrekken, rijden uitsluitend tussen Schiphol en Rotterdam Centraal en stoppen
121
+ daardoor niet in Amsterdam Centraal en Breda de treinen die om 07.28, 07.58, 08.28, 08.58 en 09.28 uur uit Breda
122
+ zouden vertrekken, rijden niet tussen Breda en Rotterdam Centraal, maar uitsluitend tussen Rotterdam Centraal en
123
+ Schiphol daardoor stoppen deze treinen niet in Breda en Amsterdam Centraal Na 09.30 uur: Fyra rijdt de hele dag
124
+ uitsluitend tussen Schiphol, Rotterdam Centraal en Breda; daardoor stopt Fyra niet in Amsterdam Centraal we
125
+ adviseren u uw reis kort van te voren te plannen met behulp van de reisplanners
126
+ </Advies>
127
+
128
+
129
+ <Bericht><![CDATA[
130
+ <p>
131
+ <b>Wanneer: zondag 16 juni</b>
132
+ <br/>
133
+ <b>Oorzaak: door werkzaamheden</b>
134
+ <br/>
135
+ <b>Advies: U kunt gebruikmaken van de overige treinen</b>
136
+ <br/> Tot 09.30 uur:<br/> de treinen die om 06.56, 07.26 en om 07.56 uur uit Amsterdam Centraal zouden vertrekken, rijden uitsluitend tussen Schiphol en Rotterdam Centraal en stoppen daardoor niet in Amsterdam Centraal en Breda<br/> de treinen die om 07.28, 07.58, 08.28, 08.58 en 09.28 uur uit Breda zouden vertrekken, rijden niet tussen Breda en Rotterdam Centraal, maar uitsluitend tussen Rotterdam Centraal en Schiphol<br/> daardoor stoppen deze treinen niet in Breda en Amsterdam Centraal<br/> Na 09.30 uur:<br/> Fyra rijdt de hele dag uitsluitend tussen Schiphol, Rotterdam Centraal en Breda; daardoor stopt Fyra niet in Amsterdam Centraal<br/> we adviseren u uw reis kort van te voren te plannen met behulp van de reisplanners<br/><br/><b>Extra Reistijd: een half uur tot een uur</b><br/></p>
137
+ ]]></Bericht>
138
+
139
+
140
+ <Oorzaak>door werkzaamheden</Oorzaak>
141
+
142
+
143
+ <Vertraging>een half uur tot een uur</Vertraging>
144
+
145
+ </Storing>
146
+
147
+ </Gepland>
148
+ </Storingen>
@@ -0,0 +1,10 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+
3
+ <Storingen>
4
+ <Ongepland>
5
+
6
+ </Ongepland>
7
+ <Gepland>
8
+
9
+ </Gepland>
10
+ </Storingen>
@@ -0,0 +1,20 @@
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>
@@ -3,22 +3,22 @@ require 'spec_helper'
3
3
  describe NSClient do
4
4
 
5
5
  before :each do
6
- @nsclient = NSClient.new("username", "password")
6
+ @client = NSClient.new("username", "password")
7
7
  end
8
8
 
9
- context "Fetching stations from NS" do
9
+ context "Stations" do
10
10
 
11
11
  before :each do
12
- stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-stations-v2", load_fixture('ns_stations.xml')
12
+ stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-stations-v2", load_fixture('stations.xml')
13
13
  end
14
14
 
15
15
  it "should return all stations" do
16
- stations = @nsclient.stations
16
+ stations = @client.stations
17
17
  stations.size.should == 620
18
18
  end
19
19
 
20
20
  it "should return expected first station from list" do
21
- stations = @nsclient.stations
21
+ stations = @client.stations
22
22
  first_station = stations.first
23
23
  first_station.class.should == NSClient::Station
24
24
  first_station.type.should == "knooppuntIntercitystation"
@@ -34,6 +34,35 @@ describe NSClient do
34
34
 
35
35
  end
36
36
 
37
+ context "Disruptions" do
38
+
39
+ it "should retrieve planned and unplanned disruptions" do
40
+ stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-storingen?", load_fixture('disruptions.xml')
41
+ disruptions = @client.disruptions
42
+ disruptions.size.should == 2
43
+ disruptions[:planned].size.should == 1
44
+ disruptions[:unplanned].size.should == 1
45
+ end
46
+
47
+ xit "should retrieve expected planned disruption"
48
+ xit "should retrieve expected unplanned disruption"
49
+
50
+ xit "should not return disruption when empty in response" do
51
+ stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-storingen?", load_fixture('no_disruptions.xml')
52
+ disruptions = @client.disruptions
53
+ disruptions.size.should == 2
54
+ disruptions[:planned].size.should == 0
55
+ disruptions[:unplanned].size.should == 0
56
+ end
57
+
58
+ xit "should retrieve disruptions for station name" # ie, for Amsterdam only (http://webservices.ns.nl/ns-api-storingen?station=Amsterdam)
59
+
60
+ end
61
+
62
+ context "Prices" do
63
+ xit "should retrieve prices for a trip"
64
+ end
65
+
37
66
  def stub_ns_client_request(url, response)
38
67
  # headers based on "username", "password"
39
68
  stub_request(:get, url).
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe NSYapi do
4
+
5
+ context "Configuration" do
6
+
7
+ it "should be configurable with a block" do
8
+ NSYapi::configure do |config|
9
+ config.username = "some-username"
10
+ config.password = "some-password"
11
+ end
12
+ NSYapi::configuration.username.should == "some-username"
13
+ NSYapi::configuration.password.should == "some-password"
14
+ end
15
+
16
+ end
17
+
18
+ context "Singleton" do
19
+
20
+ it "should create a singleton" do
21
+ NSYapi::configure do |config|
22
+ config.username = "some-username"
23
+ config.password = "some-password"
24
+ end
25
+
26
+ client = NSYapi::client
27
+ client.should == NSYapi::client
28
+ end
29
+
30
+ end
31
+
32
+
33
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ns-yapi
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,10 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-16 00:00:00.000000000 Z
12
+ date: 2013-06-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: httpi
15
+ name: httpclient
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
@@ -59,7 +59,7 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
- description: A Ruby client for the NS (Dutch Railways) API
62
+ description: Yet Another (Ruby) NS API
63
63
  email:
64
64
  - stefanhen83@gmail.com
65
65
  executables: []
@@ -75,8 +75,13 @@ files:
75
75
  - lib/ns_client.rb
76
76
  - ns.gemspec
77
77
  - rakefile.rb
78
- - spec/fixtures/ns_stations.xml
78
+ - spec/fixtures/disruptions.xml
79
+ - spec/fixtures/disruptions_amsterdam.xml
80
+ - spec/fixtures/no_disruptions.xml
81
+ - spec/fixtures/prices.xml
82
+ - spec/fixtures/stations.xml
79
83
  - spec/ns_client_spec.rb
84
+ - spec/nsyapi_spec.rb
80
85
  - spec/spec_helper.rb
81
86
  homepage: https://github.com/stefanhendriks/ns-api
82
87
  licenses: []
@@ -103,6 +108,11 @@ signing_key:
103
108
  specification_version: 3
104
109
  summary: A Ruby client for the NS (Dutch Railways) API
105
110
  test_files:
106
- - spec/fixtures/ns_stations.xml
111
+ - spec/fixtures/disruptions.xml
112
+ - spec/fixtures/disruptions_amsterdam.xml
113
+ - spec/fixtures/no_disruptions.xml
114
+ - spec/fixtures/prices.xml
115
+ - spec/fixtures/stations.xml
107
116
  - spec/ns_client_spec.rb
117
+ - spec/nsyapi_spec.rb
108
118
  - spec/spec_helper.rb
File without changes