ns-yapi 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ns-yapi (0.1.2)
4
+ ns-yapi (0.2.0)
5
5
  httpclient
6
6
  nokogiri
7
7
  nori
data/README.md CHANGED
@@ -1,4 +1,4 @@
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 [![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) [![Dependency Status](https://gemnasium.com/stefanhendriks/ns-api.png)](https://gemnasium.com/stefanhendriks/ns-api)
2
2
  ==================
3
3
  A Ruby client for the NS API.
4
4
 
@@ -54,3 +54,21 @@ station.lat # 51.69048
54
54
  station.long # 5.29362
55
55
  ```
56
56
 
57
+ Retrieve disruptions
58
+ ====================
59
+ ```ruby
60
+ # get all known disruptions
61
+ disruptions = client.disruptions
62
+
63
+ # get planned disruptions
64
+ planned = disruptions[:planned]
65
+
66
+ # get unplanned disruptions
67
+ unplanned = disruptions[:unplanned]
68
+
69
+ # get disruptions from specific station, ie Amsterdam (case insensitive)
70
+ disruptions = client.disruptions "Amsterdam"
71
+
72
+ # will raise an NSClient::InvalidStationNameError error when station name is invalid
73
+ client.disruptions "bla" # NSClient::InvalidStationNameError: Could not find a station with name 'bla'
74
+ ```
data/lib/ns_client.rb CHANGED
@@ -48,7 +48,7 @@ class NSClient
48
48
  response = @client.get "http://webservices.ns.nl/ns-api-stations-v2"
49
49
  result = []
50
50
  xdoc = Nokogiri.XML(response.content)
51
- (xdoc/'/Stations/Station').each { |station|
51
+ (xdoc/'/Stations/Station').each do |station|
52
52
  s = Station.new
53
53
  s.code = (station/'./Code').text
54
54
  s.type = (station/'./Type').text
@@ -60,42 +60,66 @@ class NSClient
60
60
  s.long = (station/'./Lon').text
61
61
  s.uiccode = (station/'./UICCode').text
62
62
  result << s
63
- }
63
+ end
64
64
  result
65
65
  end
66
66
 
67
- def disruptions
68
- response = @client.get "http://username:password@webservices.ns.nl/ns-api-storingen?"
67
+ def disruptions (query = nil)
68
+ response = @client.get disruption_url(query)
69
69
  result = {planned: [], unplanned: []}
70
70
  xdoc = Nokogiri.XML(response.content)
71
71
 
72
+ (xdoc/'/error').each do |error|
73
+ message = (error/'./message').text
74
+ raise InvalidStationNameError, message
75
+ end
76
+
72
77
  (xdoc/'/Storingen').each { |disruption|
73
78
 
74
79
  (disruption/'Ongepland/Storing').each { |unplanned|
75
- # TODO: check if element has data
76
80
  unplanned_disruption = UnplannedDisruption.new
81
+ unplanned_disruption.id = (unplanned/'./id').text
82
+ unplanned_disruption.trip = (unplanned/'./Traject').text
83
+ unplanned_disruption.reason = (unplanned/'./Reden').text
84
+ unplanned_disruption.message = (unplanned/'./Bericht').text
85
+ unplanned_disruption.datetime_string = (unplanned/'./Datum').text
77
86
  result[:unplanned] << unplanned_disruption
78
87
  }
79
88
 
80
89
  (disruption/'Gepland/Storing').each { |planned|
81
- # TODO: check if element has data
82
90
  planned_disruption = PlannedDisruption.new
91
+ planned_disruption.id = (planned/'./id').text
92
+ planned_disruption.trip = (planned/'./Traject').text
93
+ planned_disruption.reason = (planned/'./Reden').text
94
+ planned_disruption.advice = (planned/'./Advies').text
95
+ planned_disruption.message = (planned/'./Bericht').text
83
96
  result[:planned] << planned_disruption
84
97
  }
85
98
  }
86
99
  result
87
100
  end
88
101
 
102
+ def disruption_url(query)
103
+ if query
104
+ return "http://webservices.ns.nl/ns-api-storingen?station=#{query}"
105
+ end
106
+ "http://webservices.ns.nl/ns-api-storingen?"
107
+ end
108
+
89
109
  class UnplannedDisruption
90
- # TODO: add properties
110
+ attr_accessor :id, :trip, :reason, :message, :datetime_string
91
111
  end
92
112
 
93
113
  class PlannedDisruption
94
- # TODO: add properties
114
+ attr_accessor :id, :trip, :reason, :advice, :message
95
115
  end
96
116
 
97
117
  class Station
98
118
  attr_accessor :code, :type, :land, :short_name, :name, :long_name, :uiccode, :synonyms, :lat, :long
99
119
  end
100
120
 
121
+ class InvalidStationNameError < StandardError
122
+
123
+ end
124
+
101
125
  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.2'
7
+ gem.version = '0.2.0'
8
8
  gem.authors = ["Stefan Hendriks"]
9
9
  gem.email = ["stefanhen83@gmail.com"]
10
- gem.description = %q{Yet Another (Ruby) NS API}
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
12
  gem.homepage = "https://github.com/stefanhendriks/ns-api"
13
13
 
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+
3
+
4
+ <error>
5
+ <message>Could not find a station with name &#039;bla&#039;</message>
6
+ </error>
@@ -4,7 +4,7 @@
4
4
  <id>prio-13345</id>
5
5
  <Traject>'s-Hertogenbosch-Nijmegen</Traject>
6
6
  <Reden>beperkingen op last van de politie</Reden>
7
- <Bericht></Bericht>
7
+ <Bericht>Another test message</Bericht>
8
8
  <Datum>2010-12-16T11:16:00+0100</Datum>
9
9
  </Storing>
10
10
  </Ongepland>
@@ -16,9 +16,8 @@
16
16
  <Reden>Beperkt treinverkeer, businzet en/of omreizen, extra reistijd 15-30 min.</Reden>
17
17
  <Advies>Maak gebruik van de overige treinen of de bussen: reis tussen Weesp en Almere Centrum met de NS-bus in
18
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>
19
+ Muziekwijk en Naarden-Bussum via Weesp</Advies>
20
+ <Bericht>Test message</Bericht>
22
21
  </Storing>
23
22
  </Gepland>
24
23
  </Storingen>
@@ -44,10 +44,37 @@ describe NSClient do
44
44
  disruptions[:unplanned].size.should == 1
45
45
  end
46
46
 
47
- xit "should retrieve expected planned disruption"
48
- xit "should retrieve expected unplanned disruption"
47
+ it "should retrieve expected planned disruption" do
48
+ stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-storingen?", load_fixture('disruptions.xml')
49
+ disruptions = @client.disruptions
50
+ disruptions.size.should == 2
51
+ planned_disruption = disruptions[:planned].first
52
+ planned_disruption.class.should == NSClient::PlannedDisruption
53
+
54
+ planned_disruption.id.should == "2010_almo_wp_18_19dec"
55
+ planned_disruption.trip.should == "Almere Oostvaarders-Weesp/Naarden-Bussum"
56
+ planned_disruption.reason.should == "Beperkt treinverkeer, businzet en/of omreizen, extra reistijd 15-30 min."
57
+ planned_disruption.advice.should == "Maak gebruik van de overige treinen of de bussen: reis tussen Weesp en Almere Centrum met de NS-bus in
58
+ plaats van de trein tussen Almere Centrum en Lelystad Centrum rijden vier Sprinters per uur reis tussen Almere
59
+ Muziekwijk en Naarden-Bussum via Weesp"
60
+ planned_disruption.message.should == "Test message"
61
+ end
49
62
 
50
- xit "should not return disruption when empty in response" do
63
+ it "should retrieve expected unplanned disruption" do
64
+ stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-storingen?", load_fixture('disruptions.xml')
65
+ disruptions = @client.disruptions
66
+ disruptions.size.should == 2
67
+ unplanned_disruption = disruptions[:unplanned].first
68
+ unplanned_disruption.class.should == NSClient::UnplannedDisruption
69
+
70
+ unplanned_disruption.id.should == "prio-13345"
71
+ unplanned_disruption.trip.should == "'s-Hertogenbosch-Nijmegen"
72
+ unplanned_disruption.reason.should == "beperkingen op last van de politie"
73
+ unplanned_disruption.message.should == "Another test message"
74
+ unplanned_disruption.datetime_string == "2010-12-16T11:16:00+0100" #intentional, give raw data. Let user parse if needed.
75
+ end
76
+
77
+ it "should not return disruption when empty in response" do
51
78
  stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-storingen?", load_fixture('no_disruptions.xml')
52
79
  disruptions = @client.disruptions
53
80
  disruptions.size.should == 2
@@ -55,7 +82,23 @@ describe NSClient do
55
82
  disruptions[:unplanned].size.should == 0
56
83
  end
57
84
 
58
- xit "should retrieve disruptions for station name" # ie, for Amsterdam only (http://webservices.ns.nl/ns-api-storingen?station=Amsterdam)
85
+ describe "for a specific station" do
86
+
87
+ it "should retrieve disruptions for station name" do
88
+ # ie, for Amsterdam only (http://webservices.ns.nl/ns-api-storingen?station=Amsterdam)
89
+ stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-storingen?station=Amsterdam", load_fixture('disruptions_amsterdam.xml')
90
+ disruptions = @client.disruptions "Amsterdam"
91
+ disruptions.size.should == 2
92
+ disruptions[:planned].size.should == 4
93
+ disruptions[:unplanned].size.should == 0
94
+ end
95
+
96
+ it "should raise an error when using invalid station name" do
97
+ stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-storingen?station=bla", load_fixture('disruption_invalid_station_name.xml')
98
+ expect { @client.disruptions "bla" }.to raise_error(NSClient::InvalidStationNameError, "Could not find a station with name 'bla'")
99
+ end
100
+ end
101
+
59
102
 
60
103
  end
61
104
 
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.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -59,7 +59,7 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
- description: Yet Another (Ruby) NS API
62
+ description: Yet Another (Ruby) NS API client
63
63
  email:
64
64
  - stefanhen83@gmail.com
65
65
  executables: []
@@ -75,6 +75,7 @@ files:
75
75
  - lib/ns_client.rb
76
76
  - ns.gemspec
77
77
  - rakefile.rb
78
+ - spec/fixtures/disruption_invalid_station_name.xml
78
79
  - spec/fixtures/disruptions.xml
79
80
  - spec/fixtures/disruptions_amsterdam.xml
80
81
  - spec/fixtures/no_disruptions.xml
@@ -97,7 +98,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
97
98
  version: '0'
98
99
  segments:
99
100
  - 0
100
- hash: -1647499976978376550
101
+ hash: 2729208027955836065
101
102
  required_rubygems_version: !ruby/object:Gem::Requirement
102
103
  none: false
103
104
  requirements:
@@ -106,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
107
  version: '0'
107
108
  segments:
108
109
  - 0
109
- hash: -1647499976978376550
110
+ hash: 2729208027955836065
110
111
  requirements: []
111
112
  rubyforge_project:
112
113
  rubygems_version: 1.8.24
@@ -114,6 +115,7 @@ signing_key:
114
115
  specification_version: 3
115
116
  summary: A Ruby client for the NS (Dutch Railways) API
116
117
  test_files:
118
+ - spec/fixtures/disruption_invalid_station_name.xml
117
119
  - spec/fixtures/disruptions.xml
118
120
  - spec/fixtures/disruptions_amsterdam.xml
119
121
  - spec/fixtures/no_disruptions.xml